Skip to content

Commit b7653ec

Browse files
committed
C++: ignore cast arrays in constant off-by-one query
1 parent d9665e1 commit b7653ec

File tree

3 files changed

+3
-8
lines changed

3 files changed

+3
-8
lines changed

cpp/ql/src/experimental/Security/CWE/CWE-193/ConstantSizeArrayOffByOne.ql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ predicate isConstantSizeOverflowSource(Field f, FieldAddressToPointerArithmeticF
9898
FieldAddressToPointerArithmeticFlow::flowPath(fieldSource, sink) and
9999
isFieldAddressSource(f, fieldSource.getNode()) and
100100
pai.getLeft() = sink.getNode().(DataFlow::InstructionNode).asInstruction() and
101+
pai.getElementSize() = f.getUnspecifiedType().(ArrayType).getBaseType().getSize() and
101102
f.getUnspecifiedType().(ArrayType).getArraySize() = size and
102103
semBounded(getSemanticExpr(pai.getRight()), any(SemZeroBound b), bound, true, _) and
103104
delta = bound - size and

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,6 @@ edges
3535
| test.cpp:79:32:79:34 | buf | test.cpp:79:27:79:34 | buf |
3636
| test.cpp:85:34:85:36 | buf | test.cpp:87:5:87:11 | charBuf |
3737
| test.cpp:85:34:85:36 | buf | test.cpp:88:5:88:11 | charBuf |
38-
| test.cpp:87:5:87:11 | charBuf | test.cpp:87:5:87:31 | access to array |
39-
| test.cpp:88:5:88:11 | charBuf | test.cpp:88:5:88:27 | access to array |
4038
nodes
4139
| test.cpp:26:5:26:12 | buf | semmle.label | buf |
4240
| test.cpp:26:10:26:12 | buf | semmle.label | buf |
@@ -90,9 +88,7 @@ nodes
9088
| test.cpp:79:32:79:34 | buf | semmle.label | buf |
9189
| test.cpp:85:34:85:36 | buf | semmle.label | buf |
9290
| test.cpp:87:5:87:11 | charBuf | semmle.label | charBuf |
93-
| test.cpp:87:5:87:31 | access to array | semmle.label | access to array |
9491
| test.cpp:88:5:88:11 | charBuf | semmle.label | charBuf |
95-
| test.cpp:88:5:88:27 | access to array | semmle.label | access to array |
9692
subpaths
9793
#select
9894
| test.cpp:35:5:35:22 | 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 |
@@ -107,5 +103,3 @@ subpaths
107103
| test.cpp:77:27:77:44 | 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 |
108104
| test.cpp:77:27:77:44 | access to array | test.cpp:77:32:77:34 | buf | test.cpp:67:5:67:6 | * ... | 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 |
109105
| test.cpp:77:27:77:44 | access to array | test.cpp:77:32:77:34 | buf | test.cpp:67:6:67:6 | 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 |
110-
| test.cpp:87:5:87:31 | access to array | test.cpp:85:34:85:36 | buf | test.cpp:87:5:87:31 | access to array | This pointer arithmetic may have an off-by-3072 error allowing it to overrun $@ at this $@. | test.cpp:15:9:15:11 | buf | buf | test.cpp:87:5:87:35 | Store: ... = ... | write |
111-
| test.cpp:88:5:88:27 | access to array | test.cpp:85:34:85:36 | buf | test.cpp:88:5:88:27 | access to array | This pointer arithmetic may have an off-by-3073 error allowing it to overrun $@ at this $@. | test.cpp:15:9:15:11 | buf | buf | test.cpp:88:5:88:31 | Store: ... = ... | write |

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,6 @@ void testInterproc(BigArray *arr) {
8484
void testCharIndex(BigArray *arr) {
8585
char *charBuf = (char*) arr->buf;
8686

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

0 commit comments

Comments
 (0)