Skip to content

Commit 98dc73c

Browse files
authored
Merge pull request github#12611 from MathiasVP/buffer-access-should-be-evaluated
C++: Exclude unevaluated accesses in `BufferAccess`
2 parents 6d665da + 2ce0d2b commit 98dc73c

File tree

5 files changed

+19
-1
lines changed

5 files changed

+19
-1
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* The `BufferAccess` library (`semmle.code.cpp.security.BufferAccess`) no longer matches buffer accesses inside unevaluated contexts (such as inside `sizeof` or `decltype` expressions). As a result, queries using this library may see fewer false positives.

cpp/ql/lib/semmle/code/cpp/security/BufferAccess.qll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ int getPointedSize(Type t) {
1414
* BufferWrite differ.
1515
*/
1616
abstract class BufferAccess extends Expr {
17+
BufferAccess() { not this.isUnevaluated() }
18+
1719
abstract string getName();
1820

1921
/**

cpp/ql/src/Critical/OverflowStatic.ql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ import LoopBounds
2222
private predicate staticBufferBase(VariableAccess access, Variable v) {
2323
v.getType().(ArrayType).getBaseType() instanceof CharType and
2424
access = v.getAnAccess() and
25-
not memberMayBeVarSize(_, v)
25+
not memberMayBeVarSize(_, v) and
26+
not access.isUnevaluated()
2627
}
2728

2829
predicate staticBuffer(VariableAccess access, Variable v, int size) {

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,8 @@ void f3() {
5656
}
5757
}
5858
}
59+
60+
int unevaluated_test() {
61+
char buffer[100];
62+
return sizeof(buffer) / sizeof(buffer[101]); // GOOD
63+
}

cpp/ql/test/query-tests/Security/CWE/CWE-119/semmle/tests/tests.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,11 @@ void test22(bool b, const char* source) {
603603
memcpy(dest, source, n); // GOOD
604604
}
605605

606+
int test23() {
607+
char buffer[100];
608+
return sizeof(buffer) / sizeof(buffer[101]); // GOOD
609+
}
610+
606611
int tests_main(int argc, char *argv[])
607612
{
608613
long long arr17[19];
@@ -627,6 +632,7 @@ int tests_main(int argc, char *argv[])
627632
test20();
628633
test21(argc == 0);
629634
test22(argc == 0, argv[0]);
635+
test23();
630636

631637
return 0;
632638
}

0 commit comments

Comments
 (0)