-
Notifications
You must be signed in to change notification settings - Fork 2k
Expand file tree
/
Copy pathContainerLengthCmpOffByOne.qhelp
More file actions
50 lines (43 loc) · 1.74 KB
/
ContainerLengthCmpOffByOne.qhelp
File metadata and controls
50 lines (43 loc) · 1.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<!DOCTYPE qhelp PUBLIC
"-//Semmle//qhelp//EN"
"qhelp.dtd">
<qhelp>
<overview>
<p>
An indexing operation against a collection, string or array should use an index at most one less
than the length. If the index to be accessed is compared to be less than or equal to the length
(<code><=</code>), instead of less than the length (<code><</code>), the index could be out
of bounds.
</p>
</overview>
<recommendation>
<p>
Use less than (<code><</code>) rather than less than or equals (<code><=</code>) when
comparing a potential index against a length. For loops that iterate over every element in an array
or collection, prefer to use the <code>foreach</code> syntax instead of looping over explicit
indexes.
</p>
</recommendation>
<example>
<p>
The following example shows two methods which identify whether a value appears in a comma-separated
list of values.
</p>
<p>
In the first example, a loop using an index variable - <code>i</code> - is used to iterate over the
elements in the comma-separated list. However, the terminating condition of the loop is incorrectly
specified as <code>i <= values.Length</code>. This condition will succeed if <code>i</code> is equal
to <code>values.Length</code>, but the access <code>values[i]</code> in the body of the loop
will fail because the index is out of bounds.
</p>
<p>
One potential solution would be to replace <code>i <= values.Length</code> with
<code>i < values.Length</code>. However, arrays in C# can also be used in <code>foreach</code>
loops. This is shown in the second example. This circumvents the need to specify an index at all,
and therefore prevents errors where the index is out of bounds.
</p>
<sample src="ContainerLengthCmpOffByOne.cs" />
</example>
<references>
</references>
</qhelp>