Skip to content

Commit fa9242b

Browse files
committed
C++: Add tests for bounded hex format values
1 parent d5682f1 commit fa9242b

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

cpp/ql/test/query-tests/Security/CWE/CWE-120/semmle/tests/OverrunWrite.expected

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@
1414
| tests.c:120:3:120:9 | call to sprintf | This 'call to sprintf' operation requires 17 bytes but the destination is only 1 bytes. |
1515
| tests.c:121:3:121:9 | call to sprintf | This 'call to sprintf' operation requires 17 bytes but the destination is only 16 bytes. |
1616
| tests.c:136:2:136:8 | call to sprintf | This 'call to sprintf' operation requires 11 bytes but the destination is only 10 bytes. |
17+
| tests.c:178:2:178:8 | call to sprintf | This 'call to sprintf' operation requires 9 bytes but the destination is only 2 bytes. |
18+
| tests.c:179:2:179:8 | call to sprintf | This 'call to sprintf' operation requires 9 bytes but the destination is only 3 bytes. |
19+
| tests.c:180:2:180:8 | call to sprintf | This 'call to sprintf' operation requires 9 bytes but the destination is only 5 bytes. |
20+
| tests.c:182:3:182:9 | call to sprintf | This 'call to sprintf' operation requires 9 bytes but the destination is only 2 bytes. |
21+
| tests.c:186:3:186:9 | call to sprintf | This 'call to sprintf' operation requires 9 bytes but the destination is only 2 bytes. |
22+
| tests.c:189:3:189:9 | call to sprintf | This 'call to sprintf' operation requires 9 bytes but the destination is only 2 bytes. |
23+
| tests.c:193:3:193:9 | call to sprintf | This 'call to sprintf' operation requires 9 bytes but the destination is only 5 bytes. |
1724
| unions.c:26:2:26:7 | call to strcpy | This 'call to strcpy' operation requires 21 bytes but the destination is only 16 bytes. |
1825
| unions.c:27:2:27:7 | call to strcpy | This 'call to strcpy' operation requires 21 bytes but the destination is only 15 bytes. |
1926
| unions.c:27:2:27:7 | call to strcpy | This 'call to strcpy' operation requires 21 bytes but the destination is only 16 bytes. |

cpp/ql/test/query-tests/Security/CWE/CWE-120/semmle/tests/tests.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,27 @@ void testVarSizeStruct()
169169

170170
snprintf(s->data, 10, "abcdefghijklmnopqrstuvwxyz"); // GOOD
171171
}
172+
173+
void tesHexBounds(int x) {
174+
char buffer2[2];
175+
char buffer3[3];
176+
char buffer5[5];
177+
178+
sprintf(buffer2, "%x", 1); // GOOD [FALSE POSITIVE]
179+
sprintf(buffer3, "%x", 16); // GOOD [FALSE POSITIVE]
180+
sprintf(buffer5, "%x", (unsigned short)x); // GOOD: bounded by conversion [FALSE POSITIVE]
181+
if (x < 16 && x > 0) {
182+
sprintf(buffer2, "%x", x); // GOOD: bounded by check [FALSE POSITIVE]
183+
}
184+
185+
if (x < 16) {
186+
sprintf(buffer2, "%x", x); // BAD: negative values
187+
}
188+
if (x <= 16 && x > 0) {
189+
sprintf(buffer2, "%x", x); // BAD: bound too loose
190+
}
191+
192+
if(x < 0x10000 && x > 0) {
193+
sprintf(buffer5, "%x", x); // GOOD: bounded by check [FALSE POSITIVE]
194+
}
195+
}

0 commit comments

Comments
 (0)