summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorH. Peter Anvin <hpa@linux.intel.com>2012-03-21 00:28:05 (GMT)
committerH. Peter Anvin <hpa@linux.intel.com>2012-03-21 00:28:05 (GMT)
commit96cd4cad404258337eee3cc81b43f94d7154a8cd (patch)
tree29122088440ebae762992ab5c437f0e5e384d5a4
parent93fd961527e17c1588fff7ed28058275dda4b1f9 (diff)
downloadpbn-96cd4cad404258337eee3cc81b43f94d7154a8cd.zip
pbn-96cd4cad404258337eee3cc81b43f94d7154a8cd.tar.gz
pbn-96cd4cad404258337eee3cc81b43f94d7154a8cd.tar.bz2
pbn-96cd4cad404258337eee3cc81b43f94d7154a8cd.tar.xz
pbn_init.c: correct realloc/memset use in pbn_cow()
realloc(): check the return value memset(): only do this if we actually grew the allocation
-rw-r--r--pbn_init.c12
1 files changed, 9 insertions, 3 deletions
diff --git a/pbn_init.c b/pbn_init.c
index 07d490b..5c38c15 100644
--- a/pbn_init.c
+++ b/pbn_init.c
@@ -146,19 +146,25 @@ struct pbn *pbn_dupx(const struct pbn *src, int len)
/* Returns a duplicate or clears src for reuse, consumes a reference */
struct pbn *pbn_cow(struct pbn *src, int len)
{
+ struct pbn *srcx;
int olen = src->len;
if (src->ref <= 1) {
src->ref = 1;
if (len > olen) {
- src = _pbn_realloc(src, sizeof(struct pbn)+
- (len-1)*sizeof(pbn_limb_t));
+ srcx = _pbn_realloc(src, sizeof(struct pbn)+
+ (len-1)*sizeof(pbn_limb_t));
+ if (!srcx)
+ goto dupx; /* Probably futile... */
+
+ src = srcx;
src->len = len;
+ memset(&src->num[olen], 0, (len-olen)*sizeof(pbn_limb_t));
}
- memset(&src->num[olen], 0, (len-olen)*sizeof(pbn_limb_t));
return src;
}
+dupx:
src->ref--;
return pbn_dupx(src, len);
}