Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3c9432d

Browse files
committed
CPP: Queries: Improve OverflowCalculated query.
1 parent 1e7bd9e commit 3c9432d

4 files changed

Lines changed: 7 additions & 12 deletions

File tree

cpp/ql/src/Critical/OverflowCalculated.ql

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,13 @@
1212

1313
import cpp
1414
import semmle.code.cpp.dataflow.DataFlow
15-
16-
class MallocCall extends FunctionCall {
17-
MallocCall() { this.getTarget().hasGlobalOrStdName("malloc") }
18-
19-
Expr getAllocatedSize() {
20-
result = this.getArgument(0)
21-
}
22-
}
15+
import semmle.code.cpp.models.interfaces.Allocation
2316

2417
predicate spaceProblem(FunctionCall append, string msg) {
25-
exists(MallocCall malloc, StrlenCall strlen, AddExpr add, FunctionCall insert, Variable buffer |
18+
exists(AllocationExpr malloc, StrlenCall strlen, AddExpr add, FunctionCall insert, Variable buffer |
2619
add.getAChild() = strlen and
2720
exists(add.getAChild().getValue()) and
28-
DataFlow::localExprFlow(add, malloc.getAllocatedSize()) and
21+
DataFlow::localExprFlow(add, malloc.getSizeExpr()) and
2922
buffer.getAnAccess() = strlen.getStringExpr() and
3023
(
3124
insert.getTarget().hasGlobalOrStdName("strcpy") or
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
| tests1.cpp:26:21:26:26 | call to malloc | This allocation does not include space to null-terminate the string. |
2+
| tests1.cpp:56:21:56:27 | call to realloc | This allocation does not include space to null-terminate the string. |
23
| tests1.cpp:67:21:67:26 | call to malloc | This allocation does not include space to null-terminate the string. |
34
| tests1.cpp:89:25:89:30 | call to malloc | This allocation does not include space to null-terminate the string. |
45
| tests3.cpp:25:21:25:31 | call to malloc | This allocation does not include space to null-terminate the string. |
56
| tests3.cpp:30:21:30:31 | call to malloc | This allocation does not include space to null-terminate the string. |
7+
| tests3.cpp:53:17:53:44 | new[] | This allocation does not include space to null-terminate the string. |

cpp/ql/test/query-tests/Critical/OverflowCalculated/tests1.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ void tests1(int case_num)
5353
break;
5454

5555
case 7:
56-
buffer = (char *)realloc(buffer, strlen(str)); // BAD [NOT DETECTED]
56+
buffer = (char *)realloc(buffer, strlen(str)); // BAD
5757
strcpy(buffer, str);
5858
break;
5959

cpp/ql/test/query-tests/Critical/OverflowCalculated/tests3.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ void tests3(int case_num)
5050

5151
void test3b()
5252
{
53-
char *buffer = new char[strlen(str3global)]; // BAD [NOT DETECTED]
53+
char *buffer = new char[strlen(str3global)]; // BAD
5454

5555
strcpy(buffer, str3global);
5656

0 commit comments

Comments
 (0)