[mathicgb] 41/393: Fixed a bug where hash values are stored as exponents but calculated with as size_t which was causing an ASSERT if those types are different. Also, made allocMonomial fill the returned buffer full of set bits to make it more likely for asserts to trigger on uninitilized values.

Doug Torrance dtorrance-guest at moszumanska.debian.org
Fri Apr 3 15:58:30 UTC 2015


This is an automated email from the git hooks/post-receive script.

dtorrance-guest pushed a commit to branch upstream
in repository mathicgb.

commit 1843f48cf9fed179a3ffb59d4ae3a31e78ef7c70
Author: Bjarke Hammersholt Roune <bjarkehr.code at gmail.com>
Date:   Tue Oct 2 11:20:33 2012 +0200

    Fixed a bug where hash values are stored as exponents but calculated with as size_t which was causing an ASSERT if those types are different. Also, made allocMonomial fill the returned buffer full of set bits to make it more likely for asserts to trigger on uninitilized values.
---
 src/mathicgb/PolyRing.hpp | 43 ++++++++++++++++++++++++++++++-------------
 1 file changed, 30 insertions(+), 13 deletions(-)

diff --git a/src/mathicgb/PolyRing.hpp b/src/mathicgb/PolyRing.hpp
index 9b94c3c..89dfbc9 100755
--- a/src/mathicgb/PolyRing.hpp
+++ b/src/mathicgb/PolyRing.hpp
@@ -219,16 +219,23 @@ public:
   void printRingFrobbyM2Format(std::ostream& out) const;
 
   //  Allocate a monomial from an arena A
-  //  This monomial may only be freed if no other elements have
-  //  been allocated on A.  In this case, use freeMonomial(A,m) to free 'm'.
+  //  This monomial may only be freed if no other elements that were allocated
+  // later are live on A.  In this case, use freeMonomial(A,m) to free 'm'.
   Monomial allocMonomial(memt::Arena &A) const {
-    return static_cast<exponent *>(A.alloc(mMaxMonomialByteSize));
+    exponent* ptr = static_cast<exponent*>(A.alloc(mMaxMonomialByteSize));
+#ifdef MATHICGB_DEBUG
+    // fill with value that do not make sense to catch bugs in debug
+    // mode. The maximum value of setting all bits increases the
+    // chances of getting an assert.
+    std::fill_n(reinterpret_cast<char*>(ptr), mMaxMonomialByteSize,
+                ~static_cast<char>(0));
+#endif
+    return ptr;
   }
 
-  // Free monomial 'm' obtained by allocMonomial(A) 
-  // by calling freeMonomial(A,m)
-  // Recall this only works if this was the last thing 
-  // allocated in A.
+  // Free monomial 'm' obtained by allocMonomial(A) by calling
+  // freeMonomial(A,m) Recall this only works if this was the last
+  // thing allocated in A.
   void freeTopMonomial(memt::Arena &A, Monomial m) const {
     A.freeTop(m.unsafeGetRepresentation());
   }
@@ -237,7 +244,15 @@ public:
   //   maxMonomialByteSize()
   //  Free monomials here using the SAME pool
   Monomial allocMonomial(memt::BufferPool &P) const {
-    return static_cast<exponent *>(P.alloc());
+    exponent* ptr = static_cast<exponent*>(P.alloc());
+#ifdef MATHICGB_DEBUG
+    // fill with value that do not make sense to catch bugs in debug
+    // mode. The maximum value of setting all bits increases the
+    // chances of getting an assert.
+    std::fill_n(reinterpret_cast<char*>(ptr), mMaxMonomialByteSize,
+                ~static_cast<char>(0));
+#endif
+    return ptr;
   }
 
   // Free monomial 'm' obtained by allocMonomial(P) 
@@ -299,7 +314,7 @@ public:
 
   void displayHashValues() const;
 
-  size_t monomialHashIndex() const { return mHashIndex; }
+  exponent monomialHashIndex() const { return mHashIndex; }
 
   ///////////////////////////////////////////
   // Monomial Routines //////////////////////
@@ -531,7 +546,7 @@ public:
   void resetCoefficientStats() const;
 
 private:
-  inline size_t computeHashValue(const_monomial a1) const;
+  inline exponent computeHashValue(const_monomial a1) const;
 
   long mCharac; // p=mCharac: ring is ZZ/p
   size_t mNumVars;
@@ -563,11 +578,13 @@ inline bool PolyRing::monomialEQ(ConstMonomial a, ConstMonomial b) const
 }
 
 inline void PolyRing::monomialMult(ConstMonomial a, 
-                            ConstMonomial b, 
-                            Monomial &result) const
+                                   ConstMonomial b, 
+                                   Monomial &result) const
 {
   for (size_t i = mHashIndex; i != static_cast<size_t>(-1); --i)
     result[i] = a[i] + b[i];
+  MATHICGB_ASSERT(computeHashValue(result) ==
+                  computeHashValue(a) + computeHashValue(b));
 
 #if 0
   // testing different things to see if we can speed it up further.
@@ -597,7 +614,7 @@ inline void PolyRing::setWeightsOnly(Monomial& a1) const
     }
 }
 
-inline size_t PolyRing::computeHashValue(const_monomial a1) const {
+inline exponent PolyRing::computeHashValue(const_monomial a1) const {
   const exponent* a = a1.unsafeGetRepresentation();
   int hash = *a;
   a++;

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/mathicgb.git



More information about the debian-science-commits mailing list