>
>
>
V6111. Potentially negative value is us…


V6111. Potentially negative value is used as the size of an array.

The analyzer detected that a potentially negative value of a variable or expression might be used as the size of an array that is created.

Let's look at an example:

void process(boolean isNotCsv) {
    String str = "column1,column2";
    if (isNotCsv) {
        str = "content";
    }

    var arr = new String[str.indexOf(',')];
    ....
}

The value returned by the 'indexOf' method may be -1. This happens if the string does not contain the specified character. Then, when you create the 'arr' array, its length will be negative. This will result in the 'NegativeArraySizeException' type exception.

The fixed version of the 'process' method may look like this:

public static void process(boolean isNotCsv) {
    String str = "column1,column2";
    if (isNotCsv) {
        str = "content";
    } else {
        var arr = new String[str.indexOf(',')];
        ....
    }
    ....
}

This diagnostic is classified as: