Skip to content

Conversation

@thurstond
Copy link
Contributor

@thurstond thurstond commented Oct 23, 2025

The current code may trigger a compiler warning:

address of function 'wcsnlen' will always evaluate to 'true' [-Wpointer-bool-conversion]

Fix this by comparing to nullptr. The same fix is applied to strnlen for future-proofing.

The current code may trigger a compiler warning:
```
address of function 'wcsnlen' will always evaluate to 'true' [-Wpointer-bool-conversion]
```

Fix this by casting. The same fix is applied to strnlen for
future-proofing.
@llvmbot
Copy link
Member

llvmbot commented Oct 23, 2025

@llvm/pr-subscribers-compiler-rt-sanitizer

Author: Thurston Dang (thurstond)

Changes

The current code may trigger a compiler warning:

address of function 'wcsnlen' will always evaluate to 'true' [-Wpointer-bool-conversion]

Fix this by casting. The same fix is applied to strnlen for future-proofing.


Full diff: https://github.com/llvm/llvm-project/pull/164906.diff

1 Files Affected:

  • (modified) compiler-rt/lib/asan/asan_interceptors.cpp (+2-2)
diff --git a/compiler-rt/lib/asan/asan_interceptors.cpp b/compiler-rt/lib/asan/asan_interceptors.cpp
index 0f613f0fdc30b..8643271e89d70 100644
--- a/compiler-rt/lib/asan/asan_interceptors.cpp
+++ b/compiler-rt/lib/asan/asan_interceptors.cpp
@@ -58,7 +58,7 @@ namespace __asan {
 
 static inline uptr MaybeRealStrnlen(const char *s, uptr maxlen) {
 #if SANITIZER_INTERCEPT_STRNLEN
-  if (REAL(strnlen))
+  if (static_cast<bool>(REAL(strnlen)))
     return REAL(strnlen)(s, maxlen);
 #  endif
   return internal_strnlen(s, maxlen);
@@ -66,7 +66,7 @@ static inline uptr MaybeRealStrnlen(const char *s, uptr maxlen) {
 
 static inline uptr MaybeRealWcsnlen(const wchar_t* s, uptr maxlen) {
 #  if SANITIZER_INTERCEPT_WCSNLEN
-  if (REAL(wcsnlen))
+  if (static_cast<bool>(REAL(wcsnlen)))
     return REAL(wcsnlen)(s, maxlen);
 #  endif
   return internal_wcsnlen(s, maxlen);


static inline uptr MaybeRealStrnlen(const char *s, uptr maxlen) {
#if SANITIZER_INTERCEPT_STRNLEN
if (REAL(strnlen))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not != nullptr

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm worried there might be edge cases where REAL(strnlen) is defined to be zero but not a pointer.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is that possible if we call it in the line below? You can't call an integer, and it still needs to typecheck?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Australia, you can call 000 for emergency services.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to compare to nullptr: 220c33c

@thurstond thurstond requested a review from fmayer October 23, 2025 23:02
@thurstond thurstond changed the title [asan] Avoid -Wpointer-bool-conversion warning by explicitly casting [asan] Avoid -Wpointer-bool-conversion warning by comparing to nullptr Oct 23, 2025
@thurstond thurstond enabled auto-merge (squash) October 23, 2025 23:21
@thurstond thurstond merged commit cea8ffa into llvm:main Oct 23, 2025
7 of 9 checks passed
thurstond added a commit to thurstond/llvm-project that referenced this pull request Oct 24, 2025
llvm#164906 converted bool-pointer
compare into -Wtautological-pointer-compare.
thurstond added a commit that referenced this pull request Oct 24, 2025
#164906 converted a
-Wpointer-bool-conversion warning into a -Wtautological-pointer-compare
warning. Avoid both by using the bool cast.
llvm-sync bot pushed a commit to arm/arm-toolchain that referenced this pull request Oct 24, 2025
llvm/llvm-project#164906 converted a
-Wpointer-bool-conversion warning into a -Wtautological-pointer-compare
warning. Avoid both by using the bool cast.
dvbuka pushed a commit to dvbuka/llvm-project that referenced this pull request Oct 27, 2025
llvm#164906)

The current code may trigger a compiler warning:
```
address of function 'wcsnlen' will always evaluate to 'true' [-Wpointer-bool-conversion]
```

Fix this by comparing to nullptr. The same fix is applied to strnlen for
future-proofing.
dvbuka pushed a commit to dvbuka/llvm-project that referenced this pull request Oct 27, 2025
llvm#164906 converted a
-Wpointer-bool-conversion warning into a -Wtautological-pointer-compare
warning. Avoid both by using the bool cast.
Lukacma pushed a commit to Lukacma/llvm-project that referenced this pull request Oct 29, 2025
llvm#164906)

The current code may trigger a compiler warning:
```
address of function 'wcsnlen' will always evaluate to 'true' [-Wpointer-bool-conversion]
```

Fix this by comparing to nullptr. The same fix is applied to strnlen for
future-proofing.
Lukacma pushed a commit to Lukacma/llvm-project that referenced this pull request Oct 29, 2025
llvm#164906 converted a
-Wpointer-bool-conversion warning into a -Wtautological-pointer-compare
warning. Avoid both by using the bool cast.
aokblast pushed a commit to aokblast/llvm-project that referenced this pull request Oct 30, 2025
llvm#164906)

The current code may trigger a compiler warning:
```
address of function 'wcsnlen' will always evaluate to 'true' [-Wpointer-bool-conversion]
```

Fix this by comparing to nullptr. The same fix is applied to strnlen for
future-proofing.
aokblast pushed a commit to aokblast/llvm-project that referenced this pull request Oct 30, 2025
llvm#164906 converted a
-Wpointer-bool-conversion warning into a -Wtautological-pointer-compare
warning. Avoid both by using the bool cast.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants