Skip to content

Commit c9c93ca

Browse files
committed
C++: test for strncmp false positives
1 parent df4d156 commit c9c93ca

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

cpp/ql/test/experimental/query-tests/Security/CWE/CWE-193/constant-size/ConstantSizeArrayOffByOne.expected

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ edges
1212
| test.cpp:79:27:79:34 | buf | test.cpp:70:33:70:33 | p |
1313
| test.cpp:79:32:79:34 | buf | test.cpp:79:27:79:34 | buf |
1414
| test.cpp:128:9:128:11 | arr | test.cpp:128:9:128:14 | access to array |
15+
| test.cpp:134:25:134:27 | arr | test.cpp:136:9:136:16 | ... += ... |
16+
| test.cpp:136:9:136:16 | ... += ... | test.cpp:138:13:138:15 | arr |
17+
| test.cpp:143:18:143:21 | asdf | test.cpp:134:25:134:27 | arr |
18+
| test.cpp:143:18:143:21 | asdf | test.cpp:143:18:143:21 | asdf |
1519
nodes
1620
| test.cpp:35:5:35:22 | access to array | semmle.label | access to array |
1721
| test.cpp:35:10:35:12 | buf | semmle.label | buf |
@@ -36,6 +40,11 @@ nodes
3640
| test.cpp:79:32:79:34 | buf | semmle.label | buf |
3741
| test.cpp:128:9:128:11 | arr | semmle.label | arr |
3842
| test.cpp:128:9:128:14 | access to array | semmle.label | access to array |
43+
| test.cpp:134:25:134:27 | arr | semmle.label | arr |
44+
| test.cpp:136:9:136:16 | ... += ... | semmle.label | ... += ... |
45+
| test.cpp:138:13:138:15 | arr | semmle.label | arr |
46+
| test.cpp:143:18:143:21 | asdf | semmle.label | asdf |
47+
| test.cpp:143:18:143:21 | asdf | semmle.label | asdf |
3948
subpaths
4049
#select
4150
| test.cpp:35:5:35:22 | PointerAdd: access to array | test.cpp:35:10:35:12 | buf | test.cpp:35:5:35:22 | access to array | This pointer arithmetic may have an off-by-1 error allowing it to overrun $@ at this $@. | test.cpp:15:9:15:11 | buf | buf | test.cpp:35:5:35:26 | Store: ... = ... | write |
@@ -48,3 +57,4 @@ subpaths
4857
| test.cpp:72:5:72:15 | PointerAdd: access to array | test.cpp:79:32:79:34 | buf | test.cpp:72:5:72:15 | access to array | This pointer arithmetic may have an off-by-1 error allowing it to overrun $@ at this $@. | test.cpp:15:9:15:11 | buf | buf | test.cpp:72:5:72:19 | Store: ... = ... | write |
4958
| test.cpp:77:27:77:44 | PointerAdd: access to array | test.cpp:77:32:77:34 | buf | test.cpp:66:32:66:32 | p | This pointer arithmetic may have an off-by-1 error allowing it to overrun $@ at this $@. | test.cpp:15:9:15:11 | buf | buf | test.cpp:67:5:67:10 | Store: ... = ... | write |
5059
| test.cpp:128:9:128:14 | PointerAdd: access to array | test.cpp:128:9:128:11 | arr | test.cpp:128:9:128:14 | access to array | This pointer arithmetic may have an off-by-1 error allowing it to overrun $@ at this $@. | test.cpp:125:11:125:13 | arr | arr | test.cpp:128:9:128:18 | Store: ... = ... | write |
60+
| test.cpp:136:9:136:16 | PointerAdd: ... += ... | test.cpp:143:18:143:21 | asdf | test.cpp:138:13:138:15 | arr | This pointer arithmetic may have an off-by-2 error allowing it to overrun $@ at this $@. | test.cpp:142:10:142:13 | asdf | asdf | test.cpp:138:12:138:15 | Load: * ... | read |

cpp/ql/test/experimental/query-tests/Security/CWE/CWE-193/constant-size/test.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ void testCharIndex(BigArray *arr) {
8585
char *charBuf = (char*) arr->buf;
8686

8787
charBuf[MAX_SIZE_BYTES - 1] = 0; // GOOD
88-
charBuf[MAX_SIZE_BYTES] = 0; // BAD [FALSE NEGATIVE]
88+
charBuf[MAX_SIZE_BYTES] = 0; // BAD
8989
}
9090

9191
void testEqRefinement() {
@@ -128,3 +128,17 @@ void testStackAllocated() {
128128
arr[i] = 0; // BAD
129129
}
130130
}
131+
132+
int strncmp(const char*, const char*, int);
133+
134+
char testStrncmp2(char *arr) {
135+
if(strncmp(arr, "<test>", 6) == 0) {
136+
arr += 6;
137+
}
138+
return *arr; // GOOD [FALSE POSITIVE]
139+
}
140+
141+
void testStrncmp1() {
142+
char asdf[5];
143+
testStrncmp2(asdf);
144+
}

0 commit comments

Comments
 (0)