Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commitae65ca3

Browse files
committed
Avoid memory leakage across successive calls of regexp_matches() or
regexp_split_to_table() within a single query. This is only a partialsolution, as it turns out that with enough matches per string thesefunctions can also tickle a repalloc() misbehavior. But fixing thatis a topic for a separate patch.
1 parent1b70619 commitae65ca3

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

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

Lines 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/1103:56:24 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/utils/adt/regexp.c,v 1.73 2007/08/1119: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
boolforce_glob,
113113
booluse_subpatterns,
114114
boolignore_degenerate);
115+
staticvoidcleanup_regexp_matches(regexp_matches_ctx*matchctx);
115116
staticArrayType*build_regexp_matches_result(regexp_matches_ctx*matchctx);
116117
staticDatumbuild_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
returnmatchctx;
969973
}
970974

975+
/*
976+
* cleanup_regexp_matches - release memory of a regexp_matches_ctx
977+
*/
978+
staticvoid
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)

[8]ページ先頭

©2009-2025 Movatter.jp