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

Commit 3e214fa

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 0363b7e commit 3e214fa

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
@@ -942,7 +942,7 @@ pgbench <optional> <replaceable>options</replaceable> </optional> <replaceable>d
942942
<para>
943943
There is a simple variable-substitution facility for script files.
944944
Variable names must consist of letters (including non-Latin letters),
945-
digits, and underscores.
945+
digits, and underscores, with the first character not being a digit.
946946
Variables can be set by the command-line <option>-D</option> option,
947947
explained above, or by the meta commands explained below.
948948
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
@@ -1356,6 +1356,7 @@ makeVariableValue(Variable *var)
13561356
* "src/bin/pgbench/exprscan.l". Also see parseVariable(), below.
13571357
*
13581358
* Note: this static function is copied from "src/bin/psql/variables.c"
1359+
* but changed to disallow variable names starting with a digit.
13591360
*/
13601361
static bool
13611362
valid_variable_name(const char *name)
@@ -1366,6 +1367,15 @@ valid_variable_name(const char *name)
13661367
if (*ptr == '\0')
13671368
return false;
13681369

1370+
/* must not start with [0-9] */
1371+
if (IS_HIGHBIT_SET(*ptr) ||
1372+
strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz"
1373+
"_", *ptr) != NULL)
1374+
ptr++;
1375+
else
1376+
return false;
1377+
1378+
/* remaining characters can include [0-9] */
13691379
while (*ptr)
13701380
{
13711381
if (IS_HIGHBIT_SET(*ptr) ||
@@ -1487,23 +1497,27 @@ putVariableInt(CState *st, const char *context, char *name, int64 value)
14871497
*
14881498
* "sql" points at a colon. If what follows it looks like a valid
14891499
* variable name, return a malloc'd string containing the variable name,
1490-
* and set *eaten to the number of characters consumed.
1500+
* and set *eaten to the number of characters consumed (including the colon).
14911501
* Otherwise, return NULL.
14921502
*/
14931503
static char *
14941504
parseVariable(const char *sql, int *eaten)
14951505
{
1496-
int i = 0;
1506+
int i = 1; /* starting at 1 skips the colon */
14971507
char *name;
14981508

1499-
do
1500-
{
1509+
/* keep this logic in sync with valid_variable_name() */
1510+
if (IS_HIGHBIT_SET(sql[i]) ||
1511+
strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz"
1512+
"_", sql[i]) != NULL)
1513+
i++;
1514+
else
1515+
return NULL;
1516+
1517+
while (IS_HIGHBIT_SET(sql[i]) ||
1518+
strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz"
1519+
"_0123456789", sql[i]) != NULL)
15011520
i++;
1502-
} while (IS_HIGHBIT_SET(sql[i]) ||
1503-
strchr("ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz"
1504-
"_0123456789", sql[i]) != NULL);
1505-
if (i == 1)
1506-
return NULL; /* no valid variable name chars */
15071521

15081522
name = pg_malloc(i);
15091523
memcpy(name, &sql[1], i - 1);

0 commit comments

Comments
 (0)