[libc-commits] [libc] [libc] Fix undefined behavior for nan functions. (PR #106468)

via libc-commits libc-commits at lists.llvm.org
Fri Aug 30 17:31:41 PDT 2024


================
@@ -1208,6 +1210,15 @@ template <class T> LIBC_INLINE StrToNumResult<T> strtonan(const char *arg) {
   using FPBits = typename fputil::FPBits<T>;
   using StorageType = typename FPBits::StorageType;
 
+#ifndef LIBC_HAS_SANITIZER
+  if (LIBC_UNLIKELY(arg == nullptr)) {
+    // Use volatile to prevent undefined behavior of dereferencing nullptr.
----------------
lntue wrote:

No, the main purpose is not for the test, but hardening user code when sanitizers are not in used.  This is especially important when this header function is included and built from source, as the compiler can detect undefined behavior and optimize the code away completely.  The explicit check is for performance purpose, since you will not want everything to always be round-tripped through the volatile variable.

https://github.com/llvm/llvm-project/pull/106468


More information about the libc-commits mailing list