Skip to content

Commit ea56a5d

Browse files
committed
CPP: Add local dataflow to (one bit of) OverflowStatic.ql.
1 parent 01ba635 commit ea56a5d

File tree

3 files changed

+18
-8
lines changed

3 files changed

+18
-8
lines changed

cpp/ql/src/Critical/OverflowStatic.ql

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,22 +82,31 @@ class CallWithBufferSize extends FunctionCall
8282
Expr buffer() {
8383
exists(int i |
8484
bufferAndSizeFunction(this.getTarget(), i, _) and
85-
result = this.getArgument(i))
85+
result = this.getArgument(i)
86+
)
8687
}
87-
Expr statedSize() {
88+
Expr statedSizeExpr() {
8889
exists(int i |
8990
bufferAndSizeFunction(this.getTarget(), _, i) and
90-
result = this.getArgument(i))
91+
result = this.getArgument(i)
92+
)
93+
}
94+
int statedSizeValue() {
95+
exists(Expr statedSizeSrc |
96+
DataFlow::localFlowStep*(DataFlow::exprNode(statedSizeSrc), DataFlow::exprNode(statedSizeExpr())) and
97+
result = statedSizeSrc.getValue().toInt()
98+
)
9199
}
92100
}
93101

94102
predicate wrongBufferSize(Expr error, string msg) {
95-
exists(CallWithBufferSize call, int bufsize, Variable buf |
103+
exists(CallWithBufferSize call, int bufsize, Variable buf, int statedSize |
96104
staticBuffer(call.buffer(), buf, bufsize) and
97-
call.statedSize().getValue().toInt() > bufsize and
98-
error = call.statedSize() and
105+
statedSize = call.statedSizeValue() and
106+
statedSize > bufsize and
107+
error = call.statedSizeExpr() and
99108
msg = "Potential buffer-overflow: '" + buf.getName() +
100-
"' has size " + bufsize.toString() + " not " + call.statedSize().getValue() + ".")
109+
"' has size " + bufsize.toString() + " not " + statedSize + ".")
101110
}
102111

103112
predicate outOfBounds(BufferAccess bufaccess, string msg)

cpp/ql/test/query-tests/Critical/OverflowStatic/OverflowStatic.expected

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
| test.cpp:20:3:20:12 | access to array | Potential buffer-overflow: counter 'i' <= 3 but 'buffer2' has 3 elements. |
77
| test.cpp:24:27:24:27 | 4 | Potential buffer-overflow: 'buffer1' has size 3 not 4. |
88
| test.cpp:26:27:26:27 | 4 | Potential buffer-overflow: 'buffer2' has size 3 not 4. |
9+
| test.cpp:40:22:40:27 | amount | Potential buffer-overflow: 'buffer' has size 100 not 101. |

cpp/ql/test/query-tests/Critical/OverflowStatic/test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ void f2(char *src)
3737
amount = amount + 1;
3838
memcpy(buffer, src, amount); // BAD [NOT DETECTED]
3939
amount = 101;
40-
memcpy(buffer, src, amount); // BAD [NOT DETECTED]
40+
memcpy(buffer, src, amount); // BAD
4141

4242
ptr = buffer;
4343
memcpy(ptr, src, 101); // BAD [NOT DETECTED]

0 commit comments

Comments
 (0)