...
Noncompliant Code Example (Struct)
In the example below, there is an inventory system keeping track of the total number of different items (denoted {{ Wiki Markup length
}}). Each item is given an index in the array, and the value for that index is the stock of that item. Adding a new item would increase {{length
}} in the struct. Stocking more of an item would increase the value for that item's index. For example, if 5 books and 2 erasers were in stock, the inventory would be {{stockOfItem
\[0
\]
=
5
}} and {{stockOfItem
\[1
\]
=
2
}}, assuming books were index 0 and erasers were index 1.
The problem arises in this setup when no items are being stocked. getStock
would recognize that length = 0
and thus would return NULL
. In this noncompliant code example, erroneous behavior results from getStock
returning NULL
while main
neglects to check for such a value. This results in an abnormal program termination after returning to the main
function.
...
Search for vulnerabilities resulting from the violation of this guideline on the CERT website.
Bibliography
\[[Bloch 2008|java:AA. References#Bloch 08]\] Item 43: return empty arrays or collections, not nulls Wiki Markup
...