[libc-commits] [libc] [libc] Fix incorrect unsigned comparison (PR #135595)
Wu Yingcong via libc-commits
libc-commits at lists.llvm.org
Mon Apr 14 00:56:13 PDT 2025
https://github.com/yingcong-wu created https://github.com/llvm/llvm-project/pull/135595
There is a problem with such unsigned comparison pattern:
```
if(unsigned_a - unsigned_b > 0) { /* only NOT go here when unsigned_a==unsigned_b */ }
```
When `unsigned_a` < `unsigned_b`, the result will still be `>0` due to underflow.
This patch fixes two of the occurrences I found.
>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] 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));
}
More information about the libc-commits
mailing list