Skip to content

Commit 4dcaa7b

Browse files
author
dilanbhalla
committed
pr fixes
1 parent 6e6921b commit 4dcaa7b

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
///// Library routines /////
2+
3+
int scanf(const char *format, ...);
4+
int sscanf(const char *str, const char *format, ...);
5+
int fscanf(const char *str, const char *format, ...);
6+
7+
///// EXAMPLES /////
8+
9+
int main(int argc, char **argv)
10+
{
11+
12+
// BAD, do not use scanf without specifying a length first
13+
char buf1[10];
14+
scanf("%s", buf1);
15+
16+
// GOOD, length is specified. The length should be one less than the size of the buffer, since the last character is the NULL terminator.
17+
char buf2[10];
18+
sscanf(buf2, "%9s");
19+
20+
// BAD, do not use scanf without specifying a length first
21+
char file[10];
22+
fscanf(file, "%s", buf2);
23+
24+
return 0;
25+
}

cpp/ql/src/experimental/Security/CWE/CWE-120/MemoryUnsafeFunctionScan.qhelp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@
77

88
</overview>
99

10+
<recommendation>
11+
12+
<p>Specify a length within the format string parameter, and make this length one less than the size of the buffer, since the last character should be reserved for the NULL terminator.</p>
13+
14+
</recommendation>
15+
16+
<example>
17+
<p>The following example demonstrates safe and unsafe uses of scanf type functions.</p>
18+
<sample src="MemoryUnsafeFunctionScan.cpp" />
19+
20+
</example>
21+
1022
<references>
1123
</references>
1224

0 commit comments

Comments
 (0)