Skip to content

Commit a53b161

Browse files
committed
C++: Move some variant tests to a case we definitely do want to flag the base case of.
1 parent c3cd135 commit a53b161

File tree

2 files changed

+39
-46
lines changed

2 files changed

+39
-46
lines changed

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
| 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 |
66
| 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 |
77
| test2.cpp:229:3:229:7 | call to chmod | The $@ being operated upon was previously $@, but the underlying file may have been changed since then. | test2.cpp:229:9:229:12 | path | filename | test2.cpp:222:6:222:10 | call to fopen | checked |
8-
| test2.cpp:269:3:269:8 | call to remove | The $@ being operated upon was previously $@, but the underlying file may have been changed since then. | test2.cpp:269:10:269:14 | path1 | filename | test2.cpp:267:7:267:12 | call to rename | checked |
9-
| test2.cpp:279:3:279:8 | call to remove | The $@ being operated upon was previously $@, but the underlying file may have been changed since then. | test2.cpp:279:10:279:14 | path1 | filename | test2.cpp:275:6:275:11 | call to rename | checked |
10-
| test2.cpp:311:7:311:11 | call to fopen | The $@ being operated upon was previously $@, but the underlying file may have been changed since then. | test2.cpp:311:13:311:16 | path | filename | test2.cpp:309:6:309:11 | call to access | checked |
8+
| test2.cpp:239:3:239:8 | call to remove | The $@ being operated upon was previously $@, but the underlying file may have been changed since then. | test2.cpp:239:10:239:14 | path1 | filename | test2.cpp:237:7:237:12 | call to rename | checked |
9+
| test2.cpp:261:7:261:11 | call to fopen | The $@ being operated upon was previously $@, but the underlying file may have been changed since then. | test2.cpp:261:13:261:16 | path | filename | test2.cpp:259:6:259:11 | call to access | checked |
10+
| test2.cpp:287:7:287:11 | call to fopen | The $@ being operated upon was previously $@, but the underlying file may have been changed since then. | test2.cpp:287:13:287:16 | path | filename | test2.cpp:285:7:285:12 | call to access | checked |
11+
| test2.cpp:301:7:301:11 | call to fopen | The $@ being operated upon was previously $@, but the underlying file may have been changed since then. | test2.cpp:301:13:301:16 | path | filename | test2.cpp:297:6:297:11 | call to access | checked |
1112
| 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 |
1213
| 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 |
1314
| 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 |

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

Lines changed: 35 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -230,96 +230,88 @@ void test3_1(const char *path)
230230
}
231231
}
232232

233-
void test3_2(const char *path)
234-
{
235-
FILE *f = NULL;
233+
// --- rename -> remove / open ---
236234

237-
f = fopen(path, "w");
238-
if (f)
235+
void test4_1(const char *path1, const char *path2)
236+
{
237+
if (!rename(path1, path2))
239238
{
240-
// ...
241-
242-
fclose(f);
239+
remove(path1); // BAD???
243240
}
244-
245-
chmod(path, 0); // GOOD (doesn't depend on the fopen)
246241
}
247242

248-
void test3_3(const char *path1, const char *path2)
243+
void test4_4(const char *path1, const char *path2)
249244
{
250245
FILE *f = NULL;
251246

252-
f = fopen(path1, "w");
253-
if (f)
247+
if (rename(path1, path2))
254248
{
255-
// ...
256-
257-
fclose(f);
258-
259-
chmod(path2, 0); // GOOD (different file)
249+
f = fopen(path2, "r"); // BAD??? [NOT DETECTED]
260250
}
261251
}
262252

263-
// --- rename -> remove / open ---
253+
// --- access -> open ---
264254

265-
void test4_1(const char *path1, const char *path2)
255+
void test5_1(const char *path)
266256
{
267-
if (!rename(path1, path2))
268-
{
269-
remove(path1); // BAD???
270-
}
271-
}
257+
FILE *f = NULL;
272258

273-
void test4_2(const char *path1, const char *path2)
274-
{
275-
if (rename(path1, path2))
259+
if (access(path))
276260
{
261+
f = fopen(path, "r"); // BAD
262+
277263
// ...
278-
} else {
279-
remove(path1); // BAD???
280264
}
281265
}
282266

283-
void test4_3(const char *path1, const char *path2)
267+
void test5_2(const char *path)
284268
{
285-
if (rename(path1, path2))
269+
FILE *f = NULL;
270+
271+
if (access(path))
286272
{
287273
// ...
288274
}
289275

290-
remove(path1); // GOOD (does not depend on the rename)
276+
f = fopen(path, "r"); // GOOD (doesn't depend on the access check)
277+
278+
// ...
291279
}
292280

293-
void test4_4(const char *path1, const char *path2)
281+
void test5_3(const char *path)
294282
{
295283
FILE *f = NULL;
296284

297-
if (rename(path1, path2))
285+
if (!access(path))
298286
{
299-
f = fopen(path2, "r"); // BAD??? [NOT DETECTED]
287+
f = fopen(path, "r"); // BAD
288+
289+
// ...
300290
}
301291
}
302292

303-
// --- access -> open ---
304-
305-
void test5_1(const char *path)
293+
void test5_4(const char *path)
306294
{
307295
FILE *f = NULL;
308296

309297
if (access(path))
310298
{
299+
// ...
300+
} else {
311301
f = fopen(path, "r"); // BAD
302+
303+
// ...
312304
}
313305
}
314306

315-
void test5_2(const char *path)
307+
void test5_5(const char *path1, const char *path2)
316308
{
317309
FILE *f = NULL;
318310

319-
if (access(path))
311+
if (access(path1))
320312
{
313+
f = fopen(path2, "r"); // GOOD (different file)
314+
321315
// ...
322316
}
323-
324-
f = fopen(path, "r"); // GOOD
325317
}

0 commit comments

Comments
 (0)