[all-commits] [llvm/llvm-project] 267ad4: [clang-tidy] Extend `bugprone-sizeof-expression` w...

Zoltán Porkoláb via All-commits all-commits at lists.llvm.org
Tue Sep 17 07:43:20 PDT 2024


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 267ad430fc54d6d548cd7d25c7e59c3b6b650097
      https://github.com/llvm/llvm-project/commit/267ad430fc54d6d548cd7d25c7e59c3b6b650097
  Author: Zoltán Porkoláb <zporky at gmail.com>
  Date:   2024-09-17 (Tue, 17 Sep 2024)

  Changed paths:
    M clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.cpp
    M clang-tools-extra/clang-tidy/bugprone/SizeofExpressionCheck.h
    M clang-tools-extra/clang-tidy/cert/CERTTidyModule.cpp
    M clang-tools-extra/docs/ReleaseNotes.rst
    M clang-tools-extra/docs/clang-tidy/checks/bugprone/sizeof-expression.rst
    A clang-tools-extra/docs/clang-tidy/checks/cert/arr39-c.rst
    M clang-tools-extra/docs/clang-tidy/checks/list.rst
    R clang-tools-extra/test/clang-tidy/checkers/bugprone/sizeof-expression-2.c
    A clang-tools-extra/test/clang-tidy/checkers/bugprone/sizeof-expression-pointer-arithmetics-c11.c
    A clang-tools-extra/test/clang-tidy/checkers/bugprone/sizeof-expression-pointer-arithmetics.c
    A clang-tools-extra/test/clang-tidy/checkers/bugprone/sizeof-expression.c
    M clang-tools-extra/test/clang-tidy/checkers/bugprone/sizeof-expression.cpp
    M clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines/prefer-member-initializer.cpp

  Log Message:
  -----------
  [clang-tidy] Extend `bugprone-sizeof-expression` with matching `P +- sizeof(T)` and `P +- N */ sizeof(T)` cases, add `cert-arr39-c` alias (#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 <whisperity at gmail.com>



To unsubscribe from these emails, change your notification settings at https://github.com/llvm/llvm-project/settings/notifications


More information about the All-commits mailing list