Skip to content

Commit c62ae3b

Browse files
geoffw0d10c
authored andcommitted
C++: First working. We now prefer flagging the cases where the variable was initialized, as in real world cases we haven't seen it done safely.
1 parent 76ef779 commit c62ae3b

File tree

3 files changed

+33
-9
lines changed

3 files changed

+33
-9
lines changed

cpp/ql/src/Critical/MissingCheckScanf.ql

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@
22
* @name TODO
33
* @description TODO
44
* @kind problem
5-
* @problem.severity TODO
5+
* @problem.severity warning
66
* @security-severity TODO
77
* @precision TODO
88
* @id cpp/missing-check-scanf
99
* @tags TODO
1010
*/
1111

1212
import cpp
13+
import semmle.code.cpp.commons.Scanf
1314

14-
select "TODO"
15+
from ScanfFunction scanf, FunctionCall fc
16+
where
17+
fc.getTarget() = scanf and
18+
fc instanceof ExprInVoidContext
19+
select fc, "This is a call to scanf."
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
| test.cpp:23:3:23:7 | call to scanf | This is a call to scanf. |
2+
| test.cpp:39:3:39:7 | call to scanf | This is a call to scanf. |
3+
| test.cpp:48:3:48:8 | call to fscanf | This is a call to scanf. |
4+
| test.cpp:55:3:55:8 | call to sscanf | This is a call to scanf. |
5+
| test.cpp:135:3:135:7 | call to scanf | This is a call to scanf. |
6+
| test.cpp:143:3:143:7 | call to scanf | This is a call to scanf. |
7+
| test.cpp:151:3:151:7 | call to scanf | This is a call to scanf. |
8+
| test.cpp:163:3:163:7 | call to scanf | This is a call to scanf. |
9+
| test.cpp:173:3:173:7 | call to scanf | This is a call to scanf. |

cpp/ql/test/query-tests/Critical/MissingCheckScanf/test.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ int main()
3636
{
3737
int i = 0;
3838

39-
scanf("%d", &i); // GOOD: we assume the initialization of `i` is a reasonable default
39+
scanf("%d", &i); // BAD
4040
use(i);
4141
}
4242

@@ -79,7 +79,7 @@ int main()
7979
{
8080
int i;
8181

82-
if (scanf("%d", &i) != 0) // GOOD (just barely)
82+
if (scanf("%d", &i) != 0) // BAD: scanf can return -1 [NOT DETECTED]
8383
{
8484
use(i);
8585
}
@@ -88,7 +88,7 @@ int main()
8888
{
8989
int i;
9090

91-
if (scanf("%d", &i) == 0) // BAD: checks return value incorrectly
91+
if (scanf("%d", &i) == 0) // BAD: checks return value incorrectly [NOT DETECTED]
9292
{
9393
use(i);
9494
}
@@ -119,7 +119,7 @@ int main()
119119
{
120120
int i, j;
121121

122-
if (scanf("%d %d", &i) >= 1) // BAD: checks return value incorrectly
122+
if (scanf("%d %d", &i, &j) >= 1) // BAD: checks return value incorrectly [NOT DETECTED]
123123
{
124124
use(i);
125125
use(j);
@@ -132,23 +132,23 @@ int main()
132132
int i;
133133
i = 0;
134134

135-
scanf("%d", &i); // GOOD
135+
scanf("%d", &i); // BAD
136136
use(i);
137137
}
138138

139139
{
140140
int i;
141141

142142
set_by_ref(i);
143-
scanf("%d", &i); // GOOD: we have to assume `i` was initialized
143+
scanf("%d", &i); // BAD
144144
use(i);
145145
}
146146

147147
{
148148
int i;
149149

150150
set_by_ptr(&i);
151-
scanf("%d", &i); // GOOD: we have to assume `i` was initialized
151+
scanf("%d", &i); // BAD
152152
use(i);
153153
}
154154

@@ -164,6 +164,16 @@ int main()
164164
use(i);
165165
}
166166

167+
// --- different use ---
168+
169+
{
170+
int i;
171+
int *ptr_i = &i;
172+
173+
scanf("%d", &i); // BAD: may not have written `i`
174+
use(*ptr_i);
175+
}
176+
167177
// --- weird formatting strings ---
168178

169179
{

0 commit comments

Comments
 (0)