summaryrefslogtreecommitdiff
path: root/src/third_party/pcre-8.42/patches/0003-SERVER-60299-fix-compile-error-memory-leak.patch
blob: 5b86b347ed5ee47d2ac263f2f6b7fb5ba3529ab9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
From 27721e18b083fec4947391ec8edfad0e8dff2784 Mon Sep 17 00:00:00 2001
From: Ryan Egesdahl <ryan.egesdahl@mongodb.com>
Date: Tue, 28 Sep 2021 15:02:04 -0700
Subject: [PATCH] SERVER-60299 Backport PCRE bugfix for Bugzilla #2613

This is a backport of patches from PCRE 8.45 which fixed a memory leak
that occurred whenever a pattern with 20 or more capture groups fails to
compile. Both patches are from revision 1765 of the upstream Subversion
repository.

  * https://vcs.pcre.org/pcre/code/trunk/pcre_compile.c?view=patch&r1=1765&r2=1764&pathrev=1765
  * https://vcs.pcre.org/pcre/code/trunk/pcre_exec.c?view=patch&r1=1765&r2=1764&pathrev=1765

They have been modified slightly to omit the copyright header change,
which did not apply cleanly.

---
 src/third_party/pcre-8.42/pcre_compile.c | 7 +++++--
 src/third_party/pcre-8.42/pcre_exec.c    | 2 +-
 2 files changed, 6 insertions(+), 3 deletions(-)

diff --git a/src/third_party/pcre-8.42/pcre_compile.c b/src/third_party/pcre-8.42/pcre_compile.c
index 6dd88860638..6b61e10ccd1 100644
--- a/src/third_party/pcre-8.42/pcre_compile.c
+++ b/src/third_party/pcre-8.42/pcre_compile.c
@@ -9087,6 +9087,8 @@ pcre_uchar cworkspace[COMPILE_WORK_SIZE];
 similar way to cworkspace, it can be expanded using malloc() if necessary. */
 
 named_group named_groups[NAMED_GROUP_LIST_SIZE];
+cd->named_groups = named_groups;
+cd->named_group_list_size = NAMED_GROUP_LIST_SIZE;
 
 /* Set this early so that early errors get offset 0. */
 
@@ -9360,8 +9362,6 @@ cd->hwm = cworkspace;
 cd->iscondassert = FALSE;
 cd->start_workspace = cworkspace;
 cd->workspace_size = COMPILE_WORK_SIZE;
-cd->named_groups = named_groups;
-cd->named_group_list_size = NAMED_GROUP_LIST_SIZE;
 cd->start_pattern = (const pcre_uchar *)pattern;
 cd->end_pattern = (const pcre_uchar *)(pattern + STRLEN_UC((const pcre_uchar *)pattern));
 cd->req_varyopt = 0;
@@ -9472,6 +9472,7 @@ if (cd->names_found > 0)
     add_name(cd, ng->name, ng->length, ng->number);
   if (cd->named_group_list_size > NAMED_GROUP_LIST_SIZE)
     (PUBL(free))((void *)cd->named_groups);
+  cd->named_group_list_size = 0;   /* So we don't free it twice */   
   }
 
 /* Set up a starting, non-extracting bracket, then compile the expression. On
@@ -9622,6 +9623,8 @@ if (errorcode != 0)
   {
   (PUBL(free))(re);
   PCRE_EARLY_ERROR_RETURN:
+  if (cd->named_group_list_size > NAMED_GROUP_LIST_SIZE)
+    (PUBL(free))((void *)cd->named_groups);
   *erroroffset = (int)(ptr - (const pcre_uchar *)pattern);
   PCRE_EARLY_ERROR_RETURN2:
   *errorptr = find_error_text(errorcode);
diff --git a/src/third_party/pcre-8.42/pcre_exec.c b/src/third_party/pcre-8.42/pcre_exec.c
index 3fd58cbe31c..79c5cb98bdb 100644
--- a/src/third_party/pcre-8.42/pcre_exec.c
+++ b/src/third_party/pcre-8.42/pcre_exec.c
@@ -758,7 +758,7 @@ for (;;)
     md->mark = NULL;    /* In case previously set by assertion */
     RMATCH(eptr, ecode + PRIV(OP_lengths)[*ecode] + ecode[1], offset_top, md,
       eptrb, RM55);
-    if ((rrc == MATCH_MATCH || rrc == MATCH_ACCEPT) &&
+    if ((rrc == MATCH_MATCH || rrc == MATCH_ACCEPT || rrc == MATCH_KETRPOS) &&
          md->mark == NULL) md->mark = ecode + 2;
 
     /* A return of MATCH_SKIP_ARG means that matching failed at SKIP with an
-- 
2.30.2