Use reallocarr and simplify. Document valid reallocation failures.

This commit is contained in:
joerg 2015-02-17 20:30:44 +00:00
parent c9baa5968d
commit f6072d060e
1 changed files with 24 additions and 44 deletions

View File

@ -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 <sys/param.h>
#include <sys/types.h>
#include <assert.h>
@ -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. */
}
/*