Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/lib/libc/regex Use reallocarr and simplify. Document valid r...



details:   https://anonhg.NetBSD.org/src/rev/53ca7ae92ab1
branches:  trunk
changeset: 336207:53ca7ae92ab1
user:      joerg <joerg%NetBSD.org@localhost>
date:      Tue Feb 17 20:30:44 2015 +0000

description:
Use reallocarr and simplify. Document valid reallocation failures.

diffstat:

 lib/libc/regex/regcomp.c |  68 ++++++++++++++++-------------------------------
 1 files changed, 24 insertions(+), 44 deletions(-)

diffs (140 lines):

diff -r 8480cb52ffab -r 53ca7ae92ab1 lib/libc/regex/regcomp.c
--- a/lib/libc/regex/regcomp.c  Tue Feb 17 20:29:20 2015 +0000
+++ b/lib/libc/regex/regcomp.c  Tue Feb 17 20:30:44 2015 +0000
@@ -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 @@
        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 @@
        cset *cs;
        size_t css;
        size_t i;
+       void *old_ptr;
 
        _DIAGASSERT(p != NULL);
 
@@ -1248,29 +1248,18 @@
                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 @@
        cs->multis = NULL;
 
        return(cs);
+
+oomem:
+       SETERROR(REG_ESPACE);
+       /* caller's responsibility not to do set ops */
+       return NULL;
 }
 
 /*
@@ -1764,30 +1758,18 @@
  == 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 @@
        _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. */
 }
 
 /*



Home | Main Index | Thread Index | Old Index