Skip to content

Commit 9dbf5bb

Browse files
committed
C++: Add more scanf testing.
1 parent a84332a commit 9dbf5bb

6 files changed

Lines changed: 61 additions & 10 deletions

File tree

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

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,41 @@ void test_strsafe_gets() {
131131
StringCchGetsExA(dest, sizeof(dest), &end, &remaining, 0); // $ local_source
132132
}
133133
}
134+
135+
int scanf_s(const char *format, ...);
136+
int fscanf_s(FILE *stream, const char *format, ...);
137+
138+
void test_scanf_s(FILE *stream) {
139+
{
140+
int n1, n2;
141+
scanf_s(
142+
"%d",
143+
&n1, // $ MISSING: local_source
144+
&n2); // $ MISSING: local_source
145+
}
146+
147+
{
148+
int n;
149+
fscanf_s(stream, "%d", &n); // $ MISSING: remote_source
150+
}
151+
152+
{
153+
int n1, n2;
154+
char buf[256];
155+
scanf_s("%d %s",
156+
&n1, // $ MISSING: local_source
157+
buf, // $ MISSING: local_source
158+
256,
159+
&n2); // $ MISSING: local_source
160+
}
161+
162+
{
163+
int n1, n2;
164+
char buf[256];
165+
fscanf_s(stream, "%d %s",
166+
&n1, // $ MISSING: remote_source
167+
buf, // $ MISSING: remote_source
168+
256,
169+
&n2); // $ MISSING: remote_source
170+
}
171+
}
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
| test.c:18:2:18:6 | call to scanf | 0 | s | 0 | 0 |
2-
| test.c:19:2:19:7 | call to fscanf | 0 | s | 10 | 10 |
3-
| test.c:19:2:19:7 | call to fscanf | 1 | i | 0 | 0 |
4-
| test.c:20:2:20:7 | call to sscanf | 0 | s | 0 | 0 |
5-
| test.c:21:2:21:8 | call to swscanf | 0 | s | 10 | 10 |
1+
| test.c:19:2:19:6 | call to scanf | 0 | s | 0 | 0 |
2+
| test.c:20:2:20:7 | call to fscanf | 0 | s | 10 | 10 |
3+
| test.c:20:2:20:7 | call to fscanf | 1 | i | 0 | 0 |
4+
| test.c:21:2:21:7 | call to sscanf | 0 | s | 0 | 0 |
5+
| test.c:22:2:22:8 | call to swscanf | 0 | s | 10 | 10 |
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
| ms.cpp:17:3:17:8 | call to sscanf | 0 | 1 | ms.cpp:17:24:17:30 | %I64i | non-wide |
2-
| test.c:18:2:18:6 | call to scanf | 0 | 0 | test.c:18:8:18:11 | %s | non-wide |
3-
| test.c:19:2:19:7 | call to fscanf | 0 | 1 | test.c:19:15:19:23 | %10s %i | non-wide |
4-
| test.c:20:2:20:7 | call to sscanf | 0 | 1 | test.c:20:19:20:28 | %*i%s%*s | non-wide |
5-
| test.c:21:2:21:8 | call to swscanf | 0 | 1 | test.c:21:21:21:26 | %10s | wide |
2+
| test.c:19:2:19:6 | call to scanf | 0 | 0 | test.c:19:8:19:11 | %s | non-wide |
3+
| test.c:20:2:20:7 | call to fscanf | 0 | 1 | test.c:20:15:20:23 | %10s %i | non-wide |
4+
| test.c:21:2:21:7 | call to sscanf | 0 | 1 | test.c:21:19:21:28 | %*i%s%*s | non-wide |
5+
| test.c:22:2:22:8 | call to swscanf | 0 | 1 | test.c:22:21:22:26 | %10s | wide |
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
| ms.cpp:17:3:17:8 | call to sscanf | ms.cpp:17:33:17:36 | & ... | 0 |
2+
| test.c:19:2:19:6 | call to scanf | test.c:19:14:19:19 | buffer | 0 |
3+
| test.c:20:2:20:7 | call to fscanf | test.c:20:26:20:31 | buffer | 0 |
4+
| test.c:20:2:20:7 | call to fscanf | test.c:20:34:20:34 | i | 1 |
5+
| test.c:21:2:21:7 | call to sscanf | test.c:21:31:21:36 | buffer | 0 |
6+
| test.c:22:2:22:8 | call to swscanf | test.c:22:29:22:35 | wbuffer | 0 |
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import semmle.code.cpp.commons.Scanf
2+
3+
from ScanfFunctionCall sfc, Expr e, int n
4+
where e = sfc.getOutputArgument(n)
5+
select sfc, e, n

cpp/ql/test/library-tests/scanf/test.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,20 @@ int scanf(const char *format, ...);
77
int fscanf(FILE *stream, const char *format, ...);
88
int sscanf(const char *s, const char *format, ...);
99
int swscanf(const wchar_t* ws, const wchar_t* format, ...);
10+
int scanf_s(const char *format, ...);
1011

1112
int main(int argc, char *argv[])
1213
{
1314
char buffer[256];
1415
wchar_t wbuffer[256];
1516
FILE *file;
16-
int i;
17+
int i, i2;
1718

1819
scanf("%s", buffer);
1920
fscanf(file, "%10s %i", buffer, i);
2021
sscanf("Hello.", "%*i%s%*s", buffer);
2122
swscanf(L"Hello.", "%10s", wbuffer);
23+
scanf_s("%d %s %d", &i, buffer, 10, &i2);
2224

2325
return 0;
2426
}

0 commit comments

Comments
 (0)