Skip to content

Commit 1c56573

Browse files
committed
C++: Add tests.
1 parent dd95131 commit 1c56573

File tree

2 files changed

+46
-42
lines changed

2 files changed

+46
-42
lines changed

cpp/ql/test/query-tests/Security/CWE/CWE-367/semmle/TOCTOUFilesystemRace.expected

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
| test2.cpp:130:7:130:11 | call to fopen | The $@ being operated upon was previously $@, but the underlying file may have been changed since then. | test2.cpp:130:13:130:16 | path | filename | test2.cpp:128:21:128:27 | buf_ptr | checked |
66
| test2.cpp:157:7:157:10 | call to open | The $@ being operated upon was previously $@, but the underlying file may have been changed since then. | test2.cpp:157:12:157:15 | path | filename | test2.cpp:155:6:155:9 | call to stat | checked |
77
| test2.cpp:170:7:170:10 | call to open | The $@ being operated upon was previously $@, but the underlying file may have been changed since then. | test2.cpp:170:12:170:15 | path | filename | test2.cpp:168:6:168:10 | call to lstat | checked |
8+
| test2.cpp:209:7:209:10 | call to open | The $@ being operated upon was previously $@, but the underlying file may have been changed since then. | test2.cpp:209:12:209:15 | path | filename | test2.cpp:207:6:207:9 | call to stat | checked |
9+
| test2.cpp:228:8:228:11 | call to open | The $@ being operated upon was previously $@, but the underlying file may have been changed since then. | test2.cpp:228:13:228:16 | path | filename | test2.cpp:224:6:224:9 | call to stat | checked |
10+
| test2.cpp:228:8:228:11 | call to open | The $@ being operated upon was previously $@, but the underlying file may have been changed since then. | test2.cpp:228:13:228:16 | path | filename | test2.cpp:226:7:226:9 | buf | checked |
11+
| test2.cpp:249:6:249:10 | call to fopen | The $@ being operated upon was previously $@, but the underlying file may have been changed since then. | test2.cpp:249:12:249:15 | path | filename | test2.cpp:244:6:244:9 | call to stat | checked |
812
| test2.cpp:297:3:297:7 | call to chmod | The $@ being operated upon was previously $@, but the underlying file may have been changed since then. | test2.cpp:297:9:297:12 | path | filename | test2.cpp:290:6:290:10 | call to fopen | checked |
913
| test2.cpp:329:7:329:11 | call to fopen | The $@ being operated upon was previously $@, but the underlying file may have been changed since then. | test2.cpp:329:13:329:16 | path | filename | test2.cpp:327:6:327:11 | call to access | checked |
1014
| test2.cpp:355:7:355:11 | call to fopen | The $@ being operated upon was previously $@, but the underlying file may have been changed since then. | test2.cpp:355:13:355:16 | path | filename | test2.cpp:353:7:353:12 | call to access | checked |

cpp/ql/test/query-tests/Security/CWE/CWE-367/semmle/test2.cpp

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -199,57 +199,57 @@ void test2_10(int dir, const char *path, int arg)
199199
// ...
200200
}
201201

202+
void test2_11(const char *path, int arg)
203+
{
204+
stat_data buf;
205+
int f;
202206

207+
if (stat(path, &buf))
208+
{
209+
f = open(path, arg); // GOOD (here stat is just a redundant check that the file exists / path is valid, confirmed by the return value of open) [FALSE POSITIVE]
210+
if (f == -1)
211+
{
212+
// handle error
213+
}
203214

215+
// ...
216+
}
217+
}
204218

219+
void test2_12(const char *path, int arg)
220+
{
221+
stat_data buf;
222+
int f;
205223

224+
if (stat(path, &buf))
225+
{
226+
if (buf.foo == 11) // check a property of the file
227+
{
228+
f = open(path, arg); // BAD
229+
if (f == -1)
230+
{
231+
// handle error
232+
}
233+
}
206234

235+
// ...
236+
}
237+
}
207238

239+
void test2_13(const char *path, int arg)
240+
{
241+
stat_data buf;
242+
FILE *f;
208243

244+
if (stat(path, &buf)) // check the file does *not* exist
245+
{
246+
return;
247+
}
209248

249+
f = fopen(path, "wt"); // BAD
210250

211-
212-
213-
214-
215-
216-
217-
218-
219-
220-
221-
222-
223-
224-
225-
226-
227-
228-
229-
230-
231-
232-
233-
234-
235-
236-
237-
238-
239-
240-
241-
242-
243-
244-
245-
246-
247-
248-
249-
250-
251-
252-
251+
// ...
252+
}
253253

254254
// --- open -> stat ---
255255

0 commit comments

Comments
 (0)