From 27c6f398b06b40cfc928e9164d641ecdd5b34e9d Mon Sep 17 00:00:00 2001 From: Timo Teras <> Date: Mon, 4 Jun 2012 04:56:52 -0600 Subject: [PATCH] Bug 3537: polish StatHist copy to avoid memory errors --- src/StatHist.h | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/StatHist.h b/src/StatHist.h index 05db311271..dfcc304ce6 100644 --- a/src/StatHist.h +++ b/src/StatHist.h @@ -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(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(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; } -- 2.47.2