Skip to content

Commit d332093

Browse files
committed
C++: Fix test annotations. Also exclude static locals from the query and add a testcase for this.
1 parent 70a953b commit d332093

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

cpp/ql/src/Security/CWE/CWE-014/MemsetMayBeDeleted.ql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ where
3232
forall(Expr escape | variableAddressEscapesTree(v.getAnAccess(), escape) |
3333
call.getArgument(0) = escape.getUnconverted()
3434
) and
35-
// `v` is a stack-allocated array or a struct.
35+
// `v` is a stack-allocated array or a struct, and `v` is not static.
36+
not v.isStatic() and
3637
(
3738
v.getUnspecifiedType() instanceof ArrayType and call.getArgument(0) = v.getAnAccess()
3839
or

cpp/ql/test/query-tests/Security/CWE/CWE-014/test.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ int func1d() {
5959
memset(pw1d, 0, PW_SIZE); // GOOD
6060
return 0;
6161
}
62-
// x86-64 gcc 9.2: not deleted
63-
// x86-64 clang 9.0.0: not deleted
64-
// x64 msvc v19.14 (WINE): not deleted
62+
// x86-64 gcc 9.2: deleted
63+
// x86-64 clang 9.0.0: deleted
64+
// x64 msvc v19.14 (WINE): deleted
6565
char *func2(void) {
6666
char pw2[PW_SIZE];
6767
use_pw(pw2);
@@ -129,9 +129,9 @@ int func8(void) {
129129
return pw1a[4];
130130
}
131131

132-
// x86-64 gcc 9.2: not deleted
133-
// x86-64 clang 9.0.0: not deleted
134-
// x64 msvc v19.14 (WINE): not deleted
132+
// x86-64 gcc 9.2: deleted
133+
// x86-64 clang 9.0.0: deleted
134+
// x64 msvc v19.14 (WINE): deleted
135135
char *func9(void) {
136136
char pw1[PW_SIZE];
137137
use_pw(pw1);

cpp/ql/test/query-tests/Security/CWE/CWE-014/test.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ bool nobadFunc2_1_0(unsigned char ch){
277277

278278
void nobadFunc2_1_2(){
279279
unsigned char buff1[PW_SIZE];
280-
memset(buff1, 0, PW_SIZE); // GOOD
280+
memset(buff1, 0, PW_SIZE); // BAD [NOT DETECTED]
281281
buff1[2] = 5;
282282
}
283283

@@ -364,3 +364,11 @@ void nobadFunc4_6(){
364364
unsigned char * buff1 = globalBuff2->buff2;
365365
memset(buff1, 0, PW_SIZE); // GOOD
366366
}
367+
368+
extern void use_byte(unsigned char);
369+
370+
void test_static_func() {
371+
static unsigned char buffer[PW_SIZE] = {0};
372+
use_byte(buffer[0]);
373+
memset(buffer, 42, sizeof(buffer)); // GOOD
374+
}

0 commit comments

Comments
 (0)