forked from llvm/llvm-project
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[clang-tidy] Extend
bugprone-sizeof-expression
with matching `P +- …
…sizeof(T)` and `P +- N */ sizeof(T)` cases, add `cert-arr39-c` alias (llvm#106061) Improved `bugprone-sizeof-expression` check to find suspicious pointer arithmetic calculations where the pointer is offset by an `alignof()`, `offsetof()`, or `sizeof()` expression. Pointer arithmetic expressions implicitly scale the offset added to or subtracted from the address by the size of the pointee type. Using an offset expression that is already scaled by the size of the underlying type effectively results in a squared offset, which is likely an invalid pointer that points beyond the end of the intended array. ```c void printEveryEvenIndexElement(int *Array, size_t N) { int *P = Array; while (P <= Array + N * sizeof(int)) { // Suspicious pointer arithmetics using sizeof()! printf("%d ", *P); P += 2 * sizeof(int); // Suspicious pointer arithmetics using sizeof()! } } ``` --------- Co-authored-by: Whisperity <[email protected]>
- Loading branch information
1 parent
f4172f6
commit 267ad43
Showing
12 changed files
with
649 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
.. title:: clang-tidy - cert-arr39-c | ||
.. meta:: | ||
:http-equiv=refresh: 5;URL=../bugprone/sizeof-expression.html | ||
|
||
cert-arr39-c | ||
============ | ||
|
||
The `cert-arr39-c` check is an alias, please see | ||
:doc:`bugprone-sizeof-expression <../bugprone/sizeof-expression>` | ||
for more information. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
...tools-extra/test/clang-tidy/checkers/bugprone/sizeof-expression-pointer-arithmetics-c11.c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// RUN: %check_clang_tidy -std=c11-or-later %s bugprone-sizeof-expression %t | ||
|
||
#define alignof(type_name) _Alignof(type_name) | ||
extern void sink(const void *P); | ||
|
||
enum { BufferSize = 1024 }; | ||
|
||
struct S { | ||
long A, B, C; | ||
}; | ||
|
||
void bad4d(void) { | ||
struct S Buffer[BufferSize]; | ||
|
||
struct S *P = &Buffer[0]; | ||
struct S *Q = P; | ||
while (Q < P + alignof(Buffer)) { | ||
// CHECK-MESSAGES: :[[@LINE-1]]:16: warning: suspicious usage of 'alignof(...)' in pointer arithmetic; this scaled value will be scaled again by the '+' operator [bugprone-sizeof-expression] | ||
// CHECK-MESSAGES: :[[@LINE-2]]:16: note: '+' in pointer arithmetic internally scales with 'sizeof(struct S)' == {{[0-9]+}} | ||
sink(Q++); | ||
} | ||
} |
Oops, something went wrong.