Date: Tue, 28 Oct 2003 11:33:49 +0300 (MSK) From: "Alexander Aganichev" Sender: aaganichev AT yandex DOT ru Message-Id: <3F9E29ED.000004.02439@camay.yandex.ru> MIME-Version: 1.0 X-Mailer: Yamail [ http://yandex.ru ] To: djgpp-workers AT delorie DOT com Subject: regcomp.c realloc fix X-source-ip: 208.248.82.254 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit Reply-To: djgpp-workers AT delorie DOT com OK, let's start from fixing regexp library. The what I fixed there can be split in 5 patches, which I will post one-by-one until we fix everything. It is includes: 1) realloc fix (BTW, should we rescan current tree for unsafe realloc's again and fix everything?) 2) constant pointers patch (there are a lot of places where const pointer casted to non-const, but really only one cast is required) 3) casting char to unsigned char in isXXXX() ctype.h functions 4) removing unused functions and other warnings which are disabled now 5) fixing NLS support Here is fix for regcomp.c for accurate realloc handling: diff -rup djgpp.orig/src/libc/posix/regex/regcomp.c djgpp/src/libc/posix/regex/regcomp.c --- djgpp.orig/src/libc/posix/regex/regcomp.c 2002-10-18 04:08:00.000000000 +0000 +++ djgpp/src/libc/posix/regex/regcomp.c 2003-10-28 10:52:08.000000000 +0000 @@ -586,6 +586,9 @@ register struct parse *p; register cset *cs = allocset(p); register int invert = 0; + if (cs == NULL) + return; + /* Dept of Truly Sickening Special-Case Kludges */ if (p->next + 5 < p->end && strncmp(p->next, "[:<:]]", 6) == 0) { EMIT(OBOW, 0); @@ -1015,25 +1018,49 @@ register struct parse *p; nbytes = nc / CHAR_BIT * css; if (p->g->sets == NULL) p->g->sets = (cset *)malloc(nc * sizeof(cset)); - else - p->g->sets = (cset *)realloc((char *)p->g->sets, - nc * sizeof(cset)); - if (p->g->setbits == NULL) - p->g->setbits = (uch *)malloc(nbytes); else { - p->g->setbits = (uch *)realloc((char *)p->g->setbits, + cset *pgss = (cset *)realloc((char *)p->g->sets, + nc * sizeof(cset)); + if(pgss == NULL) { + free(p->g->sets); + p->g->sets = NULL; + } + else + p->g->sets = pgss; + } + + if (p->g->sets == NULL) { + if (p->g->setbits != NULL) { + free(p->g->setbits); + p->g->setbits = NULL; + } + } else { + if (p->g->setbits == NULL) + p->g->setbits = (uch *)malloc(nbytes); + else { + uch *pgsbs = (uch *)realloc((char *)p->g->setbits, nbytes); - /* xxx this isn't right if setbits is now NULL */ - for (i = 0; i < no; i++) - p->g->sets[i].ptr = p->g->setbits + css*(i/CHAR_BIT); + if (pgsbs != NULL) { + p->g->setbits = pgsbs; + for (i = 0; i < no; i++) + p->g->sets[i].ptr = p->g->setbits + css*(i/CHAR_BIT); + } else { + free(p->g->setbits); + p->g->setbits = NULL; + } + } + if (p->g->setbits == NULL) { + free(p->g->sets); + p->g->sets = NULL; + } } if (p->g->sets != NULL && p->g->setbits != NULL) (void) memset((char *)p->g->setbits + (nbytes - css), 0, css); else { - no = 0; SETERROR(REG_ESPACE); /* caller's responsibility not to do set ops */ + return NULL; } } @@ -1161,8 +1188,14 @@ register char *cp; cs->smultis += strlen(cp) + 1; if (cs->multis == NULL) cs->multis = malloc(cs->smultis); - else - cs->multis = realloc(cs->multis, cs->smultis); + else { + char *csm = realloc(cs->multis, cs->smultis); + if (csm == NULL) { + free(cs->multis); + cs->multis = NULL; + } else + cs->multis = csm; + } if (cs->multis == NULL) { SETERROR(REG_ESPACE); return; -- Alexander Aganichev url: http://aaganichev.narod.ru e-mail: aaganichev AT yandex DOT ru gsm: +7-095-786-1339