Skip to content

Commit fb1b293

Browse files
committed
C++: Add scanf_s models.
1 parent dba5c85 commit fb1b293

5 files changed

Lines changed: 24 additions & 10 deletions

File tree

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class Scanf extends ScanfFunction instanceof TopLevelFunction {
3434
Scanf() {
3535
this.hasGlobalOrStdOrBslName("scanf") or // scanf(format, args...)
3636
this.hasGlobalOrStdOrBslName("wscanf") or // wscanf(format, args...)
37+
this.hasGlobalOrStdOrBslName("scanf_s") or // scanf_s(format, args...)
3738
this.hasGlobalName("_scanf_l") or // _scanf_l(format, locale, args...)
3839
this.hasGlobalName("_wscanf_l")
3940
}
@@ -50,6 +51,7 @@ class Fscanf extends ScanfFunction instanceof TopLevelFunction {
5051
Fscanf() {
5152
this.hasGlobalOrStdOrBslName("fscanf") or // fscanf(src_stream, format, args...)
5253
this.hasGlobalOrStdOrBslName("fwscanf") or // fwscanf(src_stream, format, args...)
54+
this.hasGlobalOrStdOrBslName("fscanf_s") or // fscanf_s(src_stream, format, args...)
5355
this.hasGlobalName("_fscanf_l") or // _fscanf_l(src_stream, format, locale, args...)
5456
this.hasGlobalName("_fwscanf_l")
5557
}
@@ -66,8 +68,12 @@ class Sscanf extends ScanfFunction instanceof TopLevelFunction {
6668
Sscanf() {
6769
this.hasGlobalOrStdOrBslName("sscanf") or // sscanf(src_stream, format, args...)
6870
this.hasGlobalOrStdOrBslName("swscanf") or // swscanf(src, format, args...)
71+
this.hasGlobalOrStdOrBslName("sscanf_s") or // sscanf_s(src, format, args...)
72+
this.hasGlobalOrStdOrBslName("swscanf_s") or // swscanf_s(src, format, args...)
6973
this.hasGlobalName("_sscanf_l") or // _sscanf_l(src, format, locale, args...)
70-
this.hasGlobalName("_swscanf_l")
74+
this.hasGlobalName("_swscanf_l") or // _swscanf_l(src, format, locale, args...)
75+
this.hasGlobalName("_sscanf_s_l") or // _sscanf_s_l(src, format, locale, args...)
76+
this.hasGlobalName("_swscanf_s_l") // _swscanf_s_l(src, format, locale, args...)
7177
}
7278

7379
override int getInputParameterIndex() { result = 0 }

cpp/ql/test/library-tests/dataflow/source-sink-tests/sources-and-sinks.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -140,32 +140,32 @@ void test_scanf_s(FILE *stream) {
140140
int n1, n2;
141141
scanf_s(
142142
"%d",
143-
&n1, // $ MISSING: local_source
144-
&n2); // $ MISSING: local_source
143+
&n1, // $ local_source
144+
&n2); // $ local_source
145145
}
146146

147147
{
148148
int n;
149-
fscanf_s(stream, "%d", &n); // $ MISSING: remote_source
149+
fscanf_s(stream, "%d", &n); // $ remote_source
150150
}
151151

152152
{
153153
int n1, n2;
154154
char buf[256];
155155
scanf_s("%d %s",
156-
&n1, // $ MISSING: local_source
157-
buf, // $ MISSING: local_source
156+
&n1, // $ local_source
157+
buf, // $ local_source
158158
256,
159-
&n2); // $ MISSING: local_source
159+
&n2); // $ local_source
160160
}
161161

162162
{
163163
int n1, n2;
164164
char buf[256];
165165
fscanf_s(stream, "%d %s",
166-
&n1, // $ MISSING: remote_source
167-
buf, // $ MISSING: remote_source
166+
&n1, // $ remote_source
167+
buf, // $ remote_source
168168
256,
169-
&n2); // $ MISSING: remote_source
169+
&n2); // $ remote_source
170170
}
171171
}

cpp/ql/test/library-tests/scanf/scanfFormatLiteral.expected

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@
33
| test.c:20:2:20:7 | call to fscanf | 1 | i | 0 | 0 |
44
| test.c:21:2:21:7 | call to sscanf | 0 | s | 0 | 0 |
55
| test.c:22:2:22:8 | call to swscanf | 0 | s | 10 | 10 |
6+
| test.c:23:2:23:8 | call to scanf_s | 0 | d | 0 | 0 |
7+
| test.c:23:2:23:8 | call to scanf_s | 1 | s | 0 | 0 |
8+
| test.c:23:2:23:8 | call to scanf_s | 2 | d | 0 | 0 |

cpp/ql/test/library-tests/scanf/scanfFunctionCall.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
| test.c:20:2:20:7 | call to fscanf | 0 | 1 | test.c:20:15:20:23 | %10s %i | non-wide |
44
| test.c:21:2:21:7 | call to sscanf | 0 | 1 | test.c:21:19:21:28 | %*i%s%*s | non-wide |
55
| test.c:22:2:22:8 | call to swscanf | 0 | 1 | test.c:22:21:22:26 | %10s | wide |
6+
| test.c:23:2:23:8 | call to scanf_s | 0 | 0 | test.c:23:10:23:19 | %d %s %d | non-wide |

cpp/ql/test/library-tests/scanf/scanfFunctionCallOutput.expected

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,7 @@
44
| test.c:20:2:20:7 | call to fscanf | test.c:20:34:20:34 | i | 1 |
55
| test.c:21:2:21:7 | call to sscanf | test.c:21:31:21:36 | buffer | 0 |
66
| test.c:22:2:22:8 | call to swscanf | test.c:22:29:22:35 | wbuffer | 0 |
7+
| test.c:23:2:23:8 | call to scanf_s | test.c:23:22:23:23 | & ... | 0 |
8+
| test.c:23:2:23:8 | call to scanf_s | test.c:23:26:23:31 | buffer | 1 |
9+
| test.c:23:2:23:8 | call to scanf_s | test.c:23:34:23:35 | 10 | 2 |
10+
| test.c:23:2:23:8 | call to scanf_s | test.c:23:38:23:40 | & ... | 3 |

0 commit comments

Comments
 (0)