Skip to content

Commit af180d4

Browse files
author
Robert Marsh
authored
Merge pull request #4805 from geoffw0/sscanf
C++: Refine examples and tests for cpp/memory-unsafe-function-scan (experimental) query
2 parents e5ef0e3 + 209191b commit af180d4

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
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: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
///// Library routines /////
22

3+
typedef unsigned long size_t;
4+
void *malloc(size_t size);
5+
6+
size_t strlen(const char *s);
7+
38
int scanf(const char *format, ...);
49
int sscanf(const char *str, const char *format, ...);
510
int fscanf(const char *str, const char *format, ...);
@@ -13,13 +18,23 @@ int main(int argc, char **argv)
1318
char buf1[10];
1419
scanf("%s", buf1);
1520

16-
// GOOD, length is specified
17-
char buf2[10];
18-
sscanf(buf2, "%9s");
21+
// 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.
22+
char buf2[20];
23+
char buf3[10];
24+
sscanf(buf2, "%9s", buf3);
1925

2026
// BAD, do not use scanf without specifying a length first
2127
char file[10];
2228
fscanf(file, "%s", buf2);
2329

30+
// GOOD, with 'sscanf' the input can be checked first and enough room allocated [FALSE POSITIVE]
31+
if (argc >= 1)
32+
{
33+
char *src = argv[0];
34+
char *dest = (char *)malloc(strlen(src) + 1);
35+
36+
sscanf(src, "%s", dest);
37+
}
38+
2439
return 0;
2540
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1-
| 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 |
1+
| MemoryUnsafeFunctionScan.cpp:19:5:19:9 | call to scanf | Dangerous use of one of the scanf functions |
2+
| MemoryUnsafeFunctionScan.cpp:28:5:28:10 | call to fscanf | Dangerous use of one of the scanf functions |
3+
| MemoryUnsafeFunctionScan.cpp:36:3:36:8 | call to sscanf | Dangerous use of one of the scanf functions |

0 commit comments

Comments
 (0)