Skip to content

Commit e857ac1

Browse files
committed
C++: Add more tests and remove redundant conjunct.
1 parent 84b0b8c commit e857ac1

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

cpp/ql/src/Likely Bugs/Memory Management/SuspiciousCallToStrncat.ql

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@ predicate interestringCallWithArgs(Call call, Expr sizeArg, Expr destArg) {
3838
*/
3939
predicate case1(FunctionCall fc, Expr sizeArg, VariableAccess destArg) {
4040
interestringCallWithArgs(fc, sizeArg, destArg) and
41-
exists(StrcatFunction strncat, VariableAccess va |
42-
fc.getTarget() = strncat and
43-
destArg = fc.getArgument(strncat.getParamDest()) and
41+
exists(VariableAccess va |
4442
va = sizeArg.(BufferSizeExpr).getArg() and
4543
destArg.getTarget() = va.getTarget()
4644
)
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
| 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. |
2+
| test.c:45:3:45:9 | call to strncat | Potentially unsafe call to strncat. |
3+
| test.c:67:3:67:9 | call to strncat | Potentially unsafe call to strncat. |
4+
| test.c:75:3:75:9 | call to strncat | Potentially unsafe call to strncat. |
5+
| test.c:76:3:76:9 | call to strncat | Potentially unsafe call to strncat. |

cpp/ql/test/query-tests/Likely Bugs/Memory Management/SuspiciousCallToStrncat/test.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ void bad1(char *s) {
3939
strncat(buf, ".", 1); // BAD [NOT DETECTED] -- Need to check if any space is left
4040
}
4141

42-
4342
void strncat_test1(char *s) {
4443
char buf[80];
4544
strncat(buf, s, sizeof(buf) - strlen(buf) - 1); // GOOD
@@ -66,4 +65,20 @@ void strncat_test3(char* s, struct buffers* buffers) {
6665
unsigned max_size = sizeof(buffers->array);
6766
unsigned free_size = max_size - len_array;
6867
strncat(buffers->array, s, free_size); // BAD
69-
}
68+
}
69+
70+
#define MAX_SIZE 80
71+
72+
void strncat_test4(char *s) {
73+
char buf[MAX_SIZE];
74+
strncat(buf, s, MAX_SIZE - strlen(buf) - 1); // GOOD
75+
strncat(buf, s, MAX_SIZE - strlen(buf)); // BAD
76+
strncat(buf, "...", MAX_SIZE - strlen(buf)); // BAD
77+
}
78+
79+
void strncat_test5(char *s) {
80+
int len = 80;
81+
char* buf = (char *) malloc(len + 1);
82+
strncat(buf, s, len - strlen(buf) - 1); // GOOD
83+
strncat(buf, s, len - strlen(buf)); // GOOD
84+
}

0 commit comments

Comments
 (0)