diff --git a/libcxxabi/src/demangle/Utility.h b/libcxxabi/src/demangle/Utility.h index 4ec821cf6da9..21e5efb18542 100644 --- a/libcxxabi/src/demangle/Utility.h +++ b/libcxxabi/src/demangle/Utility.h @@ -17,7 +17,6 @@ #define DEMANGLE_UTILITY_H #include "StringView.h" -#include #include #include #include @@ -40,8 +39,11 @@ class OutputBuffer { if (Need > BufferCapacity) { // Avoid many reallocations during startup, with a bit of hysteresis. constexpr size_t MinInitAlloc = 1024; - Need = std::max(Need, MinInitAlloc); - BufferCapacity = std::max(Need, BufferCapacity * 2); + if (Need < MinInitAlloc) + Need = MinInitAlloc; + BufferCapacity *= 2; + if (BufferCapacity < Need) + BufferCapacity = Need; Buffer = static_cast(std::realloc(Buffer, BufferCapacity)); if (Buffer == nullptr) std::terminate(); diff --git a/llvm/include/llvm/Demangle/Utility.h b/llvm/include/llvm/Demangle/Utility.h index df520f5cbc96..c952068a19dd 100644 --- a/llvm/include/llvm/Demangle/Utility.h +++ b/llvm/include/llvm/Demangle/Utility.h @@ -39,8 +39,11 @@ class OutputBuffer { if (Need > BufferCapacity) { // Avoid many reallocations during startup, with a bit of hysteresis. constexpr size_t MinInitAlloc = 1024; - Need = std::max(Need, MinInitAlloc); - BufferCapacity = std::max(Need, BufferCapacity * 2); + if (Need < MinInitAlloc) + Need = MinInitAlloc; + BufferCapacity *= 2; + if (BufferCapacity < Need) + BufferCapacity = Need; Buffer = static_cast(std::realloc(Buffer, BufferCapacity)); if (Buffer == nullptr) std::terminate();