From 9884eb149e612027b48fc76cebb9fbbd6e5f7513 Mon Sep 17 00:00:00 2001 From: Alex Brachet Date: Fri, 31 Mar 2023 15:18:35 +0000 Subject: [PATCH] [libc] Fix UBSan error after D147171 Differential Revision: https://reviews.llvm.org/D147258 --- libc/src/__support/CMakeLists.txt | 1 + libc/src/__support/str_to_integer.h | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/libc/src/__support/CMakeLists.txt b/libc/src/__support/CMakeLists.txt index 30bbf3a..51230c6 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 965ac11..b7af39d 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 strtointeger(const char *__restrict src, return {cpp::numeric_limits::min(), str_len, error_val}; } - return {is_positive ? static_cast(result) : -static_cast(result), + return {is_positive + ? static_cast(result) + : static_cast(-static_cast>(result)), str_len, error_val}; } -- 2.7.4