False positive example for S2589

Please let me know if you need anything additional.

Thanks,
g

/** OS Platform: macOS 10.15.3
 * IDE Platform: Eclipse - SonarLint for Eclipse 4.3.0.12432
                 org.sonarlint.eclipse.feature.feature.group SonarSource
 * Language: Java - openjdk version "13.0.1" 2019-10-15
 * 
 * False Positive: Boolean expressions should not be gratuitous (squid:S2589)
 * Description: This method is from the dnsjava class 'Address.java'.
                The final 'if' of this method is flagged.
 * numDigits is NOT always zero as the SonarLint analysis suggests. */
private byte[] parseV4(String s) {
    int numDigits;
    int currentOctet;
    final byte[] values = new byte[4];
    int currentValue;
    final int length = s.length();

    currentOctet = 0;
    currentValue = 0;
    numDigits = 0;
    for (int i = 0; i < length; i++) {
        final char c = s.charAt(i);
        if (c >= '0' && c <= '9') {
            /* Can't have more than 3 digits per octet. */
            if (numDigits == 3)
                return null;
            /* Octets shouldn't start with 0, unless they are 0. */
            if (numDigits > 0 && currentValue == 0)
                return null;
            numDigits++;
            currentValue *= 10;
            currentValue += c - '0';
            /* 255 is the maximum value for an octet. */
            if (currentValue > 255)
                return null;
        } else if (c == '.') {
            /* Can't have more than 3 dots. */
            if (currentOctet == 3)
                return null;
            /* Two consecutive dots are bad. */
            if (numDigits == 0)
                return null;
            values[currentOctet++] = (byte) currentValue;
            currentValue = 0;
            numDigits = 0;
        } else
            return null;
    }
    /* Must have 4 octets. */
    if (currentOctet != 3)
        return null;
    /* The fourth octet can't be empty. */
    if (numDigits == 0)
        return null;
    values[currentOctet] = (byte) currentValue;
    return values;
}

Hello

This issue looks to me like a known limitation of the engine, reported here: SONARJAVA-2523
It seems like a complex problem already there for quite some time, I added your example to the list, it will add more data to better understand the problem.
In the meantime, I advise you to resolve this issue as a false positive.

Best,
Quentin

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.