Skip to content

Commit 899d1ab

Browse files
committed
C++: Add tests of strdup (and variants) as allocators.
1 parent 0420ac7 commit 899d1ab

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

cpp/ql/test/query-tests/Critical/NewFree/NewFreeMismatch.expected

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,6 @@
1818
| test.cpp:235:2:235:5 | call to free | There is a new/free mismatch between this free and the corresponding $@. | test.cpp:227:7:227:13 | new | new |
1919
| test.cpp:239:2:239:5 | call to free | There is a new/free mismatch between this free and the corresponding $@. | test.cpp:228:7:228:17 | new[] | new[] |
2020
| test.cpp:272:3:272:6 | call to free | There is a new/free mismatch between this free and the corresponding $@. | test.cpp:265:7:265:13 | new | new |
21+
| test.cpp:441:2:441:10 | delete | There is a malloc/delete mismatch between this delete and the corresponding $@. | test.cpp:434:13:434:18 | call to strdup | malloc |
22+
| test.cpp:443:2:443:10 | delete | There is a malloc/delete mismatch between this delete and the corresponding $@. | test.cpp:436:13:436:19 | call to strndup | malloc |
23+
| test.cpp:445:2:445:10 | delete | There is a malloc/delete mismatch between this delete and the corresponding $@. | test.cpp:438:16:438:21 | call to wcsdup | malloc |

cpp/ql/test/query-tests/Critical/NewFree/test.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,3 +424,24 @@ void test13()
424424

425425
delete myPointer3.getPointer(); // GOOD
426426
}
427+
428+
char *strdup(const char *s1);
429+
char *strndup(const char *s1, size_t n);
430+
wchar_t* wcsdup(const wchar_t* s1);
431+
432+
void test14()
433+
{
434+
char *s1 = strdup("string");
435+
char *s2 = strdup("string");
436+
char *s3 = strndup("string", 3);
437+
char *s4 = strndup("string", 3);
438+
wchar_t *s5 = wcsdup(L"string");
439+
wchar_t *s6 = wcsdup(L"string");
440+
441+
delete s1; // BAD: strdup -> delete
442+
free(s2); // GOOD
443+
delete s3; // BAD: strndup -> delete
444+
free(s4); // GOOD
445+
delete s5; // BAD: wcsdup -> delete
446+
free(s6); // GOOD
447+
}

0 commit comments

Comments
 (0)