Skip to content

Commit d4decf9

Browse files
authored
Fix bsearch compare function in guc.c (#507)
The prototype of the bsearch()'s compare function is `int (*compar)(const void *, const void *));` The first argument is expected to the key object, the second is the array member.
1 parent 7670f82 commit d4decf9

2 files changed

Lines changed: 23 additions & 10 deletions

File tree

src/backend/task/entry.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ get_range(bits, low, high, names, ch, file)
286286
register int i;
287287
auto int num1,
288288
num2,
289-
num3;
289+
num3 = 1;
290290

291291
Debug(DPARS|DEXT, ("get_range()...entering, exit won't show\n"))
292292

@@ -343,9 +343,6 @@ get_range(bits, low, high, names, ch, file)
343343
ch = get_number(&num3, 0, PPC_NULL, ch, file);
344344
if (ch == EOF || num3 <= 0)
345345
return EOF;
346-
} else {
347-
/* no step. default==1. */
348-
num3 = 1;
349346
}
350347

351348
/*

src/backend/utils/misc/guc.c

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5140,6 +5140,7 @@ static bool report_needed; /* true if any GUC_REPORT reports are needed */
51405140
static int GUCNestLevel = 0; /* 1 when in main transaction */
51415141

51425142

5143+
static int guc_var_name_compare(const void *key, const void *generic);
51435144
static int guc_var_compare(const void *a, const void *b);
51445145
static void InitializeGUCOptionsFromEnvironment(void);
51455146
static void InitializeOneGUCOption(struct config_generic *gconf);
@@ -5715,7 +5716,6 @@ struct config_generic *
57155716
find_option(const char *name, bool create_placeholders, bool skip_errors,
57165717
int elevel)
57175718
{
5718-
const char **key = &name;
57195719
struct config_generic **res;
57205720
int i;
57215721

@@ -5725,11 +5725,11 @@ find_option(const char *name, bool create_placeholders, bool skip_errors,
57255725
* By equating const char ** with struct config_generic *, we are assuming
57265726
* the name field is first in config_generic.
57275727
*/
5728-
res = (struct config_generic **) bsearch((void *) &key,
5728+
res = (struct config_generic **) bsearch((void *) &name,
57295729
(void *) guc_variables,
57305730
num_guc_variables,
57315731
sizeof(struct config_generic *),
5732-
guc_var_compare);
5732+
guc_var_name_compare);
57335733
if (res)
57345734
return *res;
57355735

@@ -5776,6 +5776,23 @@ find_option(const char *name, bool create_placeholders, bool skip_errors,
57765776
return NULL;
57775777
}
57785778

5779+
/*
5780+
* comparator for bsearch guc_variables array
5781+
* qsort requires that two arguments are the type of element,
5782+
* but bsearch requires the first argument to be the key,
5783+
* the second argument is type of the array element.
5784+
*
5785+
* In pg upstream, bsearch is not used in this file, so
5786+
* the function should be removed later.
5787+
*/
5788+
static int
5789+
guc_var_name_compare(const void *key, const void *generic)
5790+
{
5791+
const char *name = *(char *const *)key;
5792+
const struct config_generic *conf = *(struct config_generic *const *) generic;
5793+
5794+
return guc_name_compare(name, conf->name);
5795+
}
57795796

57805797
/*
57815798
* comparator for qsorting and bsearching guc_variables array
@@ -9435,18 +9452,17 @@ static void
94359452
define_custom_variable(struct config_generic *variable)
94369453
{
94379454
const char *name = variable->name;
9438-
const char **nameAddr = &name;
94399455
struct config_string *pHolder;
94409456
struct config_generic **res;
94419457

94429458
/*
94439459
* See if there's a placeholder by the same name.
94449460
*/
9445-
res = (struct config_generic **) bsearch((void *) &nameAddr,
9461+
res = (struct config_generic **) bsearch((void *) &name,
94469462
(void *) guc_variables,
94479463
num_guc_variables,
94489464
sizeof(struct config_generic *),
9449-
guc_var_compare);
9465+
guc_var_name_compare);
94509466
if (res == NULL)
94519467
{
94529468
/*

0 commit comments

Comments
 (0)