Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 660f532

Browse files
committed
Add verification of variable names in pgbench.
Variables must consist of only alphabets, numerals and underscores. We had allowed to set variables with invalid names, but could not refer them in queries. Thanks to Robert Haas for the review.
1 parent 90f4c2d commit 660f532

File tree

1 file changed

+38
-23
lines changed

1 file changed

+38
-23
lines changed

contrib/pgbench/pgbench.c

Lines changed: 38 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* A simple benchmark program for PostgreSQL
55
* Originally written by Tatsuo Ishii and enhanced by many contributors.
66
*
7-
* $PostgreSQL: pgsql/contrib/pgbench/pgbench.c,v 1.94 2010/01/02 16:57:32 momjian Exp $
7+
* $PostgreSQL: pgsql/contrib/pgbench/pgbench.c,v 1.95 2010/01/06 01:12:14 itagaki Exp $
88
* Copyright (c) 2000-2010, PostgreSQL Global Development Group
99
* ALL RIGHTS RESERVED;
1010
*
@@ -431,8 +431,23 @@ getVariable(CState *st, char *name)
431431
return NULL;
432432
}
433433

434+
/* check whether the name consists of alphabets, numerals and underscores. */
435+
static bool
436+
isLegalVariableName(const char *name)
437+
{
438+
int i;
439+
440+
for (i = 0; name[i] != '\0'; i++)
441+
{
442+
if (!isalnum((unsigned char) name[i]) && name[i] != '_')
443+
return false;
444+
}
445+
446+
return true;
447+
}
448+
434449
static int
435-
putVariable(CState *st, char *name, char *value)
450+
putVariable(CState *st, const char *context, char *name, char *value)
436451
{
437452
Variable key,
438453
*var;
@@ -452,14 +467,24 @@ putVariable(CState *st, char *name, char *value)
452467
{
453468
Variable *newvars;
454469

470+
/*
471+
* Check for the name only when declaring a new variable to avoid
472+
* overhead.
473+
*/
474+
if (!isLegalVariableName(name))
475+
{
476+
fprintf(stderr, "%s: invalid variable name '%s'\n", context, name);
477+
return false;
478+
}
479+
455480
if (st->variables)
456481
newvars = (Variable *) realloc(st->variables,
457482
(st->nvariables + 1) * sizeof(Variable));
458483
else
459484
newvars = (Variable *) malloc(sizeof(Variable));
460485

461486
if (newvars == NULL)
462-
return false;
487+
goto out_of_memory;
463488

464489
st->variables = newvars;
465490

@@ -493,6 +518,10 @@ putVariable(CState *st, char *name, char *value)
493518
}
494519

495520
return true;
521+
522+
out_of_memory:
523+
fprintf(stderr, "%s: out of memory for variable '%s'\n", context, name);
524+
return false;
496525
}
497526

498527
static char *
@@ -687,11 +716,8 @@ runShellCommand(CState *st, char *variable, char **argv, int argc)
687716
return false;
688717
}
689718
snprintf(res, sizeof(res), "%d", retval);
690-
if (!putVariable(st, variable, res))
691-
{
692-
fprintf(stderr, "%s: out of memory\n", argv[0]);
719+
if (!putVariable(st, "setshell", variable, res))
693720
return false;
694-
}
695721

696722
#ifdef DEBUG
697723
printf("shell parameter name: %s, value: %s\n", argv[1], res);
@@ -987,9 +1013,8 @@ doCustom(CState *st, instr_time *conn_time)
9871013
#endif
9881014
snprintf(res, sizeof(res), "%d", getrand(min, max));
9891015

990-
if (putVariable(st, argv[1], res) == false)
1016+
if (!putVariable(st, argv[0], argv[1], res))
9911017
{
992-
fprintf(stderr, "%s: out of memory\n", argv[0]);
9931018
st->ecnt++;
9941019
return true;
9951020
}
@@ -1057,9 +1082,8 @@ doCustom(CState *st, instr_time *conn_time)
10571082
}
10581083
}
10591084

1060-
if (putVariable(st, argv[1], res) == false)
1085+
if (!putVariable(st, argv[0], argv[1], res))
10611086
{
1062-
fprintf(stderr, "%s: out of memory\n", argv[0]);
10631087
st->ecnt++;
10641088
return true;
10651089
}
@@ -1874,11 +1898,8 @@ main(int argc, char **argv)
18741898
}
18751899

18761900
*p++ = '\0';
1877-
if (putVariable(&state[0], optarg, p) == false)
1878-
{
1879-
fprintf(stderr, "Couldn't allocate memory for variable\n");
1901+
if (!putVariable(&state[0], "option", optarg, p))
18801902
exit(1);
1881-
}
18821903
}
18831904
break;
18841905
case 'F':
@@ -1958,11 +1979,8 @@ main(int argc, char **argv)
19581979
state[i].id = i;
19591980
for (j = 0; j < state[0].nvariables; j++)
19601981
{
1961-
if (putVariable(&state[i], state[0].variables[j].name, state[0].variables[j].value) == false)
1962-
{
1963-
fprintf(stderr, "Couldn't allocate memory for variable\n");
1982+
if (!putVariable(&state[i], "startup", state[0].variables[j].name, state[0].variables[j].value))
19641983
exit(1);
1965-
}
19661984
}
19671985
}
19681986
}
@@ -2039,11 +2057,8 @@ main(int argc, char **argv)
20392057
snprintf(val, sizeof(val), "%d", scale);
20402058
for (i = 0; i < nclients; i++)
20412059
{
2042-
if (putVariable(&state[i], "scale", val) == false)
2043-
{
2044-
fprintf(stderr, "Couldn't allocate memory for variable\n");
2060+
if (!putVariable(&state[i], "startup", "scale", val))
20452061
exit(1);
2046-
}
20472062
}
20482063
}
20492064

0 commit comments

Comments
 (0)