Skip to content

Commit d772ea0

Browse files
ihsinmejketema
andauthored
Apply suggestions from code review
Co-authored-by: Jeroen Ketema <[email protected]>
1 parent 0c8a072 commit d772ea0

File tree

3 files changed

+12
-16
lines changed

3 files changed

+12
-16
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
...
2-
r = scanf("%i", i);
2+
r = scanf("%i", &i);
33
if (r == 1) // GOOD
44
return i;
55
else
66
return -1;
77
...
8-
scanf("%i", i); // BAD
8+
scanf("%i", &i); // BAD
99
return i;
1010
...

cpp/ql/src/experimental/Security/CWE/CWE-754/ImproperCheckReturnValueScanf.ql

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**
2-
* @name Improper check return value scanf.
2+
* @name Improper check return value scanf
33
* @description Using a function call without the ability to evaluate the correctness of the work can lead to unexpected results.
44
* @kind problem
55
* @id cpp/improper-check-return-value-scanf
@@ -15,20 +15,16 @@ import cpp
1515
import semmle.code.cpp.commons.Exclusions
1616
import semmle.code.cpp.valuenumbering.GlobalValueNumbering
1717

18-
/** Returns the starting position of the argument being filled. */
18+
/** Returns the position of the first argument being filled. */
1919
int posArgumentInFunctionCall(FunctionCall fc) {
2020
(
2121
(
22-
fc.getTarget().hasGlobalOrStdName("scanf") or
23-
fc.getTarget().hasGlobalOrStdName("scanf_s")
22+
fc.getTarget().hasGlobalOrStdName(["scanf", "scanf_s"])
2423
) and
2524
result = 1
2625
or
2726
(
28-
fc.getTarget().hasGlobalOrStdName("fscanf") or
29-
fc.getTarget().hasGlobalOrStdName("sscanf") or
30-
fc.getTarget().hasGlobalOrStdName("fscanf_s") or
31-
fc.getTarget().hasGlobalOrStdName("sscanf_s")
27+
fc.getTarget().hasGlobalOrStdName(["fscanf", "sscanf", "fscanf_s", "sscanf_s"])
3228
) and
3329
result = 2
3430
)

cpp/ql/test/experimental/query-tests/Security/CWE/CWE-754/semmle/tests/test.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ int scanf(const char *format, ...);
22
int globalVal;
33
int functionWork1() {
44
int i;
5-
if (scanf("%i", i) == 1) // GOOD
5+
if (scanf("%i", &i) == 1) // GOOD
66
return i;
77
else
88
return -1;
@@ -11,7 +11,7 @@ int functionWork1() {
1111
int functionWork1_() {
1212
int i;
1313
int r;
14-
r = scanf("%i", i);
14+
r = scanf("%i", &i);
1515
if (r == 1) // GOOD
1616
return i;
1717
else
@@ -20,25 +20,25 @@ int functionWork1_() {
2020

2121
int functionWork1b() {
2222
int i;
23-
scanf("%i", i); // BAD
23+
scanf("%i", &i); // BAD
2424
return i;
2525
}
2626

2727
int functionWork2() {
2828
int i = 0;
29-
scanf("%i", i); // GOOD:the error can be determined by examining the initial value.
29+
scanf("%i", &i); // GOOD:the error can be determined by examining the initial value.
3030
return i;
3131
}
3232

3333
int functionWork2_() {
3434
int i;
3535
i = 0;
36-
scanf("%i", i); // GOOD:the error can be determined by examining the initial value.
36+
scanf("%i", &i); // GOOD:the error can be determined by examining the initial value.
3737
return i;
3838
}
3939
int functionWork2b() {
4040
int i;
41-
scanf("%i", i); // BAD
41+
scanf("%i", &i); // BAD
4242
globalVal = i;
4343
return 0;
4444
}

0 commit comments

Comments
 (0)