From b57770387ab262a0a23e77f094f599b6c955ba14 Mon Sep 17 00:00:00 2001 From: Michael Gottesman Date: Mon, 1 Jul 2013 23:54:08 +0000 Subject: [PATCH] [APFloat] Ensure that we can properly parse strings that do not have null terminators. rdar://14323230 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@185397 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Support/APFloat.cpp | 9 ++++++++- unittests/ADT/APFloatTest.cpp | 25 +++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/lib/Support/APFloat.cpp b/lib/Support/APFloat.cpp index a7ff9f68cf7..ae4a101feba 100644 --- a/lib/Support/APFloat.cpp +++ b/lib/Support/APFloat.cpp @@ -2481,7 +2481,14 @@ APFloat::convertFromDecimalString(StringRef str, roundingMode rounding_mode) 42039/12655 < L < 28738/8651 [ numerator <= 65536 ] */ - if (decDigitValue(*D.firstSigDigit) >= 10U) { + // Test if we have a zero number allowing for strings with no null terminators + // and zero decimals with non-zero exponents. + // + // We computed firstSigDigit by ignoring all zeros and dots. Thus if + // D->firstSigDigit equals str.end(), every digit must be a zero and there can + // be at most one dot. On the other hand, if we have a zero with a non-zero + // exponent, then we know that D.firstSigDigit will be non-numeric. + if (decDigitValue(*D.firstSigDigit) >= 10U || D.firstSigDigit == str.end()) { category = fcZero; fs = opOK; diff --git a/unittests/ADT/APFloatTest.cpp b/unittests/ADT/APFloatTest.cpp index d322bcdeacb..46dfbd1d13a 100644 --- a/unittests/ADT/APFloatTest.cpp +++ b/unittests/ADT/APFloatTest.cpp @@ -538,6 +538,31 @@ TEST(APFloatTest, Zero) { EXPECT_TRUE(APFloat(-0.0).isNegative()); } +TEST(APFloatTest, DecimalStringsWithoutNullTerminators) { + // Make sure that we can parse strings without null terminators. + // rdar://14323230. + APFloat Val(APFloat::IEEEdouble); + Val.convertFromString(StringRef("0.00", 3), + llvm::APFloat::rmNearestTiesToEven); + EXPECT_EQ(Val.convertToDouble(), 0.0); + Val.convertFromString(StringRef("0.01", 3), + llvm::APFloat::rmNearestTiesToEven); + EXPECT_EQ(Val.convertToDouble(), 0.0); + Val.convertFromString(StringRef("0.09", 3), + llvm::APFloat::rmNearestTiesToEven); + EXPECT_EQ(Val.convertToDouble(), 0.0); + Val.convertFromString(StringRef("0.095", 4), + llvm::APFloat::rmNearestTiesToEven); + EXPECT_EQ(Val.convertToDouble(), 0.09); + Val.convertFromString(StringRef("0.00e+3", 7), + llvm::APFloat::rmNearestTiesToEven); + EXPECT_EQ(Val.convertToDouble(), 0.00); + Val.convertFromString(StringRef("0e+3", 4), + llvm::APFloat::rmNearestTiesToEven); + EXPECT_EQ(Val.convertToDouble(), 0.00); + +} + TEST(APFloatTest, fromZeroDecimalString) { EXPECT_EQ( 0.0, APFloat(APFloat::IEEEdouble, "0").convertToDouble()); EXPECT_EQ(+0.0, APFloat(APFloat::IEEEdouble, "+0").convertToDouble()); -- 2.11.0