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

Commit77949a2

Browse files
committed
Change the way string relopts are allocated.
Don't try to allocate the default value for a string relopt in the samepalloc chunk as the relopt_string struct. That didn't work too well if youadded a built-in string relopt in the stringRelOpts array, as it's notpossible to have an initializer for a variable length struct in C. Thismakes the code slightly simpler too.While we're at it, move the call to validator function inadd_string_reloption to before the allocation, so that if someone does passa bogus default value, we don't leak memory.
1 parent5b6c843 commit77949a2

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_tsize;
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
caseRELOPT_TYPE_REAL:
387385
size=sizeof(relopt_real);
388386
break;
387+
caseRELOPT_TYPE_STRING:
388+
size=sizeof(relopt_string);
389+
break;
389390
default:
390391
elog(ERROR,"unsupported option type");
391392
returnNULL;/* keep compiler quiet */
@@ -474,45 +475,29 @@ void
474475
add_string_reloption(bits32kinds,char*name,char*desc,char*default_val,
475476
validate_string_reloptvalidator)
476477
{
477-
MemoryContextoldcxt;
478478
relopt_string*newoption;
479-
intdefault_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
intdefault_len;
109109
booldefault_isnull;
110110
validate_string_reloptvalidate_cb;
111-
chardefault_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)

[8]ページ先頭

©2009-2025 Movatter.jp