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

Michael Jones via libc-commits libc-commits at lists.llvm.org
Thu Sep 5 10:56:39 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.
----------------
michaelrj-google wrote:

I'd suggest replacing the `volatile` stuff with just `__builtin_trap()`. It makes this more generic and makes it less dependent on optimization behavior. If you really want it to segfault, then you could do this: 
```C++
volatile char *crashing = reinterpret_cast<char*>(arg); //might also need a const_cast to be properly generic
char a = *crashing;
__builtin_trap(); //avoid the return while also making it clear to the compiler that this is a terminal path
```

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


More information about the libc-commits mailing list