Skip to content

Commit ebae47d

Browse files
jketemaCopilot
andcommitted
Exclude std::byte buffers from suspicious pointer scaling
Treat std::byte source pointers like char and void buffers when checking pointer scaling, avoiding false positives for intentional typed indexing. Match only the standard byte enum and update both cast-style tests to expect no alerts. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 73542ac commit ebae47d

3 files changed

Lines changed: 8 additions & 9 deletions

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: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
| test.cpp:50:19:50:19 | p | This pointer might have type $@ (size 8), but this pointer arithmetic is done with type int * (size 4). | test.cpp:45:11:45:11 | test.cpp:45:11:45:11 | double |
22
| test.cpp:94:18:94:18 | x | This pointer might have type $@ (size 4), but this pointer arithmetic is done with type short * (size 2). | test.cpp:88:21:88:21 | test.cpp:88:21:88:21 | int |
33
| test.cpp:130:27:130:29 | arr | This pointer might have type $@ (size 4), but this pointer arithmetic is done with type short * (size 2). | test.cpp:128:16:128:18 | test.cpp:128:16:128:18 | int |
4-
| test.cpp:160:24:160:24 | p | This pointer might have type $@ (size 1), but this pointer arithmetic is done with type int * (size 4). | test.cpp:155:14:155:14 | test.cpp:155:14:155:14 | byte |
5-
| test.cpp:169:39:169:39 | p | This pointer might have type $@ (size 1), but this pointer arithmetic is done with type int * (size 4). | test.cpp:164:14:164:14 | test.cpp:164:14:164:14 | byte |

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,16 +155,16 @@ std::byte* test19(
155155
std::byte *x
156156
) {
157157
int *p = (int*)x;
158-
// GOOD [FALSE POSITIVE cpp/suspicious-pointer-scaling]: like char*, std::byte*
159-
// is a generic pointer type, so assume that it is really an int*.
160-
return (std::byte *)(p + 1); // $ Alert[cpp/suspicious-pointer-scaling]
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);
161161
}
162162

163163
std::byte* test20(
164164
std::byte *x
165165
) {
166166
int *p = reinterpret_cast<int*>(x);
167-
// GOOD [FALSE POSITIVE cpp/suspicious-pointer-scaling]: like char*, std::byte*
168-
// is a generic pointer type, so assume that it is really an int*.
169-
return reinterpret_cast<std::byte*>(p + 1); // $ Alert[cpp/suspicious-pointer-scaling]
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);
170170
}

0 commit comments

Comments
 (0)