File tree Expand file tree Collapse file tree 2 files changed +30
-0
lines changed
cpp/ql/test/query-tests/Likely Bugs/Memory Management/SuspiciousCallToStrncat Expand file tree Collapse file tree 2 files changed +30
-0
lines changed Original file line number Diff line number Diff line change 1
1
| test.c:24:2:24:8 | call to strncat | Potentially unsafe call to strncat. |
2
+ | test.c:46:3:46:9 | call to strncat | Potentially unsafe call to strncat. |
3
+ | test.c:68:3:68:9 | call to strncat | Potentially unsafe call to strncat. |
Original file line number Diff line number Diff line change @@ -39,3 +39,31 @@ void bad1(char *s) {
39
39
strncat (buf , "." , 1 ); // BAD [NOT DETECTED] -- Need to check if any space is left
40
40
}
41
41
42
+
43
+ void strncat_test1 (char * s ) {
44
+ char buf [80 ];
45
+ strncat (buf , s , sizeof (buf ) - strlen (buf ) - 1 ); // GOOD
46
+ strncat (buf , s , sizeof (buf ) - strlen (buf )); // BAD
47
+ }
48
+
49
+ void * malloc (size_t );
50
+
51
+ void strncat_test2 (char * s ) {
52
+ int len = 80 ;
53
+ char * buf = (char * )malloc (len );
54
+ strncat (buf , s , len - strlen (buf ) - 1 ); // GOOD
55
+ strncat (buf , s , len - strlen (buf )); // BAD [NOT DETECTED]
56
+ }
57
+
58
+ struct buffers
59
+ {
60
+ char array [50 ];
61
+ char * pointer ;
62
+ };
63
+
64
+ void strncat_test3 (char * s , struct buffers * buffers ) {
65
+ unsigned len_array = strlen (buffers -> array );
66
+ unsigned max_size = sizeof (buffers -> array );
67
+ unsigned free_size = max_size - len_array ;
68
+ strncat (buffers -> array , s , free_size ); // BAD
69
+ }
You can’t perform that action at this time.
0 commit comments