Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/external/bsd/tre/dist/lib Fix memory leak when input contain...



details:   https://anonhg.NetBSD.org/src/rev/67ed00360b64
branches:  trunk
changeset: 357641:67ed00360b64
user:      rin <rin%NetBSD.org@localhost>
date:      Sun Nov 19 13:22:58 2017 +0000

description:
Fix memory leak when input contains illegal multibyte sequence, partially
taken from musl libc:
https://git.musl-libc.org/cgit/musl/commit/src/regex/regexec.c?id=546f6b322bcafa2452925c19f9607d9689c75f95

diffstat:

 external/bsd/tre/dist/lib/tre-match-approx.c   |  15 +++++++++------
 external/bsd/tre/dist/lib/tre-match-parallel.c |   8 +++++---
 external/bsd/tre/dist/lib/tre-match-utils.h    |   6 ++++--
 3 files changed, 18 insertions(+), 11 deletions(-)

diffs (81 lines):

diff -r aa560900cc13 -r 67ed00360b64 external/bsd/tre/dist/lib/tre-match-approx.c
--- a/external/bsd/tre/dist/lib/tre-match-approx.c      Sun Nov 19 13:21:02 2017 +0000
+++ b/external/bsd/tre/dist/lib/tre-match-approx.c      Sun Nov 19 13:22:58 2017 +0000
@@ -225,6 +225,8 @@
 
   size_t i, id;
 
+  reg_errcode_t ret;
+
   if (!match_tags)
     num_tags = 0;
   else
@@ -781,16 +783,17 @@
   DPRINT(("match end offset = %d, match cost = %d\n", match_eo,
          match_costs[TRE_M_COST]));
 
-#ifndef TRE_USE_ALLOCA
-  if (buf)
-    xfree(buf);
-#endif /* !TRE_USE_ALLOCA */
-
   match->cost = match_costs[TRE_M_COST];
   match->num_ins = match_costs[TRE_M_NUM_INS];
   match->num_del = match_costs[TRE_M_NUM_DEL];
   match->num_subst = match_costs[TRE_M_NUM_SUBST];
   *match_end_ofs = match_eo;
 
-  return match_eo >= 0 ? REG_OK : REG_NOMATCH;
+  ret = match_eo >= 0 ? REG_OK : REG_NOMATCH;
+error_exit:
+#ifndef TRE_USE_ALLOCA
+  if (buf)
+    xfree(buf);
+#endif /* !TRE_USE_ALLOCA */
+  return ret;
 }
diff -r aa560900cc13 -r 67ed00360b64 external/bsd/tre/dist/lib/tre-match-parallel.c
--- a/external/bsd/tre/dist/lib/tre-match-parallel.c    Sun Nov 19 13:21:02 2017 +0000
+++ b/external/bsd/tre/dist/lib/tre-match-parallel.c    Sun Nov 19 13:22:58 2017 +0000
@@ -121,6 +121,7 @@
   int new_match = 0;
   int *tmp_tags = NULL;
   int *tmp_iptr;
+  reg_errcode_t ret;
 
 #ifdef TRE_MBSTATE
   memset(&mbstate, '\0', sizeof(mbstate));
@@ -474,13 +475,14 @@
 
   DPRINT(("match end offset = %d\n", match_eo));
 
+  *match_end_ofs = match_eo;
+  ret = match_eo >= 0 ? REG_OK : REG_NOMATCH;
+error_exit:
 #ifndef TRE_USE_ALLOCA
   if (buf)
     xfree(buf);
 #endif /* !TRE_USE_ALLOCA */
-
-  *match_end_ofs = match_eo;
-  return match_eo >= 0 ? REG_OK : REG_NOMATCH;
+  return ret;
 }
 
 /* EOF */
diff -r aa560900cc13 -r 67ed00360b64 external/bsd/tre/dist/lib/tre-match-utils.h
--- a/external/bsd/tre/dist/lib/tre-match-utils.h       Sun Nov 19 13:21:02 2017 +0000
+++ b/external/bsd/tre/dist/lib/tre-match-utils.h       Sun Nov 19 13:22:58 2017 +0000
@@ -52,8 +52,10 @@
            else                                                              \
              {                                                               \
                w = tre_mbrtowc(&next_c, str_byte, (size_t)max, &mbstate);    \
-               if (w == (size_t)-1 || w == (size_t)-2)                       \
-                 return REG_NOMATCH;                                         \
+               if (w == (size_t)-1 || w == (size_t)-2) {                     \
+                 ret = REG_NOMATCH;                                          \
+                 goto error_exit;                                            \
+               }                                                             \
                if (w == 0 && len >= 0)                                       \
                  {                                                           \
                    pos_add_next = 1;                                         \



Home | Main Index | Thread Index | Old Index