CPP: Handle array accesses.

This commit is contained in:
Geoffrey White
2019-01-29 16:00:44 +00:00
parent 4685f193f5
commit 07adf6f201
3 changed files with 9 additions and 5 deletions

View File

@@ -15,7 +15,8 @@ import cpp
// or accesses a possibly stack allocated local variables
predicate exprMaybeStackAllocated(Expr e) {
e instanceof AggregateLiteral or
varMaybeStackAllocated(e.(VariableAccess).getTarget())
varMaybeStackAllocated(e.(VariableAccess).getTarget()) or
exprMayPointToStack(e.(ArrayExpr).getArrayBase())
}
// a local variable is possibly stack allocated if it is not static and
@@ -34,9 +35,11 @@ predicate exprMayPointToStack(Expr e) {
or
varMayPointToStack(e.(VariableAccess).getTarget())
or
exprMaybeStackAllocated(e) and
e.getType() instanceof ArrayType and
e.getFullyConverted().getType() instanceof PointerType
(
exprMaybeStackAllocated(e) and
e.getType() instanceof ArrayType and
e.getFullyConverted().getType() instanceof PointerType
)
}
// a local variable possibly points to the stack if it is initialized to/assigned to

View File

@@ -2,3 +2,4 @@
| test.cpp:20:2:20:12 | return ... | May return stack-allocated memory. |
| test.cpp:73:2:73:12 | return ... | May return stack-allocated memory. |
| test.cpp:93:2:93:12 | return ... | May return stack-allocated memory. |
| test.cpp:100:2:100:19 | return ... | May return stack-allocated memory. |

View File

@@ -97,7 +97,7 @@ char *testArray2()
{
char arr[256];
return &(arr[10]); // BAD [NOT DETECTED]
return &(arr[10]); // BAD
}
char testArray3()