|
6 | 6 |
|
7 | 7 | <overview> |
8 | 8 | <p> |
9 | | -This rule finds integer values that are first used to index an array and |
| 9 | +This query finds integer values that are first used to index an array and |
10 | 10 | subsequently tested for being negative. If it is relevant to perform this test |
11 | | -at all then it should probably happen <em>before</em> the indexing, not |
| 11 | +at all then it should happen <em>before</em> the indexing, not |
12 | 12 | after. Otherwise, if the value is negative then the program will have failed |
13 | 13 | before performing the test. |
14 | 14 | </p> |
15 | | -<include src="dataFlowWarning.qhelp" /> |
16 | 15 |
|
| 16 | +<include src="dataFlowWarning.qhelp" /> |
17 | 17 | </overview> |
| 18 | + |
18 | 19 | <recommendation> |
19 | 20 | <p> |
20 | | -See if the value needs checking before being used as array index. Double-check |
| 21 | +See if the value needs to be checked before being used as array index. Double-check |
21 | 22 | if the value is derived from user input. If the value clearly cannot be |
22 | 23 | negative then the negativity test is redundant and can be removed. |
23 | 24 | </p> |
24 | | - |
25 | 25 | </recommendation> |
26 | | -<example> |
27 | | -<sample src="LateNegativeTest.cpp" /> |
28 | | - |
29 | | - |
30 | | - |
31 | 26 |
|
| 27 | +<example> |
| 28 | +<p>The example below includes two functions that use the value <code>recordIdx</code> to |
| 29 | +index an array and a test to verify that the value is positive. |
| 30 | +The test is made after <code>records</code> is indexed for <code>printRecord</code> and |
| 31 | +before <code>records</code> is indexed for <code>processRecord</code>. |
| 32 | +Unless the value of <code>recordIdx</code> cannot be negative, the test should be |
| 33 | +updated to run before <em>both</em> times the array is indexed. |
| 34 | +If the value cannot be negative, the test should be removed. |
| 35 | +</p> |
32 | 36 |
|
| 37 | +<sample src="LateNegativeTest.cpp" /> |
33 | 38 | </example> |
| 39 | + |
| 40 | +<references> |
| 41 | +<li>cplusplus.com: <a href="http://www.cplusplus.com/doc/tutorial/pointers/">Pointers</a>.</li> |
| 42 | +<li>SEI CERT C Coding Standard: <a href="https://wiki.sei.cmu.edu/confluence/display/c/ARR30-C.+Do+not+form+or+use+out-of-bounds+pointers+or+array+subscripts">ARR30-C. Do not form or use out-of-bounds pointers or array subscripts</a>.</li> |
| 43 | +</references> |
34 | 44 | </qhelp> |
0 commit comments