Skip to content

Commit 22097a9

Browse files
committed
C++: Add some CWE-190 tests I had lying around.
1 parent d21c101 commit 22097a9

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

cpp/ql/test/query-tests/Security/CWE/CWE-190/semmle/tainted/IntegerOverflowTainted.expected

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
| test5.cpp:10:9:10:15 | call to strtoul | $@ flows to here and is used in an expression which might overflow. | test5.cpp:9:7:9:9 | buf | User-provided value |
55
| test5.cpp:17:6:17:27 | ... * ... | $@ flows to here and is used in an expression which might overflow. | test5.cpp:9:7:9:9 | buf | User-provided value |
66
| test5.cpp:19:6:19:13 | ... * ... | $@ flows to here and is used in an expression which might overflow. | test5.cpp:9:7:9:9 | buf | User-provided value |
7+
| test6.cpp:11:15:11:15 | s | $@ flows to here and is used in an expression which might overflow. | test6.cpp:39:23:39:24 | & ... | User-provided value |
8+
| test6.cpp:16:15:16:15 | s | $@ flows to here and is used in an expression which might overflow. | test6.cpp:39:23:39:24 | & ... | User-provided value |
9+
| test6.cpp:30:16:30:16 | s | $@ flows to here and is used in an expression which might overflow. | test6.cpp:39:23:39:24 | & ... | User-provided value |
710
| test.c:14:15:14:35 | ... * ... | $@ flows to here and is used in an expression which might overflow. | test.c:11:29:11:32 | argv | User-provided value |
811
| test.c:44:7:44:12 | ... -- | $@ flows to here and is used in an expression which might overflow negatively. | test.c:41:17:41:20 | argv | User-provided value |
912
| test.c:54:7:54:12 | ... -- | $@ flows to here and is used in an expression which might overflow negatively. | test.c:51:17:51:20 | argv | User-provided value |
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
typedef unsigned short u16;
3+
typedef unsigned int u32;
4+
5+
typedef struct {} FILE;
6+
int fscanf(FILE *stream, const char *format, ...);
7+
FILE *stdin;
8+
9+
void docast1(u32 s)
10+
{
11+
u16 c = (u16)s; // bad
12+
}
13+
14+
void docast2(u32 s)
15+
{
16+
u16 c = (u16)s; // bad
17+
}
18+
19+
class MyBaseClass
20+
{
21+
public:
22+
virtual void docast(u32 s) = 0;
23+
};
24+
25+
class MyDerivedClass : public MyBaseClass
26+
{
27+
public:
28+
void docast(u32 s)
29+
{
30+
u16 c = (u16)s; // bad
31+
}
32+
};
33+
34+
void test6()
35+
{
36+
u32 s;
37+
38+
s = -1;
39+
fscanf(stdin, "%hd", &s);
40+
41+
docast1(s);
42+
{
43+
void (*docast2_ptr)(u32) = &docast2;
44+
45+
docast2_ptr(s);
46+
}
47+
{
48+
MyBaseClass *mbc = new MyDerivedClass;
49+
50+
mbc->docast(s);
51+
52+
delete mbc;
53+
}
54+
}

0 commit comments

Comments
 (0)