From owner-svn-src-stable-11@freebsd.org Wed Aug 16 17:54:31 2017 Return-Path: Delivered-To: svn-src-stable-11@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 258D7DC54C6; Wed, 16 Aug 2017 17:54:31 +0000 (UTC) (envelope-from kevans@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 0277E687BB; Wed, 16 Aug 2017 17:54:30 +0000 (UTC) (envelope-from kevans@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id v7GHsU5q059236; Wed, 16 Aug 2017 17:54:30 GMT (envelope-from kevans@FreeBSD.org) Received: (from kevans@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id v7GHsU0n059235; Wed, 16 Aug 2017 17:54:30 GMT (envelope-from kevans@FreeBSD.org) Message-Id: <201708161754.v7GHsU0n059235@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: kevans set sender to kevans@FreeBSD.org using -f From: Kyle Evans Date: Wed, 16 Aug 2017 17:54:30 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-stable@freebsd.org, svn-src-stable-11@freebsd.org Subject: svn commit: r322586 - stable/11/usr.bin/grep/regex X-SVN-Group: stable-11 X-SVN-Commit-Author: kevans X-SVN-Commit-Paths: stable/11/usr.bin/grep/regex X-SVN-Commit-Revision: 322586 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-stable-11@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: SVN commit messages for only the 11-stable src tree List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 16 Aug 2017 17:54:31 -0000 Author: kevans Date: Wed Aug 16 17:54:29 2017 New Revision: 322586 URL: https://svnweb.freebsd.org/changeset/base/322586 Log: bsdgrep: fix escape map building when using TRE (BSD_GREP_FASTMATCH) MFC r317700: bsdgrep: use calloc where appropriate in grep's tre-fastmatch Also apply style(9) to a related NULL check. MFC r317701: bsdgrep: correct test sense from r317700 Kyle's change in review D10098 was correct. I introduced the error when extracting a portion of that change. MFC r317704: bsdgrep: fix escape map building for multibyte strings In BSD grep, fix escape map building in the regex parser. It was previously using memory not explicitly initialized, and the MBS escape map was being built based on a version of the pattern with escapes already parsed out. This is Kyle's change, but I restored the broken style that already exists in this file. Approved by: emaste (mentor, blanket MFC) Modified: stable/11/usr.bin/grep/regex/tre-fastmatch.c Directory Properties: stable/11/ (props changed) Modified: stable/11/usr.bin/grep/regex/tre-fastmatch.c ============================================================================== --- stable/11/usr.bin/grep/regex/tre-fastmatch.c Wed Aug 16 17:54:24 2017 (r322585) +++ stable/11/usr.bin/grep/regex/tre-fastmatch.c Wed Aug 16 17:54:29 2017 (r322586) @@ -98,6 +98,18 @@ static int fastcmp(const fastmatch_t *fg, const void * fg->pattern[siz] = '\0'; \ } \ +#define CONV_MBS_PAT(src, dest, destsz) \ + { \ + destsz = wcstombs(NULL, src, 0); \ + if (destsz == (size_t)-1) \ + return REG_BADPAT; \ + dest = malloc(destsz + 1); \ + if (dest == NULL) \ + return REG_ESPACE; \ + wcstombs(dest, src, destsz); \ + dest[destsz] = '\0'; \ + } \ + #define IS_OUT_OF_BOUNDS \ ((!fg->reversed \ ? ((type == STR_WIDE) ? ((j + fg->wlen) > len) \ @@ -630,7 +642,7 @@ tre_compile_fast(fastmatch_t *fg, const tre_char_t *pa if (escaped) { if (!_escmap) - _escmap = malloc(n * sizeof(bool)); + _escmap = calloc(n, sizeof(bool)); if (!_escmap) { free(tmp); @@ -714,8 +726,8 @@ badpat: { if (fg->wescmap != NULL) { - fg->escmap = malloc(fg->len * sizeof(bool)); - if (!fg->escmap) + fg->escmap = calloc(fg->len, sizeof(bool)); + if (fg->escmap == NULL) { tre_free_fast(fg); return REG_ESPACE; @@ -723,15 +735,29 @@ badpat: } escaped = false; - for (unsigned int i = 0; i < fg->len; i++) - if (fg->pattern[i] == '\\') - escaped = !escaped; - else if (fg->pattern[i] == '.' && fg->escmap && escaped) + char *_checkpat = NULL; + size_t _checklen = 0; + unsigned int escofs = 0; + /* + * Make a copy here of the original pattern, because fg->pattern has + * already been stripped of all escape sequences in the above processing. + * This is necessary if we wish to later treat fg->escmap as an actual, + * functional replacement of fg->wescmap. + */ + CONV_MBS_PAT(pat, _checkpat, _checklen); + for (unsigned int i = 0; i < n; i++) + if (_checkpat[i] == '\\') { - fg->escmap[i] = true; + escaped = !escaped; + if (escaped) + ++escofs; + } + else if (_checkpat[i] == '.' && fg->escmap != NULL && escaped) + { + fg->escmap[i - escofs] = true; escaped = false; } - else if (fg->pattern[i] == '.' && !escaped) + else if (_checkpat[i] == '.' && !escaped) { hasdot = i; if (firstdot == -1) @@ -739,6 +765,7 @@ badpat: } else escaped = false; + free(_checkpat); } #else SAVE_PATTERN(tmp, pos, fg->pattern, fg->len);