pike.git / src / modules / Regexp / pike_regexp.c

version» Context lines:

pike.git/src/modules/Regexp/pike_regexp.c:241:    * This also means that we don't allocate space until we are sure that the    * thing really will compile successfully, and we never have to move the    * code and thus invalidate pointers into it. (Note that it has to be in    * one piece because free() must be able to free it all.)    *    * Beware that the optimization-preparation code in here knows about some    * of the structure of the compiled regexp.    */   regexp *pike_regcomp(char *exp,int excompat)   { -  register regexp *r; +  register regexp *r = NULL;    register char *scan;    register char *longest;    register ptrdiff_t len;    int flags;    short *exp2,*dest,c;       if (exp == NULL)    FAIL("NULL argument");    -  exp2=xalloc( (strlen(exp)+1) * sizeof(short) ); +  exp2=xcalloc( (strlen(exp)+1), sizeof(short) );    for ( scan=exp,dest=exp2;( c= UCHARAT(scan++)); ) {    switch (c) {    case '(':    case ')':    *dest++ = excompat ? c : c | SPECIAL;    break;    case '.':    case '*':    case '+':    case '|':
pike.git/src/modules/Regexp/pike_regexp.c:292:    case 't': *dest++ = '\t'; break;    case 'r': *dest++ = '\r'; break;    default:    *dest++ = c;    }    break;    default:    *dest++ = c;    }    } -  *dest=0; +     /* First pass: determine size, legality. */    regparse = exp2;    regnpar = 1;    regsize = 0L;    regcode = &regdummy;    if (reg(0, &flags) == NULL) -  return (NULL); +  goto exit_regcomp;       /* Small enough for pointer-storage convention? */    if (regsize >= 32767L) /* Probably could be 65535L. */ -  FAIL("regexp too big"); +  goto exit_regcomp;       /* Allocate space. */ -  r = xalloc(sizeof(regexp) + (unsigned) regsize); +  r = malloc(sizeof(regexp) + (unsigned) regsize); +  if(!r) +  { +  free(exp2); +  Pike_error(msg_out_of_mem); +  }       /* Second pass: emit code. */    regparse = exp2;    regnpar = 1;    regcode = r->program;    if (reg(0, &flags) == NULL) -  return (NULL); +  { +  free(r); +  r = NULL; +  goto exit_regcomp; +  }       /* Dig out information for optimizations. */    r->regstart = '\0'; /* Worst-case defaults. */    r->reganch = 0;    r->regmust = NULL;    r->regmlen = 0;    scan = r->program; /* First BRANCH. */    if (OP(regnext(scan)) == END) { /* Only one top-level choice. */    scan = OPERAND(scan);   
pike.git/src/modules/Regexp/pike_regexp.c:351:    for (; scan != NULL; scan = regnext(scan))    if (OP(scan) == EXACTLY &&    strlen(OPERAND(scan)) >= (size_t)len) {    longest = OPERAND(scan);    len = strlen(OPERAND(scan));    }    r->regmust = longest;    r->regmlen = len;    }    } +  +  exit_regcomp:    free(exp2);    return (r);   }      /*    - reg - regular expression, i.e. main body or parenthesized thing    *    * Caller must absorb opening parenthesis.    *    * Combining parenthesis handling with the base level of regular expression