Skip to content

Commit cfa4cff

Browse files
committed
Fix memory leak when guc.c decides a setting can't be applied now.
The prohibitValueChange code paths in set_config_option(), which are executed whenever we re-read a PGC_POSTMASTER variable from postgresql.conf, neglected to free anything before exiting. Thus we'd leak the proposed new value of a PGC_STRING variable, as noted by BoChen in bug #16666. For all variable types, if the check hook creates an "extra" chunk, we'd also leak that. These are malloc not palloc chunks, so there is no mechanism for recovering the leaks before process exit. Fortunately, the values are typically not very large, meaning you'd have to go through an awful lot of SIGHUP configuration-reload cycles to make the leakage amount to anything. Still, for a long-lived postmaster process it could potentially be a problem. Oversight in commit 2594cf0. Back-patch to all supported branches. Discussion: https://postgr.es/m/16666-2c41a4eec61b03e1@postgresql.org
1 parent e3868c7 commit cfa4cff

File tree

1 file changed

+30
-2
lines changed
  • src/backend/utils/misc

1 file changed

+30
-2
lines changed

src/backend/utils/misc/guc.c

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6437,6 +6437,10 @@ set_config_option(const char *name, const char *value,
64376437

64386438
if (prohibitValueChange)
64396439
{
6440+
/* Release newextra, unless it's reset_extra */
6441+
if (newextra && !extra_field_used(&conf->gen, newextra))
6442+
free(newextra);
6443+
64406444
if (*conf->variable != newval)
64416445
{
64426446
record->status |= GUC_PENDING_RESTART;
@@ -6527,6 +6531,10 @@ set_config_option(const char *name, const char *value,
65276531

65286532
if (prohibitValueChange)
65296533
{
6534+
/* Release newextra, unless it's reset_extra */
6535+
if (newextra && !extra_field_used(&conf->gen, newextra))
6536+
free(newextra);
6537+
65306538
if (*conf->variable != newval)
65316539
{
65326540
record->status |= GUC_PENDING_RESTART;
@@ -6617,6 +6625,10 @@ set_config_option(const char *name, const char *value,
66176625

66186626
if (prohibitValueChange)
66196627
{
6628+
/* Release newextra, unless it's reset_extra */
6629+
if (newextra && !extra_field_used(&conf->gen, newextra))
6630+
free(newextra);
6631+
66206632
if (*conf->variable != newval)
66216633
{
66226634
record->status |= GUC_PENDING_RESTART;
@@ -6723,9 +6735,21 @@ set_config_option(const char *name, const char *value,
67236735

67246736
if (prohibitValueChange)
67256737
{
6738+
bool newval_different;
6739+
67266740
/* newval shouldn't be NULL, so we're a bit sloppy here */
6727-
if (*conf->variable == NULL || newval == NULL ||
6728-
strcmp(*conf->variable, newval) != 0)
6741+
newval_different = (*conf->variable == NULL ||
6742+
newval == NULL ||
6743+
strcmp(*conf->variable, newval) != 0);
6744+
6745+
/* Release newval, unless it's reset_val */
6746+
if (newval && !string_field_used(conf, newval))
6747+
free(newval);
6748+
/* Release newextra, unless it's reset_extra */
6749+
if (newextra && !extra_field_used(&conf->gen, newextra))
6750+
free(newextra);
6751+
6752+
if (newval_different)
67296753
{
67306754
record->status |= GUC_PENDING_RESTART;
67316755
ereport(elevel,
@@ -6820,6 +6844,10 @@ set_config_option(const char *name, const char *value,
68206844

68216845
if (prohibitValueChange)
68226846
{
6847+
/* Release newextra, unless it's reset_extra */
6848+
if (newextra && !extra_field_used(&conf->gen, newextra))
6849+
free(newextra);
6850+
68236851
if (*conf->variable != newval)
68246852
{
68256853
record->status |= GUC_PENDING_RESTART;

0 commit comments

Comments
 (0)