[libc-commits] [libc] 6812bc4 - [libc] Fix off by one in long double buffer size (#80889)

via libc-commits libc-commits at lists.llvm.org
Tue Feb 6 11:16:16 PST 2024


Author: michaelrj-google
Date: 2024-02-06T11:16:12-08:00
New Revision: 6812bc40bd6c70f7cae10d1e0a2aeac31cfaef03

URL: https://github.com/llvm/llvm-project/commit/6812bc40bd6c70f7cae10d1e0a2aeac31cfaef03
DIFF: https://github.com/llvm/llvm-project/commit/6812bc40bd6c70f7cae10d1e0a2aeac31cfaef03.diff

LOG: [libc] Fix off by one in long double buffer size (#80889)

The size for the long double BLOCK_BUFFER_LEN is calculated based on the
properties of the long double type. Somewhere in the calculation, the
result was mis-rounded so that the buffer was one element too small.
This patch fixes the issue and adds asserts to catch it sooner in the
future.

Added: 
    

Modified: 
    libc/src/__support/float_to_string.h

Removed: 
    


################################################################################
diff  --git a/libc/src/__support/float_to_string.h b/libc/src/__support/float_to_string.h
index 1431aeffa5b21..f30110d47b219 100644
--- a/libc/src/__support/float_to_string.h
+++ b/libc/src/__support/float_to_string.h
@@ -651,7 +651,8 @@ template <> class FloatToString<long double> {
   int int_block_index = 0;
 
   static constexpr size_t BLOCK_BUFFER_LEN =
-      internal::div_ceil(internal::log10_pow2(FLOAT_AS_INT_WIDTH), BLOCK_SIZE);
+      internal::div_ceil(internal::log10_pow2(FLOAT_AS_INT_WIDTH), BLOCK_SIZE) +
+      1;
   BlockInt block_buffer[BLOCK_BUFFER_LEN] = {0};
   size_t block_buffer_valid = 0;
 
@@ -693,6 +694,7 @@ template <> class FloatToString<long double> {
       int_block_index = 0;
 
       while (float_as_int > 0) {
+        LIBC_ASSERT(int_block_index < static_cast<int>(BLOCK_BUFFER_LEN));
         block_buffer[int_block_index] = grab_digits(float_as_int);
         ++int_block_index;
       }
@@ -785,6 +787,8 @@ template <> class FloatToString<long double> {
     if (block_index > static_cast<int>(block_buffer_valid) || block_index < 0)
       return 0;
 
+    LIBC_ASSERT(block_index < static_cast<int>(BLOCK_BUFFER_LEN));
+
     return block_buffer[block_index];
   }
 


        


More information about the libc-commits mailing list