You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<p>In a loop condition, comparison of a value of a narrow type with a value of a wide type may
7
-
result in unexpected behavior if the wider value is sufficiently large (or small). This is because
8
-
the narrower value may overflow. This can lead to an infinite loop.</p>
9
-
10
-
</overview>
11
-
<recommendation>
12
-
13
-
<p>Change the types of the compared values so that the value on the narrower side of the
14
-
comparison is at least as wide as the value it is being compared with.</p>
15
-
16
-
</recommendation>
17
-
<example>
18
-
19
-
<p>In this example, <code>bytes_received</code> is compared against <code>max_get</code> in a
20
-
<code>while</code> loop. However, <code>bytes_received</code> is an <code>int16_t</code>, and
21
-
<code>max_get</code> is an <code>int32_t</code>. Because <code>max_get</code> is larger than
22
-
<code>INT16_MAX</code>, the loop condition is always <code>true</code>, so the loop never
23
-
terminates.</p>
24
-
25
-
<p>This problem is avoided in the 'GOOD' case because <code>bytes_received2</code> is an
26
-
<code>int32_t</code>, which is as wide as the type of <code>max_get</code>.</p>
27
-
28
-
<samplesrc="ComparisonWithWiderType.c" />
29
-
30
-
</example>
31
-
32
-
<references>
33
-
<li>
34
-
<ahref="https://docs.microsoft.com/en-us/cpp/cpp/data-type-ranges">Data type ranges</a>
35
-
</li>
36
-
37
-
<li>
38
-
<ahref="https://wiki.sei.cmu.edu/confluence/display/c/INT18-C.+Evaluate+integer+expressions+in+a+larger+size+before+comparing+or+assigning+to+that+size">INT18-C. Evaluate integer expressions in a larger size before comparing or assigning to that size </a>
39
-
</li>
40
-
</references>
1
+
<!DOCTYPEqhelp PUBLIC
2
+
"-//Semmle//qhelp//EN"
3
+
"qhelp.dtd">
4
+
<qhelp>
5
+
<overview>
6
+
<p>In a loop condition, comparison of a value of a narrow type with a value of a wide type may
7
+
result in unexpected behavior if the wider value is sufficiently large (or small). This is because
8
+
the narrower value may overflow. This can lead to an infinite loop.</p>
9
+
10
+
</overview>
11
+
<recommendation>
12
+
13
+
<p>Change the types of the compared values so that the value on the narrower side of the
14
+
comparison is at least as wide as the value it is being compared with.</p>
15
+
16
+
</recommendation>
17
+
<example>
18
+
19
+
<p>In this example, <code>bytes_received</code> is compared against <code>max_get</code> in a
20
+
<code>while</code> loop. However, <code>bytes_received</code> is an <code>int16_t</code>, and
21
+
<code>max_get</code> is an <code>int32_t</code>. Because <code>max_get</code> is larger than
22
+
<code>INT16_MAX</code>, the loop condition is always <code>true</code>, so the loop never
23
+
terminates.</p>
24
+
25
+
<p>This problem is avoided in the 'GOOD' case because <code>bytes_received2</code> is an
26
+
<code>int32_t</code>, which is as wide as the type of <code>max_get</code>.</p>
27
+
28
+
<samplesrc="ComparisonWithWiderType.c" />
29
+
30
+
</example>
31
+
32
+
<references>
33
+
<li>
34
+
<ahref="https://docs.microsoft.com/en-us/cpp/cpp/data-type-ranges">Data type ranges</a>
35
+
</li>
36
+
37
+
<li>
38
+
<ahref="https://wiki.sei.cmu.edu/confluence/display/c/INT18-C.+Evaluate+integer+expressions+in+a+larger+size+before+comparing+or+assigning+to+that+size">INT18-C. Evaluate integer expressions in a larger size before comparing or assigning to that size </a>
0 commit comments