Index: usr.bin/grep/regex/tre-fastmatch.c =================================================================== --- usr.bin/grep/regex/tre-fastmatch.c +++ usr.bin/grep/regex/tre-fastmatch.c @@ -46,6 +46,8 @@ static int fastcmp(const fastmatch_t *fg, const void *data, tre_str_type_t type); +static tre_char_t *tre_strndup(const tre_char_t *src, size_t len); + /* * Clean up if pattern compilation fails. */ @@ -351,7 +353,7 @@ #define FILL_BMGS \ if (fg->len > 0 && !fg->hasdot) \ { \ - fg->sbmGs = malloc(fg->len * sizeof(int)); \ + fg->sbmGs = malloc(fg->len * sizeof(*fg->sbmGs)); \ if (!fg->sbmGs) \ return REG_ESPACE; \ if (fg->len == 1) \ @@ -367,7 +369,7 @@ #define FILL_BMGS_WIDE \ if (fg->wlen > 0 && !fg->hasdot) \ { \ - fg->bmGs = malloc(fg->wlen * sizeof(int)); \ + fg->bmGs = malloc(fg->wlen * sizeof(*fg->bmGs)); \ if (!fg->bmGs) \ return REG_ESPACE; \ if (fg->wlen == 1) \ @@ -455,19 +457,6 @@ } /* - * Copies the pattern pat having length n to p and stores - * the size in l. - */ -#define SAVE_PATTERN(src, srclen, dst, dstlen) \ - dstlen = srclen; \ - dst = malloc((dstlen + 1) * sizeof(tre_char_t)); \ - if (dst == NULL) \ - return REG_ESPACE; \ - if (dstlen > 0) \ - memcpy(dst, src, dstlen * sizeof(tre_char_t)); \ - dst[dstlen] = TRE_CHAR('\0'); - -/* * Initializes pattern compiling. */ #define INIT_COMP \ @@ -531,10 +520,16 @@ return REG_BADPAT; #ifdef TRE_WCHAR - SAVE_PATTERN(pat, n, fg->wpattern, fg->wlen); + fg->wpattern = tre_strndup(pat, n); + if (fg->wpattern == NULL) + return REG_ESPACE; + fg->wlen = n; STORE_MBS_PAT; #else - SAVE_PATTERN(pat, n, fg->pattern, fg->len); + fg->pattern = tre_strndup(pat, n); + if (fg->pattern == NULL) + return REG_ESPACE; + fg->len = n; #endif DPRINT(("tre_compile_literal: pattern: %s, len %zu, icase: %c, word: %c, " @@ -713,7 +708,13 @@ * classes stripped out. */ #ifdef TRE_WCHAR - SAVE_PATTERN(tmp, pos, fg->wpattern, fg->wlen); + fg->wpattern = tre_strndup(tmp, pos); + if (fg->wpattern == NULL) + { + free(tmp); + return REG_ESPACE; + } + fg->wlen = pos; fg->wescmap = _escmap; STORE_MBS_PAT; @@ -768,7 +769,13 @@ free(_checkpat); } #else - SAVE_PATTERN(tmp, pos, fg->pattern, fg->len); + fg->pattern = tre_strndup(tmp, pos); + if (fg->pattern == NULL) + { + free(tmp); + return REG_ESPACE; + } + fg->len = pos; fg->escmap = _escmap; #endif @@ -1066,3 +1073,21 @@ } return ret; } + +/* + * Copies the pattern pat having length n to p and stores + * the size in l. + */ +static tre_char_t * +tre_strndup(const tre_char_t *src, size_t len) +{ + tre_char_t *dest; + + dest = malloc((len + 1) * sizeof(*dest)); + if (dest == NULL) + return NULL; + if (len > 0) + memcpy(dest, src, len * sizeof(*src)); + dest[len] = TRE_CHAR('\0'); + return dest; +} Index: usr.bin/grep/util.c =================================================================== --- usr.bin/grep/util.c +++ usr.bin/grep/util.c @@ -230,8 +230,7 @@ /* Convenience */ ln = &pc.ln; - pc.ln.file = grep_malloc(strlen(fn) + 1); - strcpy(pc.ln.file, fn); + pc.ln.file = strdup(fn); pc.ln.line_no = 0; pc.ln.len = 0; pc.ln.boff = 0;