[llvm] r185397 - [APFloat] Ensure that we can properly parse strings that do not have null terminators.

Michael Gottesman mgottesman at apple.com
Mon Jul 1 16:54:09 PDT 2013


Author: mgottesman
Date: Mon Jul  1 18:54:08 2013
New Revision: 185397

URL: http://llvm.org/viewvc/llvm-project?rev=185397&view=rev
Log:
[APFloat] Ensure that we can properly parse strings that do not have null terminators.

rdar://14323230

Modified:
    llvm/trunk/lib/Support/APFloat.cpp
    llvm/trunk/unittests/ADT/APFloatTest.cpp

Modified: llvm/trunk/lib/Support/APFloat.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/APFloat.cpp?rev=185397&r1=185396&r2=185397&view=diff
==============================================================================
--- llvm/trunk/lib/Support/APFloat.cpp (original)
+++ llvm/trunk/lib/Support/APFloat.cpp Mon Jul  1 18:54:08 2013
@@ -2481,7 +2481,14 @@ APFloat::convertFromDecimalString(String
            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;
 

Modified: llvm/trunk/unittests/ADT/APFloatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/APFloatTest.cpp?rev=185397&r1=185396&r2=185397&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/APFloatTest.cpp (original)
+++ llvm/trunk/unittests/ADT/APFloatTest.cpp Mon Jul  1 18:54:08 2013
@@ -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());





More information about the llvm-commits mailing list