Skip to content

Collection Size Or Array Length

Use the appropriate condition to determine the size of a Collection or the length of an Array.

What does this mean ?

A collection's size and an array's length are always higher than or equal to zero. As a result, you should use the right condition to check the size of a Collection or the length of an Array.

What can happen ?

As a result, evaluating if a size or length is bigger than or equal to zero makes no sense because the answer is always true. Similarly, if it is less than zero, it will always return false.

Sample Code

Vulnerable :

if(collection.Count >= 0){...}

if(enumerable.Count() < 0){...}

if(array.Length >= 0){...}

bool result = array.Length >=0;

Non Vulnerable :

if (list.Any()) { ... }

if (list.Count > 0) { ... }

if (array.Length >= 42) { ... }

Vulnerable :

if (myList.size() >= 0) { ... }

if (myList.size() < 0) { ... }

boolean result = myArray.length >= 0;

if (0 > myArray.length) { ... }

Non Vulnerable :

if (!myList.isEmpty()) { ... }

if (myArray.length >= 42) { ... }

Vulnerable :

const result = arr.length >= 0;  // Vulnerable

Non Vulnerable :

const result = arr.length > 0; // Non Vulnerable

References