Skip to content

Commit bdad847

Browse files
authored
Merge pull request github#12422 from github/redsun82/cpp-scanf-fp
C++: add false positives to `MissingCheckScanf` test
2 parents 7627a53 + 429518b commit bdad847

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,6 @@
1919
| test.cpp:302:8:302:12 | ptr_i | This variable is read, but may not have been written. It should be guarded by a check that the $@ returns at least 1. | test.cpp:301:3:301:7 | call to scanf | call to scanf |
2020
| test.cpp:310:7:310:7 | i | This variable is read, but may not have been written. It should be guarded by a check that the $@ returns at least 1. | test.cpp:309:3:309:7 | call to scanf | call to scanf |
2121
| test.cpp:404:25:404:25 | u | This variable is read, but may not have been written. It should be guarded by a check that the $@ returns at least 1. | test.cpp:403:6:403:11 | call to sscanf | call to sscanf |
22+
| test.cpp:416:7:416:7 | i | This variable is read, but may not have been written. It should be guarded by a check that the $@ returns at least 1. | test.cpp:413:7:413:11 | call to scanf | call to scanf |
23+
| test.cpp:423:7:423:7 | i | This variable is read, but may not have been written. It should be guarded by a check that the $@ returns at least 1. | test.cpp:420:7:420:11 | call to scanf | call to scanf |
24+
| test.cpp:430:6:430:6 | i | This variable is read, but may not have been written. It should be guarded by a check that the $@ returns at least 1. | test.cpp:429:2:429:6 | call to scanf | call to scanf |

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,3 +406,26 @@ char *my_string_copy() {
406406
*ptr++ = 0;
407407
return DST_STRING;
408408
}
409+
410+
void scan_and_write() {
411+
{
412+
int i;
413+
if (scanf("%d", &i) < 1) {
414+
i = 0;
415+
}
416+
use(i); // GOOD [FALSE POSITIVE]: variable is overwritten with a default value when scanf fails
417+
}
418+
{
419+
int i;
420+
if (scanf("%d", &i) != 1) {
421+
i = 0;
422+
}
423+
use(i); // GOOD [FALSE POSITIVE]: variable is overwritten with a default value when scanf fails
424+
}
425+
}
426+
427+
void scan_and_static_variable() {
428+
static int i;
429+
scanf("%d", &i);
430+
use(i); // GOOD [FALSE POSITIVE]: static variables are always 0-initialized
431+
}

0 commit comments

Comments
 (0)