]> git.ipfire.org Git - thirdparty/squid.git/commitdiff
Bug 3537: polish StatHist copy to avoid memory errors
authorTimo Teras <>
Mon, 4 Jun 2012 10:56:52 +0000 (04:56 -0600)
committerAmos Jeffries <squid3@treenet.co.nz>
Mon, 4 Jun 2012 10:56:52 +0000 (04:56 -0600)
src/StatHist.h

index 05db3112715e934bfb12e22a51e009cceb9497c2..dfcc304ce6ee9467ceb3197da228168bd293a4eb 100644 (file)
@@ -130,15 +130,18 @@ StatHist::operator =(const StatHist & src)
 {
     if (this==&src) //handle self-assignment
         return *this;
-    xfree(bins); // xfree can handle NULL pointers, no need to check
-    capacity_=src.capacity_;
-    bins = static_cast<bins_type *>(xcalloc(src.capacity_, sizeof(bins_type)));
+    if (capacity_ != src.capacity_) {
+        xfree(bins); // xfree can handle NULL pointers, no need to check
+        capacity_=src.capacity_;
+        bins = static_cast<bins_type *>(xcalloc(src.capacity_, sizeof(bins_type)));
+    }
     min_=src.min_;
     max_=src.max_;
     scale_=src.scale_;
     val_in=src.val_in;
     val_out=src.val_out;
-    memcpy(bins,src.bins,capacity_*sizeof(*bins));
+    if (bins != NULL)
+        memcpy(bins,src.bins,capacity_*sizeof(*bins));
     return *this;
 }