Aharon Robbins wrote:

Attached is a diff with bits and pieces (fixes / changes) in gawk's
regex routines that you may wish to apply to the GNULIB version.

Thanks for doing all that. I looked over that diff and installed the attached patches into gnulib; they are taken from your diff.

Here are comments on the parts of the diff not incorporated in this round:

-static const char __re_error_msgid[] =
+static const char __re_error_msgid[] attribute_hidden =

Since the constant is static, there should be no need for attribute_hidden, as the constant is used only in this compilation unit. Similarly for other introductions of attribute_hidden.

-  re_dfa_t *dfa = bufp->buffer;
+  re_dfa_t *dfa = (re_dfa_t *) bufp->buffer;

This cast is not needed, as bufp->buffer is already of the proper type. Similarly elsewhere.

-      preg->buffer = dfa;
+      preg->buffer = (unsigned char *) dfa;

This cast seems counterproductive, as dfa is already of the proper type and the unsigned char * is not the proper type.

-  dfa->subexp_map = re_malloc (Idx, preg->re_nsub);
+  /* some malloc()-checkers don't like zero allocations */
+  if (preg->re_nsub > 0)
+    dfa->subexp_map = re_malloc (int, preg->re_nsub);
+  else
+    dfa->subexp_map = NULL;
+

Since malloc (0) is well-defined to return either NULL or a valid pointer to a zero-byte object that can be freed, the code is working as-is. I'd rather look for a solution that involves silencing the checking without making the code bulkier and typically slower.

+    /*
+     * Fedora Core 2, maybe others, have broken `btowc' that returns -1
+     * for any value > 127. Sigh. Note that `start_ch' and `end_ch' are
+     * unsigned, so we don't have sign extension problems.
+     */
     start_wc = ((start_elem->type == SB_CHAR || start_elem->type == COLL_SYM)
-               ? __btowc (start_ch) : start_elem->opr.wch);
+               ? start_ch : start_elem->opr.wch);
     end_wc = ((end_elem->type == SB_CHAR || end_elem->type == COLL_SYM)
-             ? __btowc (end_ch) : end_elem->opr.wch);
+             ? end_ch : end_elem->opr.wch);
     if (start_wc == WEOF || end_wc == WEOF)
       return REG_ECOLLATE;

I don't see how this patch is helpful. If btowc returns -1 we are looking at an encoding error, and we can't treat that byte as if it were a character. In some single-byte locales, btowc returns 1 for values < 128, and they're encoding errors too. Perhaps you could mention a use case?

       /* Build single byte matching table for this equivalence class.  */
+      char_buf[1] = (unsigned char) '\0';

This should be unnecessary, as the rest of the code shouldn't be looking at that byte. Is this something to pacify a lint checker?

-       wc = wc2;
+       wc = (wint_t) wc2;

wc and wc2 are both integers so the cast is unnecessary. Similarly elsewhere.

-      pstr->mbs[char_idx] = toupper (ch);
+      if (islower (ch))
+       pstr->mbs[char_idx] = toupper (ch);
+      else
+       pstr->mbs[char_idx] = ch;

toupper is a no-op if ch is not a lower-case character, so the patch should be unnecessary.

+  /*
+   * ADR: valgrind says size can be 0, which then doesn't
+   * free the block of size 0.  Harumph. This seems
+   * to work ok, though.
+   */
+  if (size == 0)
+    {
+       memset(set, 0, sizeof(*set));
+       return REG_NOERROR;
+    }

I assume this is related to the malloc (0) stuff above. Or possibly MALLOC_0_IS_NONNULL is incorrect on your platform? If so, I'd rather fix that bug rather than attempt to work around it.

diff --git a/lib/regex_internal.h b/lib/regex_internal.h
index 0307a34..c634a00 100644
--- a/lib/regex_internal.h
+++ b/lib/regex_internal.h
@@ -117,6 +117,10 @@
 # define BE(expr, val) __builtin_expect (expr, val)
 #else
 # define BE(expr, val) (expr)
+# ifdef inline
+# undef inline
+# endif
+# define inline
 #endif

This should not be necessary, since m4/regex.m4's gl_PREREQ_REGEX requires AC_C_INLINE, which should define 'inline' on the ancient systems that lack it. If that's not working, I'd like to fix the problem at the source rather than work around it here.

>From 64fa84073cf0d8b0f8d604b88e92b0a1450ccdc4 Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email protected]>
Date: Mon, 18 Jan 2016 10:31:07 -0800
Subject: [PATCH 1/3] regex: fix memory leaks

Problem and draft fix reported by Aharon Robbins in:
http://lists.gnu.org/archive/html/bug-gnulib/2016-01/msg00082.html
* lib/regcomp.c (build_range_exp, build_charclass_op):
* lib/regex_internal.c (re_dfa_add_node):
Fix memory leak on failure.
---
 ChangeLog            |  9 +++++++++
 lib/regcomp.c        | 27 +++++++++++++--------------
 lib/regex_internal.c |  8 +++++++-
 3 files changed, 29 insertions(+), 15 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index fac13e6..ecb0746 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2016-01-18  Paul Eggert  <[email protected]>
+
+	regex: fix memory leaks
+	Problem and draft fix reported by Aharon Robbins in:
+	http://lists.gnu.org/archive/html/bug-gnulib/2016-01/msg00082.html
+	* lib/regcomp.c (build_range_exp, build_charclass_op):
+	* lib/regex_internal.c (re_dfa_add_node):
+	Fix memory leak on failure.
+
 2016-01-18  Pádraig Brady  <[email protected]>
 
 	fts: don't unconditionally use leaf optimization for NFS
diff --git a/lib/regcomp.c b/lib/regcomp.c
index 43999d0..0369f26 100644
--- a/lib/regcomp.c
+++ b/lib/regcomp.c
@@ -2779,7 +2779,11 @@ build_range_exp (const reg_syntax_t syntax,
 					new_nranges);
 
 	    if (BE (new_array_start == NULL || new_array_end == NULL, 0))
-	      return REG_ESPACE;
+	      {
+		re_free (new_array_start);
+		re_free (new_array_end);
+		return REG_ESPACE;
+	      }
 
 	    mbcset->range_starts = new_array_start;
 	    mbcset->range_ends = new_array_end;
@@ -3676,26 +3680,21 @@ build_charclass_op (re_dfa_t *dfa, RE_TRANSLATE_TYPE trans,
   bin_tree_t *tree;
 
   sbcset = (re_bitset_ptr_t) calloc (sizeof (bitset_t), 1);
-#ifdef RE_ENABLE_I18N
-  mbcset = (re_charset_t *) calloc (sizeof (re_charset_t), 1);
-#endif /* RE_ENABLE_I18N */
-
-#ifdef RE_ENABLE_I18N
-  if (BE (sbcset == NULL || mbcset == NULL, 0))
-#else /* not RE_ENABLE_I18N */
   if (BE (sbcset == NULL, 0))
-#endif /* not RE_ENABLE_I18N */
     {
       *err = REG_ESPACE;
       return NULL;
     }
-
-  if (non_match)
-    {
 #ifdef RE_ENABLE_I18N
-      mbcset->non_match = 1;
-#endif /* not RE_ENABLE_I18N */
+  mbcset = (re_charset_t *) calloc (sizeof (re_charset_t), 1);
+  if (BE (mbcset == NULL, 0))
+    {
+      re_free (sbcset);
+      *err = REG_ESPACE;
+      return NULL;
     }
+  mbcset->non_match = non_match;
+#endif /* RE_ENABLE_I18N */
 
   /* We don't care the syntax in this case.  */
   ret = build_charclass (trans, sbcset,
diff --git a/lib/regex_internal.c b/lib/regex_internal.c
index fd20407..9dc7124 100644
--- a/lib/regex_internal.c
+++ b/lib/regex_internal.c
@@ -1428,7 +1428,13 @@ re_dfa_add_node (re_dfa_t *dfa, re_token_t token)
       new_eclosures = re_realloc (dfa->eclosures, re_node_set, new_nodes_alloc);
       if (BE (new_nexts == NULL || new_indices == NULL
 	      || new_edests == NULL || new_eclosures == NULL, 0))
-	return REG_MISSING;
+	{
+	   re_free (new_nexts);
+	   re_free (new_indices);
+	   re_free (new_edests);
+	   re_free (new_eclosures);
+	   return REG_MISSING;
+	}
       dfa->nexts = new_nexts;
       dfa->org_indices = new_indices;
       dfa->edests = new_edests;
-- 
2.5.0

>From d69ce088e92fb25c56e2600d816d16235eaba96b Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email protected]>
Date: Mon, 18 Jan 2016 10:32:26 -0800
Subject: [PATCH 2/3] regex: fix [ diagnostic

Problem and fix reported by Aharon Robbins in:
http://lists.gnu.org/archive/html/bug-gnulib/2016-01/msg00082.html
* lib/regcomp.c (REG_EBRACK_IDX): Fix misleading diagnostic about [.

* lib/regcomp.c (build_range_exp, build_charclass_op)
---
 ChangeLog     | 7 ++++++-
 lib/regcomp.c | 4 ++--
 2 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index ecb0746..d4779a1 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,9 +1,14 @@
 2016-01-18  Paul Eggert  <[email protected]>
 
+	regex: fix [ diagnostic
+	Problem and fix reported by Aharon Robbins in:
+	http://lists.gnu.org/archive/html/bug-gnulib/2016-01/msg00082.html
+	* lib/regcomp.c (REG_EBRACK_IDX): Fix misleading diagnostic about [.
+
 	regex: fix memory leaks
 	Problem and draft fix reported by Aharon Robbins in:
 	http://lists.gnu.org/archive/html/bug-gnulib/2016-01/msg00082.html
-	* lib/regcomp.c (build_range_exp, build_charclass_op):
+	* lib/regcomp.c (build_range_exp, build_charclass_op)
 	* lib/regex_internal.c (re_dfa_add_node):
 	Fix memory leak on failure.
 
diff --git a/lib/regcomp.c b/lib/regcomp.c
index 0369f26..1eda110 100644
--- a/lib/regcomp.c
+++ b/lib/regcomp.c
@@ -153,9 +153,9 @@ static const char __re_error_msgid[] =
     gettext_noop ("Invalid back reference") /* REG_ESUBREG */
     "\0"
 #define REG_EBRACK_IDX	(REG_ESUBREG_IDX + sizeof "Invalid back reference")
-    gettext_noop ("Unmatched [ or [^")	/* REG_EBRACK */
+    gettext_noop ("Unmatched [, [^, [:, [., or [=")	/* REG_EBRACK */
     "\0"
-#define REG_EPAREN_IDX	(REG_EBRACK_IDX + sizeof "Unmatched [ or [^")
+#define REG_EPAREN_IDX	(REG_EBRACK_IDX + sizeof "Unmatched [, [^, [:, [., or [=")
     gettext_noop ("Unmatched ( or \\(") /* REG_EPAREN */
     "\0"
 #define REG_EBRACE_IDX	(REG_EPAREN_IDX + sizeof "Unmatched ( or \\(")
-- 
2.5.0

>From 4995acd81a426303125c8a9b34c01bd3aa064dde Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email protected]>
Date: Mon, 18 Jan 2016 10:34:18 -0800
Subject: [PATCH 3/3] regex: pacify static checkers

Problem and draft fix reported by Aharon Robbins in:
http://lists.gnu.org/archive/html/bug-gnulib/2016-01/msg00082.html
* lib/regcomp.c (build_charclass_op, create_tree) [lint]:
Clear memory to pacify static checkers.
---
 ChangeLog     | 6 ++++++
 lib/regcomp.c | 6 ++++++
 2 files changed, 12 insertions(+)

diff --git a/ChangeLog b/ChangeLog
index d4779a1..b9da935 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,11 @@
 2016-01-18  Paul Eggert  <[email protected]>
 
+	regex: pacify static checkers
+	Problem and draft fix reported by Aharon Robbins in:
+	http://lists.gnu.org/archive/html/bug-gnulib/2016-01/msg00082.html
+	* lib/regcomp.c (build_charclass_op, create_tree) [lint]:
+	Clear memory to pacify static checkers.
+
 	regex: fix [ diagnostic
 	Problem and fix reported by Aharon Robbins in:
 	http://lists.gnu.org/archive/html/bug-gnulib/2016-01/msg00082.html
diff --git a/lib/regcomp.c b/lib/regcomp.c
index 1eda110..f86ce06 100644
--- a/lib/regcomp.c
+++ b/lib/regcomp.c
@@ -3727,6 +3727,9 @@ build_charclass_op (re_dfa_t *dfa, RE_TRANSLATE_TYPE trans,
 #endif
 
   /* Build a tree for simple bracket.  */
+#ifdef lint
+  memset (&br_token, 0, sizeof br_token);
+#endif
   br_token.type = SIMPLE_BRACKET;
   br_token.opr.sbcset = sbcset;
   tree = create_token_tree (dfa, NULL, NULL, &br_token);
@@ -3821,6 +3824,9 @@ create_tree (re_dfa_t *dfa, bin_tree_t *left, bin_tree_t *right,
 	     re_token_type_t type)
 {
   re_token_t t;
+#ifdef lint
+  memset (&t, 0, sizeof t);
+#endif
   t.type = type;
   return create_token_tree (dfa, left, right, &t);
 }
-- 
2.5.0

Reply via email to