Skip to content

Commit 80db155

Browse files
committed
C++: Fix example and test.
1 parent ec573b5 commit 80db155

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed

cpp/ql/src/experimental/Security/CWE/CWE-120/MemoryUnsafeFunctionScan.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ int main(int argc, char **argv)
1313
char buf1[10];
1414
scanf("%s", buf1);
1515

16-
// GOOD, length is specified. The length should be one less than the size of the buffer, since the last character is the NULL terminator.
17-
char buf2[10];
18-
sscanf(buf2, "%9s");
16+
// GOOD, length is specified. The length should be one less than the size of the destination buffer, since the last character is the NULL terminator.
17+
char buf2[20];
18+
char buf3[10];
19+
sscanf(buf2, "%9s", buf3);
1920

2021
// BAD, do not use scanf without specifying a length first
2122
char file[10];

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ int main(int argc, char **argv)
1313
char buf1[10];
1414
scanf("%s", buf1);
1515

16-
// GOOD, length is specified
17-
char buf2[10];
18-
sscanf(buf2, "%9s");
16+
// GOOD, length is specified. The length should be one less than the size of the destination buffer, since the last character is the NULL terminator.
17+
char buf2[20];
18+
char buf3[10];
19+
sscanf(buf2, "%9s", buf3);
1920

2021
// BAD, do not use scanf without specifying a length first
2122
char file[10];
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
| MemoryUnsafeFunctionScan.cpp:14:5:14:9 | call to scanf | Dangerous use of one of the scanf functions |
2-
| MemoryUnsafeFunctionScan.cpp:22:5:22:10 | call to fscanf | Dangerous use of one of the scanf functions |
2+
| MemoryUnsafeFunctionScan.cpp:23:5:23:10 | call to fscanf | Dangerous use of one of the scanf functions |

0 commit comments

Comments
 (0)