Skip to content

Commit 2463841

Browse files
authored
Merge pull request github#6503 from andersfugmann/model_strdupa_functions
C++: Model strdupa and strndupa
2 parents 089077e + 0b98b39 commit 2463841

File tree

6 files changed

+403
-327
lines changed

6 files changed

+403
-327
lines changed

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ private class StrdupFunction extends AllocationFunction, ArrayFunction, DataFlow
1616
hasGlobalName([
1717
// --- C library allocation
1818
"strdup", // strdup(str)
19+
"strdupa", // strdupa(str) - returns stack allocated buffer
1920
"wcsdup", // wcsdup(str)
2021
"_strdup", // _strdup(str)
2122
"_wcsdup", // _wcsdup(str)
@@ -31,18 +32,20 @@ private class StrdupFunction extends AllocationFunction, ArrayFunction, DataFlow
3132
input.isParameterDeref(0) and
3233
output.isReturnValueDeref()
3334
}
35+
36+
override predicate requiresDealloc() { not hasGlobalName("strdupa") }
3437
}
3538

3639
/**
3740
* A `strndup` style allocation function.
3841
*/
3942
private class StrndupFunction extends AllocationFunction, ArrayFunction, DataFlowFunction {
4043
StrndupFunction() {
41-
exists(string name |
42-
hasGlobalName(name) and
43-
// --- C library allocation
44-
name = "strndup" // strndup(str, maxlen)
45-
)
44+
hasGlobalName([
45+
// -- C library allocation
46+
"strndup", // strndup(str, maxlen)
47+
"strndupa" // strndupa(str, maxlen) -- returns stack allocated buffer
48+
])
4649
}
4750

4851
override predicate hasArrayInput(int bufParam) { bufParam = 0 }
@@ -56,4 +59,6 @@ private class StrndupFunction extends AllocationFunction, ArrayFunction, DataFlo
5659
) and
5760
output.isReturnValueDeref()
5861
}
62+
63+
override predicate requiresDealloc() { not hasGlobalName("strndupa") }
5964
}

0 commit comments

Comments
 (0)