[llvm] r285468 - [APFloat] Fix memory bugs revealed by MSan

Tim Shen via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 28 15:45:34 PDT 2016


Author: timshen
Date: Fri Oct 28 17:45:33 2016
New Revision: 285468

URL: http://llvm.org/viewvc/llvm-project?rev=285468&view=rev
Log:
[APFloat] Fix memory bugs revealed by MSan

Reviewers: eugenis, hfinkel, kbarton, iteratee, echristo

Subscribers: mehdi_amini, llvm-commits

Differential Revision: https://reviews.llvm.org/D26102

Modified:
    llvm/trunk/include/llvm/ADT/APFloat.h
    llvm/trunk/lib/Support/APFloat.cpp

Modified: llvm/trunk/include/llvm/ADT/APFloat.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/APFloat.h?rev=285468&r1=285467&r2=285468&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/APFloat.h (original)
+++ llvm/trunk/include/llvm/ADT/APFloat.h Fri Oct 28 17:45:33 2016
@@ -777,6 +777,7 @@ public:
   APFloat(const fltSemantics &Semantics) : U(Semantics) {}
   APFloat(const fltSemantics &Semantics, StringRef S);
   APFloat(const fltSemantics &Semantics, integerPart I) : U(Semantics, I) {}
+  // TODO: Remove this constructor. This isn't faster than the first one.
   APFloat(const fltSemantics &Semantics, uninitializedTag)
       : U(Semantics, uninitialized) {}
   APFloat(const fltSemantics &Semantics, const APInt &I) : U(Semantics, I) {}

Modified: llvm/trunk/lib/Support/APFloat.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/APFloat.cpp?rev=285468&r1=285467&r2=285468&view=diff
==============================================================================
--- llvm/trunk/lib/Support/APFloat.cpp (original)
+++ llvm/trunk/lib/Support/APFloat.cpp Fri Oct 28 17:45:33 2016
@@ -818,7 +818,10 @@ IEEEFloat::IEEEFloat(const fltSemantics
   sign = false;
 }
 
-IEEEFloat::IEEEFloat(const fltSemantics &ourSemantics, uninitializedTag tag) {
+// Delegate to the previous constructor, because later copy constructor may
+// actually inspects category, which can't be garbage.
+IEEEFloat::IEEEFloat(const fltSemantics &ourSemantics, uninitializedTag tag)
+    : IEEEFloat(ourSemantics) {
   // Allocates storage if necessary but does not initialize it.
   initialize(&ourSemantics);
 }
@@ -3877,7 +3880,9 @@ DoubleAPFloat::DoubleAPFloat(const fltSe
 
 DoubleAPFloat::DoubleAPFloat(const DoubleAPFloat &RHS)
     : Semantics(RHS.Semantics),
-      Floats(new APFloat[2]{APFloat(RHS.Floats[0]), APFloat(RHS.Floats[1])}) {
+      Floats(RHS.Floats ? new APFloat[2]{APFloat(RHS.Floats[0]),
+                                         APFloat(RHS.Floats[1])}
+                        : nullptr) {
   assert(Semantics == &PPCDoubleDouble);
 }
 
@@ -3888,7 +3893,7 @@ DoubleAPFloat::DoubleAPFloat(DoubleAPFlo
 }
 
 DoubleAPFloat &DoubleAPFloat::operator=(const DoubleAPFloat &RHS) {
-  if (Semantics == RHS.Semantics) {
+  if (Semantics == RHS.Semantics && RHS.Floats) {
     Floats[0] = RHS.Floats[0];
     Floats[1] = RHS.Floats[1];
   } else if (this != &RHS) {




More information about the llvm-commits mailing list