Skip to content

Commit 65810fc

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 616de5b commit 65810fc

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

src/backend/utils/misc/guc.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8965,7 +8965,14 @@ get_explain_guc_options(int *num)
89658965
{
89668966
struct config_string *lconf = (struct config_string *) conf;
89678967

8968-
modified = (strcmp(lconf->boot_val, *(lconf->variable)) != 0);
8968+
if (lconf->boot_val == NULL &&
8969+
*lconf->variable == NULL)
8970+
modified = false;
8971+
else if (lconf->boot_val == NULL ||
8972+
*lconf->variable == NULL)
8973+
modified = true;
8974+
else
8975+
modified = (strcmp(lconf->boot_val, *(lconf->variable)) != 0);
89698976
}
89708977
break;
89718978

@@ -9720,7 +9727,8 @@ write_one_nondefault_variable(FILE *fp, struct config_generic *gconf)
97209727
{
97219728
struct config_string *conf = (struct config_string *) gconf;
97229729

9723-
fprintf(fp, "%s", *conf->variable);
9730+
if (*conf->variable)
9731+
fprintf(fp, "%s", *conf->variable);
97249732
}
97259733
break;
97269734

src/include/utils/guc_tables.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,16 @@ struct config_real
223223
void *reset_extra;
224224
};
225225

226+
/*
227+
* A note about string GUCs: the boot_val is allowed to be NULL, which leads
228+
* to the reset_val and the actual variable value (*variable) also being NULL.
229+
* However, there is no way to set a NULL value subsequently using
230+
* set_config_option or any other GUC API. Also, GUC APIs such as SHOW will
231+
* display a NULL value as an empty string. Callers that choose to use a NULL
232+
* boot_val should overwrite the setting later in startup, or else be careful
233+
* that NULL doesn't have semantics that are visibly different from an empty
234+
* string.
235+
*/
226236
struct config_string
227237
{
228238
struct config_generic gen;

0 commit comments

Comments
 (0)