Skip to content

Commit 1fbb0fb

Browse files
authored
Merge pull request #4266 from geoffw0/cwe190tests
C++: CWE-190 Tests.
2 parents d095d6b + 6ca9c44 commit 1fbb0fb

File tree

4 files changed

+90
-0
lines changed

4 files changed

+90
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
| test2.cpp:14:11:14:11 | v | $@ flows to here and is used in arithmetic, potentially causing an overflow. | test2.cpp:25:22:25:23 | & ... | User-provided value |
2+
| test2.cpp:14:11:14:11 | v | $@ flows to here and is used in arithmetic, potentially causing an underflow. | test2.cpp:25:22:25:23 | & ... | User-provided value |
13
| test3.c:15:10:15:10 | x | $@ flows to here and is used in arithmetic, potentially causing an overflow. | test3.c:11:15:11:18 | argv | User-provided value |
24
| test3.c:15:14:15:14 | y | $@ flows to here and is used in arithmetic, potentially causing an overflow. | test3.c:11:15:11:18 | argv | User-provided value |
35
| test3.c:15:18:15:18 | z | $@ flows to here and is used in arithmetic, potentially causing an overflow. | test3.c:11:15:11:18 | argv | User-provided value |
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1+
| test2.cpp:14:11:14:15 | ... * ... | $@ flows to here and is used in an expression which might overflow. | test2.cpp:25:22:25:23 | & ... | User-provided value |
2+
| test2.cpp:16:11:16:21 | ... * ... | $@ flows to here and is used in an expression which might overflow. | test2.cpp:25:22:25:23 | & ... | User-provided value |
3+
| test2.cpp:17:11:17:22 | ... * ... | $@ flows to here and is used in an expression which might overflow. | test2.cpp:25:22:25:23 | & ... | User-provided value |
14
| test3.c:12:31:12:34 | * ... | $@ flows to here and is used in an expression which might overflow negatively. | test3.c:11:15:11:18 | argv | User-provided value |
25
| test3.c:13:16:13:19 | * ... | $@ flows to here and is used in an expression which might overflow negatively. | test3.c:11:15:11:18 | argv | User-provided value |
36
| test4.cpp:13:17:13:20 | access to array | $@ flows to here and is used in an expression which might overflow negatively. | test4.cpp:9:13:9:16 | argv | User-provided value |
47
| 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 |
58
| 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 |
69
| 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 |
10+
| 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 |
11+
| 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 |
12+
| 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 |
713
| 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 |
814
| 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 |
915
| 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: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
typedef signed long long int s64;
3+
4+
typedef struct {} FILE;
5+
int fscanf(FILE *stream, const char *format, ...);
6+
FILE *stdin;
7+
8+
typedef struct _myStruct {
9+
s64 val;
10+
} MyStruct;
11+
12+
void test2_sink(s64 v, MyStruct s, MyStruct &s_r, MyStruct *s_p)
13+
{
14+
s64 v1 = v * 2; // bad
15+
s64 v2 = s.val * 2; // bad [NOT DETECTED]
16+
s64 v3 = s_r.val * 2; // bad
17+
s64 v4 = s_p->val * 2; // bad
18+
}
19+
20+
void test2_source()
21+
{
22+
MyStruct ms;
23+
s64 v;
24+
25+
fscanf(stdin, "%i", &v);
26+
ms.val = v;
27+
test2_sink(v, ms, ms, &ms);
28+
}
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)