Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ae65ca3

Browse filesBrowse files
committed
Avoid memory leakage across successive calls of regexp_matches() or
regexp_split_to_table() within a single query. This is only a partial solution, as it turns out that with enough matches per string these functions can also tickle a repalloc() misbehavior. But fixing that is a topic for a separate patch.
1 parent 1b70619 commit ae65ca3
Copy full SHA for ae65ca3

File tree

Expand file treeCollapse file tree

1 file changed

+29
-1
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+29
-1
lines changed

‎src/backend/utils/adt/regexp.c

Copy file name to clipboardExpand all lines: src/backend/utils/adt/regexp.c
+29-1Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/utils/adt/regexp.c,v 1.72 2007/08/11 03:56:24 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/utils/adt/regexp.c,v 1.73 2007/08/11 19:16:41 tgl Exp $
1212
*
1313
* Alistair Crooks added the code for the regex caching
1414
* agc - cached the regular expressions used - there's a good chance
@@ -112,6 +112,7 @@ static regexp_matches_ctx *setup_regexp_matches(text *orig_str, text *pattern,
112112
bool force_glob,
113113
bool use_subpatterns,
114114
bool ignore_degenerate);
115+
static void cleanup_regexp_matches(regexp_matches_ctx *matchctx);
115116
static ArrayType *build_regexp_matches_result(regexp_matches_ctx *matchctx);
116117
static Datum build_regexp_split_result(regexp_matches_ctx *splitctx);
117118

@@ -815,6 +816,9 @@ regexp_matches(PG_FUNCTION_ARGS)
815816
SRF_RETURN_NEXT(funcctx, PointerGetDatum(result_ary));
816817
}
817818

819+
/* release space in multi-call ctx to avoid intraquery memory leak */
820+
cleanup_regexp_matches(matchctx);
821+
818822
SRF_RETURN_DONE(funcctx);
819823
}
820824

@@ -968,6 +972,21 @@ setup_regexp_matches(text *orig_str, text *pattern, text *flags,
968972
return matchctx;
969973
}
970974

975+
/*
976+
* cleanup_regexp_matches - release memory of a regexp_matches_ctx
977+
*/
978+
static void
979+
cleanup_regexp_matches(regexp_matches_ctx *matchctx)
980+
{
981+
pfree(matchctx->orig_str);
982+
pfree(matchctx->match_locs);
983+
if (matchctx->elems)
984+
pfree(matchctx->elems);
985+
if (matchctx->nulls)
986+
pfree(matchctx->nulls);
987+
pfree(matchctx);
988+
}
989+
971990
/*
972991
* build_regexp_matches_result - build output array for current match
973992
*/
@@ -1050,6 +1069,9 @@ regexp_split_to_table(PG_FUNCTION_ARGS)
10501069
SRF_RETURN_NEXT(funcctx, result);
10511070
}
10521071

1072+
/* release space in multi-call ctx to avoid intraquery memory leak */
1073+
cleanup_regexp_matches(splitctx);
1074+
10531075
SRF_RETURN_DONE(funcctx);
10541076
}
10551077

@@ -1084,6 +1106,12 @@ Datum regexp_split_to_array(PG_FUNCTION_ARGS)
10841106
splitctx->next_match++;
10851107
}
10861108

1109+
/*
1110+
* We don't call cleanup_regexp_matches here; it would try to pfree
1111+
* the input string, which we didn't copy. The space is not in a
1112+
* long-lived memory context anyway.
1113+
*/
1114+
10871115
PG_RETURN_ARRAYTYPE_P(makeArrayResult(astate, CurrentMemoryContext));
10881116
}
10891117

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.