Skip to content

Commit 77949a2

Browse files
committed
Change the way string relopts are allocated.
Don't try to allocate the default value for a string relopt in the same palloc chunk as the relopt_string struct. That didn't work too well if you added a built-in string relopt in the stringRelOpts array, as it's not possible to have an initializer for a variable length struct in C. This makes the code slightly simpler too. While we're at it, move the call to validator function in add_string_reloption to before the allocation, so that if someone does pass a bogus default value, we don't leak memory.
1 parent 5b6c843 commit 77949a2

File tree

2 files changed

+13
-28
lines changed

2 files changed

+13
-28
lines changed

src/backend/access/common/reloptions.c

Lines changed: 12 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -371,8 +371,6 @@ allocate_reloption(bits32 kinds, int type, char *name, char *desc)
371371
size_t size;
372372
relopt_gen *newoption;
373373

374-
Assert(type != RELOPT_TYPE_STRING);
375-
376374
oldcxt = MemoryContextSwitchTo(TopMemoryContext);
377375

378376
switch (type)
@@ -386,6 +384,9 @@ allocate_reloption(bits32 kinds, int type, char *name, char *desc)
386384
case RELOPT_TYPE_REAL:
387385
size = sizeof(relopt_real);
388386
break;
387+
case RELOPT_TYPE_STRING:
388+
size = sizeof(relopt_string);
389+
break;
389390
default:
390391
elog(ERROR, "unsupported option type");
391392
return NULL; /* keep compiler quiet */
@@ -474,45 +475,29 @@ void
474475
add_string_reloption(bits32 kinds, char *name, char *desc, char *default_val,
475476
validate_string_relopt validator)
476477
{
477-
MemoryContext oldcxt;
478478
relopt_string *newoption;
479-
int default_len = 0;
480-
481-
oldcxt = MemoryContextSwitchTo(TopMemoryContext);
482-
483-
if (default_val)
484-
default_len = strlen(default_val);
485479

486-
newoption = palloc0(sizeof(relopt_string) + default_len);
480+
/* make sure the validator/default combination is sane */
481+
if (validator)
482+
(validator) (default_val);
487483

488-
newoption->gen.name = pstrdup(name);
489-
if (desc)
490-
newoption->gen.desc = pstrdup(desc);
491-
else
492-
newoption->gen.desc = NULL;
493-
newoption->gen.kinds = kinds;
494-
newoption->gen.namelen = strlen(name);
495-
newoption->gen.type = RELOPT_TYPE_STRING;
484+
newoption = (relopt_string *) allocate_reloption(kinds, RELOPT_TYPE_STRING,
485+
name, desc);
496486
newoption->validate_cb = validator;
497487
if (default_val)
498488
{
499-
strcpy(newoption->default_val, default_val);
500-
newoption->default_len = default_len;
489+
newoption->default_val = MemoryContextStrdup(TopMemoryContext,
490+
default_val);
491+
newoption->default_len = strlen(default_val);
501492
newoption->default_isnull = false;
502493
}
503494
else
504495
{
505-
newoption->default_val[0] = '\0';
496+
newoption->default_val = "";
506497
newoption->default_len = 0;
507498
newoption->default_isnull = true;
508499
}
509500

510-
/* make sure the validator/default combination is sane */
511-
if (newoption->validate_cb)
512-
(newoption->validate_cb) (newoption->default_val);
513-
514-
MemoryContextSwitchTo(oldcxt);
515-
516501
add_reloption((relopt_gen *) newoption);
517502
}
518503

src/include/access/reloptions.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ typedef struct relopt_string
108108
int default_len;
109109
bool default_isnull;
110110
validate_string_relopt validate_cb;
111-
char default_val[1]; /* variable length, zero-terminated */
111+
char *default_val;
112112
} relopt_string;
113113

114114
/* This is the table datatype for fillRelOptions */

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy