grep/backport-grep-fix-regex-compilation-memory-leaks.patch
2022-07-27 14:37:18 +08:00

64 lines
1.9 KiB
Diff

From e2aec8c91e9d6ed3fc76f9f145dec8a456ce623a Mon Sep 17 00:00:00 2001
From: Paul Eggert <eggert@cs.ucla.edu>
Date: Fri, 24 Jun 2022 17:53:34 -0500
Subject: [PATCH] grep: fix regex compilation memory leaks
Problem reported by Jim Meyering in:
https://lists.gnu.org/r/grep-devel/2022-06/msg00012.html
* src/dfasearch.c (regex_compile): Fix memory leaks when SYNTAX_ONLY.
Reference:https://git.savannah.gnu.org/cgit/grep.git/commit?id=e2aec8c91e9d6ed3fc76f9f145dec8a456ce623a
Conflict:context adaptation
---
src/dfasearch.c | 24 ++++++++++++++++--------
1 file changed, 16 insertions(+), 8 deletions(-)
diff --git a/src/dfasearch.c b/src/dfasearch.c
index d6afa8d..2875453 100644
--- a/src/dfasearch.c
+++ b/src/dfasearch.c
@@ -148,24 +148,32 @@ regex_compile (struct dfa_comp *dc, char const *p, ptrdiff_t len,
ptrdiff_t pcount, ptrdiff_t lineno, reg_syntax_t syntax_bits,
bool syntax_only)
{
- struct re_pattern_buffer pat0;
- struct re_pattern_buffer *pat = syntax_only ? &pat0 : &dc->patterns[pcount];
- pat->buffer = NULL;
- pat->allocated = 0;
+ struct re_pattern_buffer pat;
+ pat.buffer = NULL;
+ pat.allocated = 0;
/* Do not use a fastmap with -i, to work around glibc Bug#20381. */
- pat->fastmap = (syntax_only | match_icase) ? NULL : xmalloc (UCHAR_MAX + 1);
+ pat.fastmap = syntax_only | match_icase ? NULL : ximalloc (UCHAR_MAX + 1);
- pat->translate = NULL;
+ pat.translate = NULL;
if (syntax_only)
re_set_syntax (syntax_bits | RE_NO_SUB);
else
re_set_syntax (syntax_bits);
- char const *err = re_compile_pattern (p, len, pat);
+ char const *err = re_compile_pattern (p, len, &pat);
if (!err)
- return true;
+ {
+ if (syntax_only)
+ regfree (&pat);
+ else
+ dc->patterns[pcount] = pat;
+
+ return true;
+ }
+
+ free (pat.fastmap);
/* Emit a filename:lineno: prefix for patterns taken from files. */
size_t pat_lineno;
--
2.27.0