Skip to content

Commit 82063ed

Browse files
committed
Be more wary about NULL values for GUC string variables.
get_explain_guc_options() crashed if a string GUC marked GUC_EXPLAIN has a NULL boot_val. Nosing around found a couple of other places that seemed insufficiently cautious about NULL string values, although those are likely unreachable in practice. Add some commentary defining the expectations for NULL values of string variables, in hopes of forestalling future additions of more such bugs. Xing Guo, Aleksander Alekseev, Tom Lane Discussion: https://postgr.es/m/CACpMh+AyDx5YUpPaAgzVwC1d8zfOL4JoD-uyFDnNSa1z0EsDQQ@mail.gmail.com
1 parent e22819a commit 82063ed

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

src/backend/utils/misc/guc.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,7 +1445,9 @@ check_GUC_init(struct config_generic *gconf)
14451445
{
14461446
struct config_string *conf = (struct config_string *) gconf;
14471447

1448-
if (*conf->variable != NULL && strcmp(*conf->variable, conf->boot_val) != 0)
1448+
if (*conf->variable != NULL &&
1449+
(conf->boot_val == NULL ||
1450+
strcmp(*conf->variable, conf->boot_val) != 0))
14491451
{
14501452
elog(LOG, "GUC (PGC_STRING) %s, boot_val=%s, C-var=%s",
14511453
conf->gen.name, conf->boot_val ? conf->boot_val : "<null>", *conf->variable);
@@ -5215,7 +5217,14 @@ get_explain_guc_options(int *num)
52155217
{
52165218
struct config_string *lconf = (struct config_string *) conf;
52175219

5218-
modified = (strcmp(lconf->boot_val, *(lconf->variable)) != 0);
5220+
if (lconf->boot_val == NULL &&
5221+
*lconf->variable == NULL)
5222+
modified = false;
5223+
else if (lconf->boot_val == NULL ||
5224+
*lconf->variable == NULL)
5225+
modified = true;
5226+
else
5227+
modified = (strcmp(lconf->boot_val, *(lconf->variable)) != 0);
52195228
}
52205229
break;
52215230

@@ -5442,7 +5451,8 @@ write_one_nondefault_variable(FILE *fp, struct config_generic *gconf)
54425451
{
54435452
struct config_string *conf = (struct config_string *) gconf;
54445453

5445-
fprintf(fp, "%s", *conf->variable);
5454+
if (*conf->variable)
5455+
fprintf(fp, "%s", *conf->variable);
54465456
}
54475457
break;
54485458

src/include/utils/guc_tables.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,16 @@ struct config_real
240240
void *reset_extra;
241241
};
242242

243+
/*
244+
* A note about string GUCs: the boot_val is allowed to be NULL, which leads
245+
* to the reset_val and the actual variable value (*variable) also being NULL.
246+
* However, there is no way to set a NULL value subsequently using
247+
* set_config_option or any other GUC API. Also, GUC APIs such as SHOW will
248+
* display a NULL value as an empty string. Callers that choose to use a NULL
249+
* boot_val should overwrite the setting later in startup, or else be careful
250+
* that NULL doesn't have semantics that are visibly different from an empty
251+
* string.
252+
*/
243253
struct config_string
244254
{
245255
struct config_generic gen;

0 commit comments

Comments
 (0)