[libc-commits] [libc] 9884eb1 - [libc] Fix UBSan error after D147171

Alex Brachet via libc-commits libc-commits at lists.llvm.org
Fri Mar 31 08:19:06 PDT 2023


Author: Alex Brachet
Date: 2023-03-31T15:18:35Z
New Revision: 9884eb149e612027b48fc76cebb9fbbd6e5f7513

URL: https://github.com/llvm/llvm-project/commit/9884eb149e612027b48fc76cebb9fbbd6e5f7513
DIFF: https://github.com/llvm/llvm-project/commit/9884eb149e612027b48fc76cebb9fbbd6e5f7513.diff

LOG: [libc] Fix UBSan error after D147171

Differential Revision: https://reviews.llvm.org/D147258

Added: 
    

Modified: 
    libc/src/__support/CMakeLists.txt
    libc/src/__support/str_to_integer.h

Removed: 
    


################################################################################
diff  --git a/libc/src/__support/CMakeLists.txt b/libc/src/__support/CMakeLists.txt
index 30bbf3ae96d26..51230c638cdcf 100644
--- a/libc/src/__support/CMakeLists.txt
+++ b/libc/src/__support/CMakeLists.txt
@@ -72,6 +72,7 @@ add_header_library(
     .str_to_num_result
     libc.src.errno.errno
     libc.src.__support.CPP.limits
+    libc.src.__support.CPP.type_traits
     libc.src.__support.common
 )
 

diff  --git a/libc/src/__support/str_to_integer.h b/libc/src/__support/str_to_integer.h
index 965ac11cb2173..b7af39d09bb47 100644
--- a/libc/src/__support/str_to_integer.h
+++ b/libc/src/__support/str_to_integer.h
@@ -10,6 +10,7 @@
 #define LIBC_SRC_SUPPORT_STR_TO_INTEGER_H
 
 #include "src/__support/CPP/limits.h"
+#include "src/__support/CPP/type_traits.h"
 #include "src/__support/common.h"
 #include "src/__support/ctype_utils.h"
 #include "src/__support/str_to_num_result.h"
@@ -142,7 +143,9 @@ LIBC_INLINE StrToNumResult<T> strtointeger(const char *__restrict src,
       return {cpp::numeric_limits<T>::min(), str_len, error_val};
   }
 
-  return {is_positive ? static_cast<T>(result) : -static_cast<T>(result),
+  return {is_positive
+              ? static_cast<T>(result)
+              : static_cast<T>(-static_cast<cpp::make_unsigned_t<T>>(result)),
           str_len, error_val};
 }
 


        


More information about the libc-commits mailing list