Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 80eaf0b

Browse files
authored
Merge pull request #5174 from criemen/bsl-str
Model bsl functions in Str*.qll
2 parents f087ff3 + 30659f3 commit 80eaf0b

5 files changed

Lines changed: 30 additions & 38 deletions

File tree

cpp/ql/src/semmle/code/cpp/commons/Scanf.qll

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@ class Scanf extends ScanfFunction {
3434
Scanf() {
3535
this instanceof TopLevelFunction and
3636
(
37-
hasName("scanf") or // scanf(format, args...)
38-
hasName("wscanf") or // wscanf(format, args...)
39-
hasName("_scanf_l") or // _scanf_l(format, locale, args...)
40-
hasName("_wscanf_l") // _wscanf_l(format, locale, args...)
37+
hasGlobalOrStdOrBslName("scanf") or // scanf(format, args...)
38+
hasGlobalOrStdOrBslName("wscanf") or // wscanf(format, args...)
39+
hasGlobalName("_scanf_l") or // _scanf_l(format, locale, args...)
40+
hasGlobalName("_wscanf_l") // _wscanf_l(format, locale, args...)
4141
)
4242
}
4343

@@ -53,10 +53,10 @@ class Fscanf extends ScanfFunction {
5353
Fscanf() {
5454
this instanceof TopLevelFunction and
5555
(
56-
hasName("fscanf") or // fscanf(src_stream, format, args...)
57-
hasName("fwscanf") or // fwscanf(src_stream, format, args...)
58-
hasName("_fscanf_l") or // _fscanf_l(src_stream, format, locale, args...)
59-
hasName("_fwscanf_l") // _fwscanf_l(src_stream, format, locale, args...)
56+
hasGlobalOrStdOrBslName("fscanf") or // fscanf(src_stream, format, args...)
57+
hasGlobalOrStdOrBslName("fwscanf") or // fwscanf(src_stream, format, args...)
58+
hasGlobalName("_fscanf_l") or // _fscanf_l(src_stream, format, locale, args...)
59+
hasGlobalName("_fwscanf_l") // _fwscanf_l(src_stream, format, locale, args...)
6060
)
6161
}
6262

@@ -72,10 +72,10 @@ class Sscanf extends ScanfFunction {
7272
Sscanf() {
7373
this instanceof TopLevelFunction and
7474
(
75-
hasName("sscanf") or // sscanf(src_stream, format, args...)
76-
hasName("swscanf") or // swscanf(src, format, args...)
77-
hasName("_sscanf_l") or // _sscanf_l(src, format, locale, args...)
78-
hasName("_swscanf_l") // _swscanf_l(src, format, locale, args...)
75+
hasGlobalOrStdOrBslName("sscanf") or // sscanf(src_stream, format, args...)
76+
hasGlobalOrStdOrBslName("swscanf") or // swscanf(src, format, args...)
77+
hasGlobalName("_sscanf_l") or // _sscanf_l(src, format, locale, args...)
78+
hasGlobalName("_swscanf_l") // _swscanf_l(src, format, locale, args...)
7979
)
8080
}
8181

@@ -91,10 +91,10 @@ class Snscanf extends ScanfFunction {
9191
Snscanf() {
9292
this instanceof TopLevelFunction and
9393
(
94-
hasName("_snscanf") or // _snscanf(src, max_amount, format, args...)
95-
hasName("_snwscanf") or // _snwscanf(src, max_amount, format, args...)
96-
hasName("_snscanf_l") or // _snscanf_l(src, max_amount, format, locale, args...)
97-
hasName("_snwscanf_l") // _snwscanf_l(src, max_amount, format, locale, args...)
94+
hasGlobalName("_snscanf") or // _snscanf(src, max_amount, format, args...)
95+
hasGlobalName("_snwscanf") or // _snwscanf(src, max_amount, format, args...)
96+
hasGlobalName("_snscanf_l") or // _snscanf_l(src, max_amount, format, locale, args...)
97+
hasGlobalName("_snwscanf_l") // _snwscanf_l(src, max_amount, format, locale, args...)
9898
// note that the max_amount is not a limit on the output length, it's an input length
9999
// limit used with non null-terminated strings.
100100
)

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

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,15 @@ private class Printf extends FormattingFunction, AliasFunction {
1515
Printf() {
1616
this instanceof TopLevelFunction and
1717
(
18-
hasGlobalOrStdName(["printf", "wprintf"]) or
18+
hasGlobalOrStdOrBslName(["printf", "wprintf"]) or
1919
hasGlobalName(["printf_s", "wprintf_s", "g_printf"])
2020
) and
2121
not exists(getDefinition().getFile().getRelativePath())
2222
}
2323

2424
override int getFormatParameterIndex() { result = 0 }
2525

26-
deprecated override predicate isWideCharDefault() {
27-
hasGlobalOrStdName("wprintf") or
28-
hasGlobalName("wprintf_s")
29-
}
26+
deprecated override predicate isWideCharDefault() { hasName(["wprintf", "wprintf_s"]) }
3027

3128
override predicate isOutputGlobal() { any() }
3229

@@ -44,15 +41,15 @@ private class Fprintf extends FormattingFunction {
4441
Fprintf() {
4542
this instanceof TopLevelFunction and
4643
(
47-
hasGlobalOrStdName(["fprintf", "fwprintf"]) or
44+
hasGlobalOrStdOrBslName(["fprintf", "fwprintf"]) or
4845
hasGlobalName("g_fprintf")
4946
) and
5047
not exists(getDefinition().getFile().getRelativePath())
5148
}
5249

5350
override int getFormatParameterIndex() { result = 1 }
5451

55-
deprecated override predicate isWideCharDefault() { hasGlobalOrStdName("fwprintf") }
52+
deprecated override predicate isWideCharDefault() { hasName("fwprintf") }
5653

5754
override int getOutputParameterIndex(boolean isStream) { result = 0 and isStream = true }
5855
}
@@ -64,7 +61,7 @@ private class Sprintf extends FormattingFunction {
6461
Sprintf() {
6562
this instanceof TopLevelFunction and
6663
(
67-
hasGlobalOrStdName([
64+
hasGlobalOrStdOrBslName([
6865
"sprintf", // sprintf(dst, format, args...)
6966
"wsprintf" // wsprintf(dst, format, args...)
7067
])
@@ -90,22 +87,20 @@ private class Sprintf extends FormattingFunction {
9087
}
9188

9289
override int getFormatParameterIndex() {
93-
hasGlobalName("g_strdup_printf") and result = 0
90+
hasName("g_strdup_printf") and result = 0
9491
or
95-
hasGlobalName("__builtin___sprintf_chk") and result = 3
92+
hasName("__builtin___sprintf_chk") and result = 3
9693
or
9794
not getName() = ["g_strdup_printf", "__builtin___sprintf_chk"] and
9895
result = 1
9996
}
10097

10198
override int getOutputParameterIndex(boolean isStream) {
102-
not hasGlobalName("g_strdup_printf") and result = 0 and isStream = false
99+
not hasName("g_strdup_printf") and result = 0 and isStream = false
103100
}
104101

105102
override int getFirstFormatArgumentIndex() {
106-
if hasGlobalName("__builtin___sprintf_chk")
107-
then result = 4
108-
else result = getNumberOfParameters()
103+
if hasName("__builtin___sprintf_chk") then result = 4 else result = getNumberOfParameters()
109104
}
110105
}
111106

@@ -116,7 +111,7 @@ private class SnprintfImpl extends Snprintf {
116111
SnprintfImpl() {
117112
this instanceof TopLevelFunction and
118113
(
119-
hasGlobalOrStdName([
114+
hasGlobalOrStdOrBslName([
120115
"snprintf", // C99 defines snprintf
121116
"swprintf" // The s version of wide-char printf is also always the n version
122117
])
@@ -163,10 +158,7 @@ private class SnprintfImpl extends Snprintf {
163158
}
164159

165160
override predicate returnsFullFormatLength() {
166-
(
167-
hasGlobalOrStdName("snprintf") or
168-
hasGlobalName(["g_snprintf", "__builtin___snprintf_chk", "snprintf_s"])
169-
) and
161+
hasName(["snprintf", "g_snprintf", "__builtin___snprintf_chk", "snprintf_s"]) and
170162
not exists(getDefinition().getFile().getRelativePath())
171163
}
172164

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import semmle.code.cpp.models.interfaces.SideEffect
1313
*/
1414
class StrcatFunction extends TaintFunction, DataFlowFunction, ArrayFunction, SideEffectFunction {
1515
StrcatFunction() {
16-
this.hasGlobalOrStdName([
16+
this.hasGlobalOrStdOrBslName([
1717
"strcat", // strcat(dst, src)
1818
"strncat", // strncat(dst, src, max_amount)
1919
"wcscat", // wcscat(dst, src)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import semmle.code.cpp.models.interfaces.SideEffect
1313
*/
1414
class StrcpyFunction extends ArrayFunction, DataFlowFunction, TaintFunction, SideEffectFunction {
1515
StrcpyFunction() {
16-
this.hasGlobalOrStdName([
16+
this.hasGlobalOrStdOrBslName([
1717
"strcpy", // strcpy(dst, src)
1818
"wcscpy", // wcscpy(dst, src)
1919
"strncpy", // strncpy(dst, src, max_amount)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import semmle.code.cpp.models.interfaces.Taint
1515
*/
1616
private class Strtok extends ArrayFunction, AliasFunction, TaintFunction, SideEffectFunction {
1717
Strtok() {
18-
this.hasGlobalOrStdName("strtok") or
18+
this.hasGlobalOrStdOrBslName("strtok") or
1919
this.hasGlobalName(["strtok_r", "_strtok_l", "wcstok", "_wcstok_l", "_mbstok", "_mbstok_l"])
2020
}
2121

0 commit comments

Comments
 (0)