Skip to content

Commit d8bb22a

Browse files
committed
Disallow a digit as the first character of a variable name in pgbench.
The point of this restriction is to avoid trying to substitute variables into timestamp literal values, which may contain strings like '12:34'. There is a good deal more that should be done to reduce pgbench's tendency to substitute where it shouldn't. But this is sufficient to solve the case complained of by Jaime Soler, and it's simple enough to back-patch. Back-patch to v11; before commit 9d36a38, pgbench had a slightly different definition of what a variable name is, and anyway it seems unwise to change long-stable branches for this. Fabien Coelho Discussion: https://postgr.es/m/alpine.DEB.2.22.394.2006291740420.805678@pseudo
1 parent bcdff44 commit d8bb22a

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

doc/src/sgml/ref/pgbench.sgml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -899,7 +899,7 @@ pgbench <optional> <replaceable>options</replaceable> </optional> <replaceable>d
899899
<para>
900900
There is a simple variable-substitution facility for script files.
901901
Variable names must consist of letters (including non-Latin letters),
902-
digits, and underscores.
902+
digits, and underscores, with the first character not being a digit.
903903
Variables can be set by the command-line <option>-D</option> option,
904904
explained above, or by the meta commands explained below.
905905
In addition to any variables preset by <option>-D</option> command-line options,

src/bin/pgbench/pgbench.c

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,6 +1322,7 @@ makeVariableValue(Variable *var)
13221322
* "src/bin/pgbench/exprscan.l". Also see parseVariable(), below.
13231323
*
13241324
* Note: this static function is copied from "src/bin/psql/variables.c"
1325+
* but changed to disallow variable names starting with a digit.
13251326
*/
13261327
static bool
13271328
valid_variable_name(const char *name)
@@ -1332,6 +1333,15 @@ valid_variable_name(const char *name)
13321333
if (*ptr == '\0')
13331334
return false;
13341335

1336+
/* must not start with [0-9] */
1337+
if (IS_HIGHBIT_SET(*ptr) ||
1338+
strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz"
1339+
"_", *ptr) != NULL)
1340+
ptr++;
1341+
else
1342+
return false;
1343+
1344+
/* remaining characters can include [0-9] */
13351345
while (*ptr)
13361346
{
13371347
if (IS_HIGHBIT_SET(*ptr) ||
@@ -1453,23 +1463,27 @@ putVariableInt(CState *st, const char *context, char *name, int64 value)
14531463
*
14541464
* "sql" points at a colon. If what follows it looks like a valid
14551465
* variable name, return a malloc'd string containing the variable name,
1456-
* and set *eaten to the number of characters consumed.
1466+
* and set *eaten to the number of characters consumed (including the colon).
14571467
* Otherwise, return NULL.
14581468
*/
14591469
static char *
14601470
parseVariable(const char *sql, int *eaten)
14611471
{
1462-
int i = 0;
1472+
int i = 1; /* starting at 1 skips the colon */
14631473
char *name;
14641474

1465-
do
1466-
{
1475+
/* keep this logic in sync with valid_variable_name() */
1476+
if (IS_HIGHBIT_SET(sql[i]) ||
1477+
strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz"
1478+
"_", sql[i]) != NULL)
1479+
i++;
1480+
else
1481+
return NULL;
1482+
1483+
while (IS_HIGHBIT_SET(sql[i]) ||
1484+
strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz"
1485+
"_0123456789", sql[i]) != NULL)
14671486
i++;
1468-
} while (IS_HIGHBIT_SET(sql[i]) ||
1469-
strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz"
1470-
"_0123456789", sql[i]) != NULL);
1471-
if (i == 1)
1472-
return NULL; /* no valid variable name chars */
14731487

14741488
name = pg_malloc(i);
14751489
memcpy(name, &sql[1], i - 1);

0 commit comments

Comments
 (0)