Skip to content

Commit fd7dec7

Browse files
authored
Merge pull request #4824 from geoffw0/modelchanges5
C++: Add cases in the Allocation model.
2 parents 39acc9a + c89f7d8 commit fd7dec7

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed

cpp/ql/src/semmle/code/cpp/models/implementations/Allocation.qll

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ private class AllocaAllocationFunction extends AllocationFunction {
8282
hasGlobalName([
8383
// --- stack allocation
8484
"alloca", // // alloca(size)
85-
"__builtin_alloca" // __builtin_alloca(size)
85+
"__builtin_alloca", // __builtin_alloca(size)
86+
"_alloca", // _alloca(size)
87+
"_malloca" // _malloca(size)
8688
]) and
8789
sizeArg = 0
8890
}

cpp/ql/src/semmle/code/cpp/models/implementations/Strdup.qll

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import semmle.code.cpp.models.interfaces.Taint
1414
private class StrdupFunction extends AllocationFunction, ArrayFunction, DataFlowFunction {
1515
StrdupFunction() {
1616
hasGlobalName([
17+
// --- C library allocation
1718
"strdup", // strdup(str)
1819
"wcsdup", // wcsdup(str)
1920
"_strdup", // _strdup(str)
@@ -39,8 +40,8 @@ private class StrndupFunction extends AllocationFunction, ArrayFunction, DataFlo
3940
StrndupFunction() {
4041
exists(string name |
4142
hasGlobalName(name) and
42-
// strndup(str, maxlen)
43-
name = "strndup"
43+
// --- C library allocation
44+
name = "strndup" // strndup(str, maxlen)
4445
)
4546
}
4647

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)