Skip to content

Commit 18b48aa

Browse files
authored
Merge pull request #22933 from github/copilot/issue-22929-byte-pointer-scaling
Fix false positives for std::byte pointer scaling
2 parents baa1d3a + e701d22 commit 18b48aa

3 files changed

Lines changed: 28 additions & 1 deletion

File tree

‎cpp/ql/src/Security/CWE/CWE-468/IncorrectPointerScaling.ql‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@ where
2222
destBase = baseType(destType) and
2323
destBase.getSize() != sourceBase.getSize() and
2424
not dest.isInMacroExpansion() and
25-
// If the source type is a `char*` or `void*` then don't
25+
// If the source type is a `char*`, `void*` or `std::byte*` then don't
2626
// produce a result, because it is likely to be a false
2727
// positive.
2828
not sourceBase instanceof CharType and
2929
not sourceBase instanceof VoidType and
30+
not sourceBase.(Enum).hasQualifiedName("std", "byte") and
3031
// Low-level pointer tricks often involve casting a struct pointer to a
3132
// char pointer, then accessing it at byte offsets. For example, this can
3233
// be necessary in order to resume an interrupted `write(2)`.
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 `cpp/suspicious-pointer-scaling` query no longer reports false positives for intentional pointer arithmetic from `std::byte*` buffers.

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,3 +146,25 @@ int test18(int i) {
146146
// BAD: the pointer arithmetic uses type char*, so the offset is not scaled by sizeof(int).
147147
return *(int *)(charPointer + i); // $ Alert[cpp/incorrect-pointer-scaling-char]
148148
}
149+
150+
namespace std {
151+
enum class byte : unsigned char {};
152+
}
153+
154+
std::byte* test19(
155+
std::byte *x
156+
) {
157+
int *p = (int*)x;
158+
// GOOD: like char*, std::byte* is a generic pointer type,
159+
// so assume that it is really an int*.
160+
return (std::byte *)(p + 1);
161+
}
162+
163+
std::byte* test20(
164+
std::byte *x
165+
) {
166+
int *p = reinterpret_cast<int*>(x);
167+
// GOOD: like char*, std::byte* is a generic pointer type,
168+
// so assume that it is really an int*.
169+
return reinterpret_cast<std::byte*>(p + 1);
170+
}

0 commit comments

Comments
 (0)