Skip to content

Commit 6499197

Browse files
committed
C++: Add a test of TOCTOUFilesystemRace.ql.
1 parent b2f1008 commit 6499197

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
| test.cpp:21:3:21:8 | call to remove | The $@ being operated upon was previously $@, but the underlying file may have been changed since then. | test.cpp:21:10:21:14 | file1 | filename | test.cpp:19:7:19:12 | call to rename | checked |
2+
| test.cpp:35:3:35:8 | call to remove | The $@ being operated upon was previously $@, but the underlying file may have been changed since then. | test.cpp:35:10:35:14 | file1 | filename | test.cpp:32:7:32:12 | call to rename | checked |
3+
| test.cpp:49:3:49:8 | call to remove | The $@ being operated upon was previously $@, but the underlying file may have been changed since then. | test.cpp:49:10:49:14 | file1 | filename | test.cpp:47:7:47:12 | call to rename | checked |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Security/CWE/CWE-367/TOCTOUFilesystemRace.ql
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
class String
3+
{
4+
public:
5+
String(const char *_s);
6+
void set(const char *_s);
7+
};
8+
9+
void create(const String &filename);
10+
bool rename(const String &from, const String &to);
11+
void remove(const String &filename);
12+
13+
void test1()
14+
{
15+
String file1 = "a.txt";
16+
String file2 = "b.txt";
17+
18+
create(file1);
19+
if (!rename(file1, file2))
20+
{
21+
remove(file1); // BAD
22+
}
23+
}
24+
25+
26+
void test2()
27+
{
28+
String file1 = "a.txt";
29+
String file2 = "b.txt";
30+
31+
create(file1);
32+
if (!rename(file1, file2))
33+
{
34+
file1.set("d.txt");
35+
remove(file1); // GOOD [FALSE POSITIVE]
36+
}
37+
}
38+
39+
40+
void test3()
41+
{
42+
String file1 = "a.txt";
43+
String file2 = "b.txt";
44+
file1.set("d.txt");
45+
46+
create(file1);
47+
if (!rename(file1, file2))
48+
{
49+
remove(file1); // BAD
50+
}
51+
}

0 commit comments

Comments
 (0)