From 2e987f8d785f8169fd36defa8e0ae4186522d658 Mon Sep 17 00:00:00 2001 From: Geoffrey White <40627776+geoffw0@users.noreply.github.com> Date: Wed, 18 Mar 2026 14:15:05 +0000 Subject: [PATCH 1/4] C++: Add test cases emulating cpp/suspicious-add-sizeof in buildless mode. --- .../SuspiciousAddWithSizeof.expected | 5 +++++ .../semmle/SuspiciousAddWithSizeof/buildless.cpp | 10 ++++++++++ .../CWE-468/semmle/SuspiciousAddWithSizeof/test.cpp | 6 ++++++ 3 files changed, 21 insertions(+) create mode 100644 cpp/ql/test/query-tests/Security/CWE/CWE-468/semmle/SuspiciousAddWithSizeof/buildless.cpp diff --git a/cpp/ql/test/query-tests/Security/CWE/CWE-468/semmle/SuspiciousAddWithSizeof/SuspiciousAddWithSizeof.expected b/cpp/ql/test/query-tests/Security/CWE/CWE-468/semmle/SuspiciousAddWithSizeof/SuspiciousAddWithSizeof.expected index 8b67b3f8bc91..c1a192afc3a4 100644 --- a/cpp/ql/test/query-tests/Security/CWE/CWE-468/semmle/SuspiciousAddWithSizeof/SuspiciousAddWithSizeof.expected +++ b/cpp/ql/test/query-tests/Security/CWE/CWE-468/semmle/SuspiciousAddWithSizeof/SuspiciousAddWithSizeof.expected @@ -1,3 +1,8 @@ +| 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 * | +| 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 * | +| buildless.cpp:7:11:7:21 | sizeof(int) | Suspicious sizeof offset in a pointer arithmetic expression. The type of the pointer is $@. | file://:0:0:0:0 | const * | const * | +| buildless.cpp:8:12:8:22 | sizeof(int) | Suspicious sizeof offset in a pointer arithmetic expression. The type of the pointer is $@. | file://:0:0:0:0 | const * | const * | +| buildless.cpp:9:12:9:22 | sizeof(int) | Suspicious sizeof offset in a pointer arithmetic expression. The type of the pointer is $@. | file://:0:0:0:0 | const * | const * | | 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 * | | 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 * | | 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 * | diff --git a/cpp/ql/test/query-tests/Security/CWE/CWE-468/semmle/SuspiciousAddWithSizeof/buildless.cpp b/cpp/ql/test/query-tests/Security/CWE/CWE-468/semmle/SuspiciousAddWithSizeof/buildless.cpp new file mode 100644 index 000000000000..cc93ef719b52 --- /dev/null +++ b/cpp/ql/test/query-tests/Security/CWE/CWE-468/semmle/SuspiciousAddWithSizeof/buildless.cpp @@ -0,0 +1,10 @@ +// semmle-extractor-options: --expect_errors + +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) { + *(p_c + sizeof(int)); // GOOD (`sizeof(char)` is 1) + *(p_short + sizeof(int)); // BAD + *(p_int + sizeof(int)); // BAD + *(p_8 + sizeof(int)); // GOOD (`sizeof(p_8)` is 1) [FALSE POSITIVE] + *(p_16 + sizeof(int)); // BAD + *(p_32 + sizeof(int)); // BAD +} diff --git a/cpp/ql/test/query-tests/Security/CWE/CWE-468/semmle/SuspiciousAddWithSizeof/test.cpp b/cpp/ql/test/query-tests/Security/CWE/CWE-468/semmle/SuspiciousAddWithSizeof/test.cpp index f2ad227417e5..fa2bd934cca2 100644 --- a/cpp/ql/test/query-tests/Security/CWE/CWE-468/semmle/SuspiciousAddWithSizeof/test.cpp +++ b/cpp/ql/test/query-tests/Security/CWE/CWE-468/semmle/SuspiciousAddWithSizeof/test.cpp @@ -93,3 +93,9 @@ class MyTest8Class myChar * const myCharsPointer; myInt * const myIntsPointer; }; + +typedef unsigned char uint8_t; +typedef unsigned short uint16_t; +typedef unsigned int uint32_t; + +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); From 0f794b57ed742e7551c50a40a56e2fb09240eb61 Mon Sep 17 00:00:00 2001 From: Geoffrey White <40627776+geoffw0@users.noreply.github.com> Date: Thu, 19 Mar 2026 13:16:16 +0000 Subject: [PATCH 2/4] C++: Fix the issue. --- cpp/ql/src/Security/CWE/CWE-468/SuspiciousAddWithSizeof.ql | 3 ++- .../SuspiciousAddWithSizeof.expected | 3 --- .../CWE-468/semmle/SuspiciousAddWithSizeof/buildless.cpp | 6 +++--- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/cpp/ql/src/Security/CWE/CWE-468/SuspiciousAddWithSizeof.ql b/cpp/ql/src/Security/CWE/CWE-468/SuspiciousAddWithSizeof.ql index d9c9df4fd918..343e96a00d39 100644 --- a/cpp/ql/src/Security/CWE/CWE-468/SuspiciousAddWithSizeof.ql +++ b/cpp/ql/src/Security/CWE/CWE-468/SuspiciousAddWithSizeof.ql @@ -18,7 +18,8 @@ import IncorrectPointerScalingCommon private predicate isCharSzPtrExpr(Expr e) { exists(PointerType pt | pt = e.getFullyConverted().getUnspecifiedType() | pt.getBaseType() instanceof CharType or - pt.getBaseType() instanceof VoidType + pt.getBaseType() instanceof VoidType or + pt.getBaseType() instanceof ErroneousType // this could be char / void type in a successful compilation ) } diff --git a/cpp/ql/test/query-tests/Security/CWE/CWE-468/semmle/SuspiciousAddWithSizeof/SuspiciousAddWithSizeof.expected b/cpp/ql/test/query-tests/Security/CWE/CWE-468/semmle/SuspiciousAddWithSizeof/SuspiciousAddWithSizeof.expected index c1a192afc3a4..dbff4230f254 100644 --- a/cpp/ql/test/query-tests/Security/CWE/CWE-468/semmle/SuspiciousAddWithSizeof/SuspiciousAddWithSizeof.expected +++ b/cpp/ql/test/query-tests/Security/CWE/CWE-468/semmle/SuspiciousAddWithSizeof/SuspiciousAddWithSizeof.expected @@ -1,8 +1,5 @@ | 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 * | | 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 * | -| buildless.cpp:7:11:7:21 | sizeof(int) | Suspicious sizeof offset in a pointer arithmetic expression. The type of the pointer is $@. | file://:0:0:0:0 | const * | const * | -| buildless.cpp:8:12:8:22 | sizeof(int) | Suspicious sizeof offset in a pointer arithmetic expression. The type of the pointer is $@. | file://:0:0:0:0 | const * | const * | -| buildless.cpp:9:12:9:22 | sizeof(int) | Suspicious sizeof offset in a pointer arithmetic expression. The type of the pointer is $@. | file://:0:0:0:0 | const * | const * | | 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 * | | 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 * | | 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 * | diff --git a/cpp/ql/test/query-tests/Security/CWE/CWE-468/semmle/SuspiciousAddWithSizeof/buildless.cpp b/cpp/ql/test/query-tests/Security/CWE/CWE-468/semmle/SuspiciousAddWithSizeof/buildless.cpp index cc93ef719b52..bfe4f5468035 100644 --- a/cpp/ql/test/query-tests/Security/CWE/CWE-468/semmle/SuspiciousAddWithSizeof/buildless.cpp +++ b/cpp/ql/test/query-tests/Security/CWE/CWE-468/semmle/SuspiciousAddWithSizeof/buildless.cpp @@ -4,7 +4,7 @@ void test_buildless(const char *p_c, const short *p_short, const int *p_int, con *(p_c + sizeof(int)); // GOOD (`sizeof(char)` is 1) *(p_short + sizeof(int)); // BAD *(p_int + sizeof(int)); // BAD - *(p_8 + sizeof(int)); // GOOD (`sizeof(p_8)` is 1) [FALSE POSITIVE] - *(p_16 + sizeof(int)); // BAD - *(p_32 + sizeof(int)); // BAD + *(p_8 + sizeof(int)); // GOOD (`sizeof(p_8)` is 1, but there's an error in the type) + *(p_16 + sizeof(int)); // BAD [NOT DETECTED] + *(p_32 + sizeof(int)); // BAD [NOT DETECTED] } From 21cb11ea5d1b343a68e7be818c41a9984c85ad69 Mon Sep 17 00:00:00 2001 From: Geoffrey White <40627776+geoffw0@users.noreply.github.com> Date: Thu, 19 Mar 2026 13:29:41 +0000 Subject: [PATCH 3/4] C++: Change note. --- cpp/ql/src/change-notes/2026-03-19-suspicious-add-sizeof.md | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 cpp/ql/src/change-notes/2026-03-19-suspicious-add-sizeof.md diff --git a/cpp/ql/src/change-notes/2026-03-19-suspicious-add-sizeof.md b/cpp/ql/src/change-notes/2026-03-19-suspicious-add-sizeof.md new file mode 100644 index 000000000000..387e2d44b469 --- /dev/null +++ b/cpp/ql/src/change-notes/2026-03-19-suspicious-add-sizeof.md @@ -0,0 +1,4 @@ +--- +category: minorAnalysis +--- +* Fixed an issue with the "Suspicious add with sizeof" (`cpp/suspicious-add-sizeof`) query causing false positive results in `build-mode: none` databases. From 92c9a8e1460d711c3ea87ab01725baa273905dce Mon Sep 17 00:00:00 2001 From: Geoffrey White <40627776+geoffw0@users.noreply.github.com> Date: Thu, 19 Mar 2026 13:51:03 +0000 Subject: [PATCH 4/4] Update cpp/ql/test/query-tests/Security/CWE/CWE-468/semmle/SuspiciousAddWithSizeof/buildless.cpp --- .../CWE/CWE-468/semmle/SuspiciousAddWithSizeof/buildless.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cpp/ql/test/query-tests/Security/CWE/CWE-468/semmle/SuspiciousAddWithSizeof/buildless.cpp b/cpp/ql/test/query-tests/Security/CWE/CWE-468/semmle/SuspiciousAddWithSizeof/buildless.cpp index bfe4f5468035..b0b590fba699 100644 --- a/cpp/ql/test/query-tests/Security/CWE/CWE-468/semmle/SuspiciousAddWithSizeof/buildless.cpp +++ b/cpp/ql/test/query-tests/Security/CWE/CWE-468/semmle/SuspiciousAddWithSizeof/buildless.cpp @@ -4,7 +4,7 @@ void test_buildless(const char *p_c, const short *p_short, const int *p_int, con *(p_c + sizeof(int)); // GOOD (`sizeof(char)` is 1) *(p_short + sizeof(int)); // BAD *(p_int + sizeof(int)); // BAD - *(p_8 + sizeof(int)); // GOOD (`sizeof(p_8)` is 1, but there's an error in the type) + *(p_8 + sizeof(int)); // GOOD (`sizeof(uint8_t)` is 1, but there's an error in the type) *(p_16 + sizeof(int)); // BAD [NOT DETECTED] *(p_32 + sizeof(int)); // BAD [NOT DETECTED] }