From f6072d060e2e5a9dae8ae3e30dd9d3eb7451d046 Mon Sep 17 00:00:00 2001 From: joerg Date: Tue, 17 Feb 2015 20:30:44 +0000 Subject: [PATCH] Use reallocarr and simplify. Document valid reallocation failures. --- lib/libc/regex/regcomp.c | 68 ++++++++++++++-------------------------- 1 file changed, 24 insertions(+), 44 deletions(-) diff --git a/lib/libc/regex/regcomp.c b/lib/libc/regex/regcomp.c index d4e30c1bba30..3d580091f821 100644 --- a/lib/libc/regex/regcomp.c +++ b/lib/libc/regex/regcomp.c @@ -1,4 +1,4 @@ -/* $NetBSD: regcomp.c,v 1.34 2015/02/05 16:04:35 christos Exp $ */ +/* $NetBSD: regcomp.c,v 1.35 2015/02/17 20:30:44 joerg Exp $ */ /*- * Copyright (c) 1992, 1993, 1994 @@ -76,12 +76,11 @@ #if 0 static char sccsid[] = "@(#)regcomp.c 8.5 (Berkeley) 3/20/94"; #else -__RCSID("$NetBSD: regcomp.c,v 1.34 2015/02/05 16:04:35 christos Exp $"); +__RCSID("$NetBSD: regcomp.c,v 1.35 2015/02/17 20:30:44 joerg Exp $"); #endif #endif /* LIBC_SCCS and not lint */ #include "namespace.h" -#include #include #include @@ -267,7 +266,7 @@ regcomp( if (g == NULL) return(REG_ESPACE); p->ssize = len/(size_t)2*(size_t)3 + (size_t)1; /* ugh */ - p->strip = reallocarray(NULL, p->ssize, sizeof(sop)); + p->strip = calloc(p->ssize, sizeof(sop)); p->slen = 0; if (p->strip == NULL) { free(g); @@ -1236,6 +1235,7 @@ allocset( cset *cs; size_t css; size_t i; + void *old_ptr; _DIAGASSERT(p != NULL); @@ -1248,29 +1248,18 @@ allocset( nbytes = nc / CHAR_BIT * css; if (MEMSIZE(p) > MEMLIMIT) goto oomem; - if (p->g->sets == NULL) - p->g->sets = reallocarray(NULL, nc, sizeof(cset)); - else - p->g->sets = reallocarray(p->g->sets, nc, sizeof(cset)); - if (p->g->setbits == NULL) - p->g->setbits = malloc(nbytes); - else { - p->g->setbits = realloc(p->g->setbits, nbytes); - if (p->g->setbits == NULL) - goto oomem; + if (reallocarr(&p->g->sets, nc, sizeof(cset))) + goto oomem; + old_ptr = p->g->setbits; + if (reallocarr(&p->g->setbits, nc / CHAR_BIT, css)) { + free(old_ptr); + goto oomem; + } + if (old_ptr != p->g->setbits) { for (i = 0; i < no; i++) p->g->sets[i].ptr = p->g->setbits + css*(i/CHAR_BIT); } - if (p->g->sets != NULL && p->g->setbits != NULL) - (void) memset((char *)p->g->setbits + (nbytes - css), - 0, css); - else { -oomem: - no = 0; - SETERROR(REG_ESPACE); - /* caller's responsibility not to do set ops */ - return NULL; - } + (void) memset((char *)p->g->setbits + (nbytes - css), 0, css); } cs = &p->g->sets[no]; @@ -1281,6 +1270,11 @@ oomem: cs->multis = NULL; return(cs); + +oomem: + SETERROR(REG_ESPACE); + /* caller's responsibility not to do set ops */ + return NULL; } /* @@ -1764,30 +1758,18 @@ dofwd( == static void enlarge(struct parse *p, sopno size); */ static int -enlarge( - struct parse *p, - sopno size) +enlarge(struct parse *p, sopno size) { - sop *sp; - sopno osize; - _DIAGASSERT(p != NULL); if (p->ssize >= size) return 1; - osize = p->ssize; - p->ssize = size; - if (MEMSIZE(p) > MEMLIMIT) - goto oomem; - sp = reallocarray(p->strip, p->ssize, sizeof(sop)); - if (sp == NULL) { -oomem: - p->ssize = osize; + if (MEMSIZE(p) > MEMLIMIT || reallocarr(&p->strip, size, sizeof(sop))) { SETERROR(REG_ESPACE); return 0; } - p->strip = sp; + p->ssize = size; return 1; } @@ -1805,11 +1787,9 @@ stripsnug( _DIAGASSERT(g != NULL); g->nstates = p->slen; - g->strip = reallocarray(p->strip, p->slen, sizeof(sop)); - if (g->strip == NULL) { - SETERROR(REG_ESPACE); - g->strip = p->strip; - } + g->strip = p->strip; + reallocarr(&g->strip, p->slen, sizeof(sop)); + /* Ignore error as tries to free memory only. */ } /*