[libc-commits] [PATCH] D119156: [libc] Fix mixed-sign comparison warnings and an out-of-bound access in high_precision_decimal.h
Tue Ly via Phabricator via libc-commits
libc-commits at lists.llvm.org
Mon Feb 7 09:40:15 PST 2022
lntue created this revision.
lntue added reviewers: michaelrj, sivachandra.
Herald added subscribers: ecnelises, tschuett.
Herald added a project: libc-project.
lntue requested review of this revision.
Fix mixed-sign comparison warnings and an out-of-bound access in high_precision_decimal.h
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D119156
Files:
libc/src/__support/high_precision_decimal.h
libc/test/src/__support/high_precision_decimal_test.cpp
Index: libc/test/src/__support/high_precision_decimal_test.cpp
===================================================================
--- libc/test/src/__support/high_precision_decimal_test.cpp
+++ libc/test/src/__support/high_precision_decimal_test.cpp
@@ -36,6 +36,17 @@
EXPECT_EQ(hpd.get_decimal_point(), 1);
}
+TEST(LlvmLibcHighPrecisionDecimalTest, ShouldRoundup) {
+ __llvm_libc::internal::HighPrecisionDecimal hpd =
+ __llvm_libc::internal::HighPrecisionDecimal(".5");
+ uint8_t *digits = hpd.get_digits();
+
+ EXPECT_EQ(digits[0], uint8_t(5));
+ EXPECT_EQ(hpd.get_num_digits(), 1u);
+ EXPECT_EQ(hpd.get_decimal_point(), 0);
+ EXPECT_EQ(hpd.round_to_integer_type<int>(), 0);
+}
+
TEST(LlvmLibcHighPrecisionDecimalTest, SmallShift) {
__llvm_libc::internal::HighPrecisionDecimal hpd =
__llvm_libc::internal::HighPrecisionDecimal("1.2345");
Index: libc/src/__support/high_precision_decimal.h
===================================================================
--- libc/src/__support/high_precision_decimal.h
+++ libc/src/__support/high_precision_decimal.h
@@ -112,13 +112,14 @@
private:
bool should_round_up(int32_t roundToDigit) {
- if (roundToDigit < 0 || roundToDigit >= this->num_digits) {
+ if (roundToDigit < 0 ||
+ static_cast<uint32_t>(roundToDigit) >= this->num_digits) {
return false;
}
// If we're right in the middle and there are no extra digits
if (this->digits[roundToDigit] == 5 &&
- roundToDigit + 1 == this->num_digits) {
+ static_cast<uint32_t>(roundToDigit + 1) == this->num_digits) {
// Round up if we've truncated (since that means the result is slightly
// higher than what's represented.)
@@ -127,6 +128,9 @@
}
// If this exactly halfway, round to even.
+ if (roundToDigit == 0)
+ // When the input is ".5".
+ return false;
return this->digits[roundToDigit - 1] % 2 != 0;
}
// If there are digits after roundToDigit, they must be non-zero since we
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D119156.406508.patch
Type: text/x-patch
Size: 2026 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libc-commits/attachments/20220207/a4e8f623/attachment.bin>
More information about the libc-commits
mailing list