Skip to content

Commit

Permalink
Fix bsearch compare function in guc.c
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
gfphoenix78 committed Jul 9, 2024
1 parent 7670f82 commit 6fd1940
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 10 deletions.
5 changes: 1 addition & 4 deletions src/backend/task/entry.c
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ get_range(bits, low, high, names, ch, file)
register int i;
auto int num1,
num2,
num3;
num3 = 1;

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

Expand Down Expand Up @@ -343,9 +343,6 @@ get_range(bits, low, high, names, ch, file)
ch = get_number(&num3, 0, PPC_NULL, ch, file);
if (ch == EOF || num3 <= 0)
return EOF;
} else {
/* no step. default==1. */
num3 = 1;
}

/*
Expand Down
28 changes: 22 additions & 6 deletions src/backend/utils/misc/guc.c
Original file line number Diff line number Diff line change
Expand Up @@ -5140,6 +5140,7 @@ static bool report_needed; /* true if any GUC_REPORT reports are needed */
static int GUCNestLevel = 0; /* 1 when in main transaction */


static int guc_var_name_compare(const void *key, const void *generic);
static int guc_var_compare(const void *a, const void *b);
static void InitializeGUCOptionsFromEnvironment(void);
static void InitializeOneGUCOption(struct config_generic *gconf);
Expand Down Expand Up @@ -5715,7 +5716,6 @@ struct config_generic *
find_option(const char *name, bool create_placeholders, bool skip_errors,
int elevel)
{
const char **key = &name;
struct config_generic **res;
int i;

Expand All @@ -5725,11 +5725,11 @@ find_option(const char *name, bool create_placeholders, bool skip_errors,
* By equating const char ** with struct config_generic *, we are assuming
* the name field is first in config_generic.
*/
res = (struct config_generic **) bsearch((void *) &key,
res = (struct config_generic **) bsearch((void *) &name,
(void *) guc_variables,
num_guc_variables,
sizeof(struct config_generic *),
guc_var_compare);
guc_var_name_compare);
if (res)
return *res;

Expand Down Expand Up @@ -5776,6 +5776,23 @@ find_option(const char *name, bool create_placeholders, bool skip_errors,
return NULL;
}

/*
* comparator for bsearch guc_variables array
* qsort requires that two arguments are the type of element,
* but bsearch requires the first argument to be the key,
* the second argument is type of the array element.
*
* In pg upstream, bsearch is not used in this file, so
* the function should be removed later.
*/
static int
guc_var_name_compare(const void *key, const void *generic)
{
const char *name = *(char *const *)key;
const struct config_generic *conf = *(struct config_generic *const *) generic;

return guc_name_compare(name, conf->name);
}

/*
* comparator for qsorting and bsearching guc_variables array
Expand Down Expand Up @@ -9435,18 +9452,17 @@ static void
define_custom_variable(struct config_generic *variable)
{
const char *name = variable->name;
const char **nameAddr = &name;
struct config_string *pHolder;
struct config_generic **res;

/*
* See if there's a placeholder by the same name.
*/
res = (struct config_generic **) bsearch((void *) &nameAddr,
res = (struct config_generic **) bsearch((void *) &name,
(void *) guc_variables,
num_guc_variables,
sizeof(struct config_generic *),
guc_var_compare);
guc_var_name_compare);
if (res == NULL)
{
/*
Expand Down

0 comments on commit 6fd1940

Please sign in to comment.