Skip to content

Commit 2af82d9

Browse files
LF for .qhelp files too
1 parent dfb082e commit 2af82d9

File tree

4 files changed

+132
-131
lines changed

4 files changed

+132
-131
lines changed

.editorconfig

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
[*.{ql,qll,qlref,dbscheme,}]
1+
[*.{ql,qll,qlref,dbscheme,qhelp}]
22
end_of_line = lf

.gitattributes

+1
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@
1414
*.qll eol=lf
1515
*.qlref eol=lf
1616
*.dbscheme eol=lf
17+
*.qhelp eol=lf
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,41 @@
1-
<!DOCTYPE qhelp 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-
<sample src="ComparisonWithWiderType.c" />
29-
30-
</example>
31-
32-
<references>
33-
<li>
34-
<a href="https://docs.microsoft.com/en-us/cpp/cpp/data-type-ranges">Data type ranges</a>
35-
</li>
36-
37-
<li>
38-
<a href="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+
<!DOCTYPE qhelp 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+
<sample src="ComparisonWithWiderType.c" />
29+
30+
</example>
31+
32+
<references>
33+
<li>
34+
<a href="https://docs.microsoft.com/en-us/cpp/cpp/data-type-ranges">Data type ranges</a>
35+
</li>
36+
37+
<li>
38+
<a href="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>
4141
</qhelp>
Original file line numberDiff line numberDiff line change
@@ -1,90 +1,90 @@
1-
<!DOCTYPE qhelp PUBLIC
2-
"-//Semmle//qhelp//EN"
3-
"qhelp.dtd">
4-
<qhelp>
5-
6-
<overview>
7-
<p>There are a number of Boolean expression patterns that can easily be rewritten
8-
to make them simpler.
9-
Boolean expressions involving comparisons with Boolean literals,
10-
ternary conditionals with a Boolean literal as one of the results,
11-
double negations, or negated comparisons can all be changed to
12-
equivalent and simpler expressions.</p>
13-
</overview>
14-
15-
<recommendation>
16-
<p>If <code>A</code> and <code>B</code> are expressions of Boolean type, you can
17-
simplify them using the rewrites shown below.</p>
18-
19-
<table><tbody>
20-
<tr><th>Expression</th><th></th><th>Simplified expression</th></tr>
21-
<tr><td><code>A == true</code></td><td></td><td><code>A</code></td></tr>
22-
<tr><td><code>A != false</code></td><td></td><td><code>A</code></td></tr>
23-
<tr><td><code>A == false</code></td><td></td><td><code>!A</code></td></tr>
24-
<tr><td><code>A != true</code></td><td></td><td><code>!A</code></td></tr>
25-
<tr><td><code>A ? true : B</code></td><td></td><td><code>A || B</code></td></tr>
26-
<tr><td><code>A ? B : false</code></td><td></td><td><code>A &amp;&amp; B</code></td></tr>
27-
<tr><td><code>A ? B : true</code></td><td></td><td><code>!A || B</code></td></tr>
28-
<tr><td><code>A ? false : B</code></td><td></td><td><code>!A &amp;&amp; B</code></td></tr>
29-
<tr><td><code>A ? true : false</code></td><td></td><td><code>A</code></td></tr>
30-
<tr><td><code>A ? false : true</code></td><td></td><td><code>!A</code></td></tr>
31-
<tr><td><code>!!A</code></td><td></td><td><code>A</code></td></tr>
32-
<tr><td><code>A &amp;&amp; true</code></td><td></td><td><code>A</code></td></tr>
33-
<tr><td><code>A || false</code></td><td></td><td><code>A</code></td></tr>
34-
</tbody></table>
35-
36-
<p>Some expressions always yield a constant value. If the side-effect in
37-
<code>A</code> is intended, consider restructuring the code to make this more clear.
38-
Otherwise, replace the expression with the constant value as shown below.</p>
39-
40-
<table><tbody>
41-
<tr><th>Expression</th><th></th><th>Value</th></tr>
42-
<tr><td><code>A &amp;&amp; false</code></td><td></td><td><code>false</code></td></tr>
43-
<tr><td><code>A || true</code></td><td></td><td><code>true</code></td></tr>
44-
<tr><td><code>A ? true : true</code></td><td></td><td><code>true</code></td></tr>
45-
<tr><td><code>A ? false : false</code></td><td></td><td><code>false</code></td></tr>
46-
</tbody></table>
47-
48-
<p>In addition to the rewrites above, negated comparisons can also be simplified in the following way:</p>
49-
50-
<table><tbody>
51-
<tr><th>Expression</th><th></th><th>Simplified expression</th></tr>
52-
<tr><td><code>!(A == B)</code></td><td></td><td><code>A != B</code></td></tr>
53-
<tr><td><code>!(A != B)</code></td><td></td><td><code>A == B</code></td></tr>
54-
<tr><td><code>!(A &lt; B)</code></td><td></td><td><code>A >= B</code></td></tr>
55-
<tr><td><code>!(A > B)</code></td><td></td><td><code>A &lt;= B</code></td></tr>
56-
<tr><td><code>!(A &lt;= B)</code></td><td></td><td><code>A > B</code></td></tr>
57-
<tr><td><code>!(A >= B)</code></td><td></td><td><code>A &lt; B</code></td></tr>
58-
</tbody></table>
59-
60-
</recommendation>
61-
62-
<example>
63-
<p>
64-
In the following example, the properties <code>Espresso</code>, <code>Latte</code>, and <code>Grande</code>
65-
are written in a complex way and can be simplified.
66-
</p>
67-
68-
<sample src="SimplifyBoolExprBad.cs" />
69-
70-
<p>The code below shows the same logic expressed in a simpler and more readable way.</p>
71-
72-
<sample src="SimplifyBoolExprGood.cs" />
73-
</example>
74-
75-
<references>
76-
77-
<li>
78-
Microsoft C# Reference:
79-
<a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/logical-negation-operator">! Operator</a>,
80-
<a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/equality-comparison-operator">== Operator</a>,
81-
<a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/not-equal-operator">!= Operator</a>,
82-
<a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-and-operator">&amp;&amp; Operator</a>,
83-
<a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-or-operator">|| Operator</a>,
84-
<a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-operator">?: Operator</a>,
85-
<a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/less-than-operator">&lt; Operator</a>.
86-
</li>
87-
88-
</references>
89-
90-
</qhelp>
1+
<!DOCTYPE qhelp PUBLIC
2+
"-//Semmle//qhelp//EN"
3+
"qhelp.dtd">
4+
<qhelp>
5+
6+
<overview>
7+
<p>There are a number of Boolean expression patterns that can easily be rewritten
8+
to make them simpler.
9+
Boolean expressions involving comparisons with Boolean literals,
10+
ternary conditionals with a Boolean literal as one of the results,
11+
double negations, or negated comparisons can all be changed to
12+
equivalent and simpler expressions.</p>
13+
</overview>
14+
15+
<recommendation>
16+
<p>If <code>A</code> and <code>B</code> are expressions of Boolean type, you can
17+
simplify them using the rewrites shown below.</p>
18+
19+
<table><tbody>
20+
<tr><th>Expression</th><th></th><th>Simplified expression</th></tr>
21+
<tr><td><code>A == true</code></td><td></td><td><code>A</code></td></tr>
22+
<tr><td><code>A != false</code></td><td></td><td><code>A</code></td></tr>
23+
<tr><td><code>A == false</code></td><td></td><td><code>!A</code></td></tr>
24+
<tr><td><code>A != true</code></td><td></td><td><code>!A</code></td></tr>
25+
<tr><td><code>A ? true : B</code></td><td></td><td><code>A || B</code></td></tr>
26+
<tr><td><code>A ? B : false</code></td><td></td><td><code>A &amp;&amp; B</code></td></tr>
27+
<tr><td><code>A ? B : true</code></td><td></td><td><code>!A || B</code></td></tr>
28+
<tr><td><code>A ? false : B</code></td><td></td><td><code>!A &amp;&amp; B</code></td></tr>
29+
<tr><td><code>A ? true : false</code></td><td></td><td><code>A</code></td></tr>
30+
<tr><td><code>A ? false : true</code></td><td></td><td><code>!A</code></td></tr>
31+
<tr><td><code>!!A</code></td><td></td><td><code>A</code></td></tr>
32+
<tr><td><code>A &amp;&amp; true</code></td><td></td><td><code>A</code></td></tr>
33+
<tr><td><code>A || false</code></td><td></td><td><code>A</code></td></tr>
34+
</tbody></table>
35+
36+
<p>Some expressions always yield a constant value. If the side-effect in
37+
<code>A</code> is intended, consider restructuring the code to make this more clear.
38+
Otherwise, replace the expression with the constant value as shown below.</p>
39+
40+
<table><tbody>
41+
<tr><th>Expression</th><th></th><th>Value</th></tr>
42+
<tr><td><code>A &amp;&amp; false</code></td><td></td><td><code>false</code></td></tr>
43+
<tr><td><code>A || true</code></td><td></td><td><code>true</code></td></tr>
44+
<tr><td><code>A ? true : true</code></td><td></td><td><code>true</code></td></tr>
45+
<tr><td><code>A ? false : false</code></td><td></td><td><code>false</code></td></tr>
46+
</tbody></table>
47+
48+
<p>In addition to the rewrites above, negated comparisons can also be simplified in the following way:</p>
49+
50+
<table><tbody>
51+
<tr><th>Expression</th><th></th><th>Simplified expression</th></tr>
52+
<tr><td><code>!(A == B)</code></td><td></td><td><code>A != B</code></td></tr>
53+
<tr><td><code>!(A != B)</code></td><td></td><td><code>A == B</code></td></tr>
54+
<tr><td><code>!(A &lt; B)</code></td><td></td><td><code>A >= B</code></td></tr>
55+
<tr><td><code>!(A > B)</code></td><td></td><td><code>A &lt;= B</code></td></tr>
56+
<tr><td><code>!(A &lt;= B)</code></td><td></td><td><code>A > B</code></td></tr>
57+
<tr><td><code>!(A >= B)</code></td><td></td><td><code>A &lt; B</code></td></tr>
58+
</tbody></table>
59+
60+
</recommendation>
61+
62+
<example>
63+
<p>
64+
In the following example, the properties <code>Espresso</code>, <code>Latte</code>, and <code>Grande</code>
65+
are written in a complex way and can be simplified.
66+
</p>
67+
68+
<sample src="SimplifyBoolExprBad.cs" />
69+
70+
<p>The code below shows the same logic expressed in a simpler and more readable way.</p>
71+
72+
<sample src="SimplifyBoolExprGood.cs" />
73+
</example>
74+
75+
<references>
76+
77+
<li>
78+
Microsoft C# Reference:
79+
<a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/logical-negation-operator">! Operator</a>,
80+
<a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/equality-comparison-operator">== Operator</a>,
81+
<a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/not-equal-operator">!= Operator</a>,
82+
<a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-and-operator">&amp;&amp; Operator</a>,
83+
<a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-or-operator">|| Operator</a>,
84+
<a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/conditional-operator">?: Operator</a>,
85+
<a href="https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/less-than-operator">&lt; Operator</a>.
86+
</li>
87+
88+
</references>
89+
90+
</qhelp>

0 commit comments

Comments
 (0)