Skip to content

Commit a9f6b21

Browse files
committed
C++: Add new test cases for cpp/incorrectly-checked-scanf.
1 parent a63e70d commit a9f6b21

File tree

3 files changed

+56
-1
lines changed

3 files changed

+56
-1
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
| test.cpp:204:7:204:11 | call to scanf | The result of scanf is only checked against 0, but it can also return EOF. |
44
| test.cpp:436:7:436:11 | call to scanf | The result of scanf is only checked against 0, but it can also return EOF. |
55
| test.cpp:443:11:443:15 | call to scanf | The result of scanf is only checked against 0, but it can also return EOF. |
6+
| test.cpp:501:13:501:17 | call to scanf | The result of scanf is only checked against 0, but it can also return EOF. |
7+
| test.cpp:512:13:512:17 | call to scanf | The result of scanf is only checked against 0, but it can also return EOF. |

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ edges
3737
| test.cpp:420:19:420:20 | scanf output argument | test.cpp:423:7:423:7 | i | provenance | |
3838
| test.cpp:455:41:455:46 | sscanf output argument | test.cpp:460:6:460:10 | value | provenance | |
3939
| test.cpp:467:20:467:25 | scanf output argument | test.cpp:474:6:474:10 | value | provenance | |
40+
| test.cpp:480:25:480:26 | scanf output argument | test.cpp:484:9:484:9 | i | provenance | |
41+
| test.cpp:491:25:491:26 | scanf output argument | test.cpp:495:8:495:8 | i | provenance | |
4042
nodes
4143
| test.cpp:34:15:34:16 | scanf output argument | semmle.label | scanf output argument |
4244
| test.cpp:35:7:35:7 | i | semmle.label | i |
@@ -114,6 +116,10 @@ nodes
114116
| test.cpp:460:6:460:10 | value | semmle.label | value |
115117
| test.cpp:467:20:467:25 | scanf output argument | semmle.label | scanf output argument |
116118
| test.cpp:474:6:474:10 | value | semmle.label | value |
119+
| test.cpp:480:25:480:26 | scanf output argument | semmle.label | scanf output argument |
120+
| test.cpp:484:9:484:9 | i | semmle.label | i |
121+
| test.cpp:491:25:491:26 | scanf output argument | semmle.label | scanf output argument |
122+
| test.cpp:495:8:495:8 | i | semmle.label | i |
117123
subpaths
118124
#select
119125
| test.cpp:35:7:35:7 | i | test.cpp:34:15:34:16 | scanf output argument | test.cpp:35:7:35: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:34:3:34:7 | call to scanf | call to scanf |
@@ -134,3 +140,5 @@ subpaths
134140
| test.cpp:423:7:423:7 | i | test.cpp:420:19:420:20 | scanf output argument | 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 |
135141
| test.cpp:460:6:460:10 | value | test.cpp:455:41:455:46 | sscanf output argument | test.cpp:460:6:460:10 | value | 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:455:12:455:17 | call to sscanf | call to sscanf |
136142
| test.cpp:474:6:474:10 | value | test.cpp:467:20:467:25 | scanf output argument | test.cpp:474:6:474:10 | value | 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:467:8:467:12 | call to scanf | call to scanf |
143+
| test.cpp:484:9:484:9 | i | test.cpp:480:25:480:26 | scanf output argument | test.cpp:484:9:484:9 | 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:480:13:480:17 | call to scanf | call to scanf |
144+
| test.cpp:495:8:495:8 | i | test.cpp:491:25:491:26 | scanf output argument | test.cpp:495:8:495:8 | 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:491:13:491:17 | call to scanf | call to scanf |

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

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,4 +472,49 @@ void check_for_negative_test() {
472472
return;
473473
}
474474
use(value);
475-
}
475+
}
476+
477+
void multiple_checks() {
478+
{
479+
int i;
480+
int res = scanf("%d", &i);
481+
482+
if (res >= 0) {
483+
if (res != 0) {
484+
use(i); // GOOD: checks return value [FALSE POSITIVE]
485+
}
486+
}
487+
}
488+
489+
{
490+
int i;
491+
int res = scanf("%d", &i);
492+
493+
if (res < 0) return;
494+
if (res != 0) {
495+
use(i); // GOOD: checks return value [FALSE POSITIVE]
496+
}
497+
}
498+
499+
{
500+
int i;
501+
int res = scanf("%d", &i); // [FALSE POSITIVE]
502+
503+
if (res >= 1) {
504+
if (res != 0) {
505+
use(i); // GOOD: checks return value
506+
}
507+
}
508+
}
509+
510+
{
511+
int i;
512+
int res = scanf("%d", &i); // [FALSE POSITIVE]
513+
514+
if (res == 1) {
515+
if (res != 0) {
516+
use(i); // GOOD: checks return value
517+
}
518+
}
519+
}
520+
}

0 commit comments

Comments
 (0)