Skip to content

Commit 209191b

Browse files
committed
C++: Add another good example.
1 parent 80db155 commit 209191b

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

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

Lines changed: 14 additions & 0 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, ...);
@@ -22,5 +27,14 @@ int main(int argc, char **argv)
2227
char file[10];
2328
fscanf(file, "%s", buf2);
2429

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+
2539
return 0;
2640
}
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:23:5:23: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)