Skip to content

clang-tidy "cppcoreguidelines-pro-bounds-pointer-arithmetic" false negative #139241

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
adesitter opened this issue May 9, 2025 · 2 comments · May be fixed by #139430
Open

clang-tidy "cppcoreguidelines-pro-bounds-pointer-arithmetic" false negative #139241

adesitter opened this issue May 9, 2025 · 2 comments · May be fixed by #139430

Comments

@adesitter
Copy link

adesitter commented May 9, 2025

❯ clang-tidy --version
LLVM (http://llvm.org/):
LLVM version 20.1.2
Optimized build.

❯ cat g.cpp

#include <span>

auto g1(std::span<int> s, std::size_t i) {
    // pointer arithmetic **not** detected
    return *(s.data() + i);
}

auto g2(std::span<int> s, std::size_t i) {
    auto p = s.data();
    // pointer arithmetic detected
    return *(p + i);
}

❯ clang-tidy "--checks=-*,cppcoreguidelines-pro-bounds-pointer-arithmetic" -quiet g.cpp -- -std=c++20 --gcc-toolchain=/usr/local/gcc-15

4 warnings generated.
.../g.cpp:11:16: warning: do not use pointer arithmetic [cppcoreguidelines-pro-bounds-pointer-arithmetic]
   11 |     return *(p + i);
      |                ^

The usage of pointer arithmetic in function g1 is not detected.

@llvmbot
Copy link
Member

llvmbot commented May 9, 2025

@llvm/issue-subscribers-clang-tidy

Author: Arnaud Desitter (adesitter)

❯ clang-tidy --version LLVM (http://llvm.org/): LLVM version 20.1.2 Optimized build.

❯ cat g.cpp
#include <span>

auto g1(std::span<int> s, std::size_t i) {
// pointer arithmetic not detected
return *(s.data() + i);
}

auto g2(std::span<int> s, std::size_t i) {
auto p = s.data();
// pointer arithmetic detected
return *(p + i);
}

❯ clang-tidy "--checks=-*,cppcoreguidelines-pro-bounds-pointer-arithmetic" -quiet g.cpp -- -std=c++20 --gcc-toolchain=/usr/local/gcc-15
4 warnings generated.
.../g.cpp:11:16: warning: do not use pointer arithmetic [cppcoreguidelines-pro-bounds-pointer-arithmetic]
11 | return *(p + i);
| ^

@adesitter
Copy link
Author

For comparison:

❯ clang++ -std=c++20 -c g.cpp --gcc-toolchain=/usr/local/gcc-15 -Wunsafe-buffer-usage
g.cpp:5:16: warning: unsafe pointer arithmetic [-Wunsafe-buffer-usage]
    5 |     return *(s.data() + i);
      |              ~~^~~~~~
g.cpp:5:16: note: pass -fsafe-buffer-usage-suggestions to receive code hardening suggestions
g.cpp:11:14: warning: unsafe pointer arithmetic [-Wunsafe-buffer-usage]
   11 |     return *(p + i);
      |              ^
g.cpp:11:14: note: pass -fsafe-buffer-usage-suggestions to receive code hardening suggestions
2 warnings generated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants