Skip to content

Commit 208ae7a

Browse files
authored
Merge pull request #21514 from geoffw0/suspicioussizeof
C++: Fix an issue with cpp/suspicious-add-sizeof in BMN databases
2 parents be746b7 + 92c9a8e commit 208ae7a

File tree

5 files changed

+24
-1
lines changed

5 files changed

+24
-1
lines changed

cpp/ql/src/Security/CWE/CWE-468/SuspiciousAddWithSizeof.ql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ import IncorrectPointerScalingCommon
1818
private predicate isCharSzPtrExpr(Expr e) {
1919
exists(PointerType pt | pt = e.getFullyConverted().getUnspecifiedType() |
2020
pt.getBaseType() instanceof CharType or
21-
pt.getBaseType() instanceof VoidType
21+
pt.getBaseType() instanceof VoidType or
22+
pt.getBaseType() instanceof ErroneousType // this could be char / void type in a successful compilation
2223
)
2324
}
2425

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* Fixed an issue with the "Suspicious add with sizeof" (`cpp/suspicious-add-sizeof`) query causing false positive results in `build-mode: none` databases.

cpp/ql/test/query-tests/Security/CWE/CWE-468/semmle/SuspiciousAddWithSizeof/SuspiciousAddWithSizeof.expected

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
| buildless.cpp:5:15:5:25 | sizeof(int) | Suspicious sizeof offset in a pointer arithmetic expression. The type of the pointer is $@. | file://:0:0:0:0 | const short * | const short * |
2+
| buildless.cpp:6:13:6:23 | sizeof(int) | Suspicious sizeof offset in a pointer arithmetic expression. The type of the pointer is $@. | file://:0:0:0:0 | const int * | const int * |
13
| test.cpp:6:30:6:40 | sizeof(int) | Suspicious sizeof offset in a pointer arithmetic expression. The type of the pointer is $@. | file://:0:0:0:0 | int * | int * |
24
| test.cpp:14:30:14:40 | sizeof(int) | Suspicious sizeof offset in a pointer arithmetic expression. The type of the pointer is $@. | file://:0:0:0:0 | int * | int * |
35
| test.cpp:22:25:22:35 | sizeof(int) | Suspicious sizeof offset in a pointer arithmetic expression. The type of the pointer is $@. | file://:0:0:0:0 | int * | int * |
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// semmle-extractor-options: --expect_errors
2+
3+
void test_buildless(const char *p_c, const short *p_short, const int *p_int, const uint8_t *p_8, const uint16_t *p_16, const uint32_t *p_32) {
4+
*(p_c + sizeof(int)); // GOOD (`sizeof(char)` is 1)
5+
*(p_short + sizeof(int)); // BAD
6+
*(p_int + sizeof(int)); // BAD
7+
*(p_8 + sizeof(int)); // GOOD (`sizeof(uint8_t)` is 1, but there's an error in the type)
8+
*(p_16 + sizeof(int)); // BAD [NOT DETECTED]
9+
*(p_32 + sizeof(int)); // BAD [NOT DETECTED]
10+
}

cpp/ql/test/query-tests/Security/CWE/CWE-468/semmle/SuspiciousAddWithSizeof/test.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,3 +93,9 @@ class MyTest8Class
9393
myChar * const myCharsPointer;
9494
myInt * const myIntsPointer;
9595
};
96+
97+
typedef unsigned char uint8_t;
98+
typedef unsigned short uint16_t;
99+
typedef unsigned int uint32_t;
100+
101+
void test_buildless(const char *p_c, const short *p_short, const int *p_int, const uint8_t *p_8, const uint16_t *p_16, const uint32_t *p_32);

0 commit comments

Comments
 (0)