[libc-commits] [libc] [libc] Fix incorrect unsigned comparison (PR #135595)

Wu Yingcong via libc-commits libc-commits at lists.llvm.org
Mon Apr 14 22:10:45 PDT 2025


https://github.com/yingcong-wu updated https://github.com/llvm/llvm-project/pull/135595

>From 806f26ba23c823233bc782c4c588c11968cdebc8 Mon Sep 17 00:00:00 2001
From: "Wu, Yingcong" <yingcong.wu at intel.com>
Date: Mon, 14 Apr 2025 15:36:04 +0800
Subject: [PATCH 1/2] fix

---
 libc/src/stdio/printf_core/float_dec_converter.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/libc/src/stdio/printf_core/float_dec_converter.h b/libc/src/stdio/printf_core/float_dec_converter.h
index ee5549825a6f2..178f228527f68 100644
--- a/libc/src/stdio/printf_core/float_dec_converter.h
+++ b/libc/src/stdio/printf_core/float_dec_converter.h
@@ -192,7 +192,7 @@ template <WriteMode write_mode> class FloatWriter {
         RET_IF_RESULT_NEGATIVE(writer->write({block_buffer, digits_to_write}));
       }
       RET_IF_RESULT_NEGATIVE(writer->write(DECIMAL_POINT));
-      if (buffered_digits - digits_to_write > 0) {
+      if (buffered_digits > digits_to_write) {
         // Write the digits after the decimal point.
         RET_IF_RESULT_NEGATIVE(
             writer->write({block_buffer + digits_to_write,
@@ -222,7 +222,7 @@ template <WriteMode write_mode> class FloatWriter {
         RET_IF_RESULT_NEGATIVE(writer->write(MAX_BLOCK_DIGIT, digits_to_write));
       }
       RET_IF_RESULT_NEGATIVE(writer->write(DECIMAL_POINT));
-      if ((BLOCK_SIZE * max_block_count) - digits_to_write > 0) {
+      if ((BLOCK_SIZE * max_block_count) > digits_to_write) {
         RET_IF_RESULT_NEGATIVE(writer->write(
             MAX_BLOCK_DIGIT, (BLOCK_SIZE * max_block_count) - digits_to_write));
       }

>From 1c6c1b576a334752977cac2bee4ab13af1788e3d Mon Sep 17 00:00:00 2001
From: "Wu, Yingcong" <yingcong.wu at intel.com>
Date: Tue, 15 Apr 2025 13:10:28 +0800
Subject: [PATCH 2/2] remove redundent if

---
 libc/src/stdio/printf_core/float_dec_converter.h | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)

diff --git a/libc/src/stdio/printf_core/float_dec_converter.h b/libc/src/stdio/printf_core/float_dec_converter.h
index 178f228527f68..ed004f9a26a13 100644
--- a/libc/src/stdio/printf_core/float_dec_converter.h
+++ b/libc/src/stdio/printf_core/float_dec_converter.h
@@ -186,11 +186,10 @@ template <WriteMode write_mode> class FloatWriter {
     if (total_digits_written < digits_before_decimal &&
         total_digits_written + buffered_digits >= digits_before_decimal &&
         has_decimal_point) {
+      // digits_to_write > 0 guaranteed by outer if
       size_t digits_to_write = digits_before_decimal - total_digits_written;
-      if (digits_to_write > 0) {
-        // Write the digits before the decimal point.
-        RET_IF_RESULT_NEGATIVE(writer->write({block_buffer, digits_to_write}));
-      }
+      // Write the digits before the decimal point.
+      RET_IF_RESULT_NEGATIVE(writer->write({block_buffer, digits_to_write}));
       RET_IF_RESULT_NEGATIVE(writer->write(DECIMAL_POINT));
       if (buffered_digits > digits_to_write) {
         // Write the digits after the decimal point.
@@ -217,10 +216,9 @@ template <WriteMode write_mode> class FloatWriter {
         total_digits_written + BLOCK_SIZE * max_block_count >=
             digits_before_decimal &&
         has_decimal_point) {
+      // digits_to_write > 0 guaranteed by outer if
       size_t digits_to_write = digits_before_decimal - total_digits_written;
-      if (digits_to_write > 0) {
-        RET_IF_RESULT_NEGATIVE(writer->write(MAX_BLOCK_DIGIT, digits_to_write));
-      }
+      RET_IF_RESULT_NEGATIVE(writer->write(MAX_BLOCK_DIGIT, digits_to_write));
       RET_IF_RESULT_NEGATIVE(writer->write(DECIMAL_POINT));
       if ((BLOCK_SIZE * max_block_count) > digits_to_write) {
         RET_IF_RESULT_NEGATIVE(writer->write(



More information about the libc-commits mailing list