@@ -1020,19 +1020,38 @@ makeVariableNumeric(Variable *var)
1020
1020
return true;
1021
1021
}
1022
1022
1023
- /* check whether the name consists of alphabets, numerals and underscores. */
1023
+ /*
1024
+ * Check whether a variable's name is allowed.
1025
+ *
1026
+ * We allow any non-ASCII character, as well as ASCII letters, digits, and
1027
+ * underscore.
1028
+ *
1029
+ * Keep this in sync with the definitions of variable name characters in
1030
+ * "src/fe_utils/psqlscan.l", "src/bin/psql/psqlscanslash.l" and
1031
+ * "src/bin/pgbench/exprscan.l". Also see parseVariable(), below.
1032
+ *
1033
+ * Note: this static function is copied from "src/bin/psql/variables.c"
1034
+ */
1024
1035
static bool
1025
- isLegalVariableName (const char * name )
1036
+ valid_variable_name (const char * name )
1026
1037
{
1027
- int i ;
1038
+ const unsigned char * ptr = (const unsigned char * ) name ;
1039
+
1040
+ /* Mustn't be zero-length */
1041
+ if (* ptr == '\0' )
1042
+ return false;
1028
1043
1029
- for ( i = 0 ; name [ i ] != '\0' ; i ++ )
1044
+ while ( * ptr )
1030
1045
{
1031
- if (!isalnum ((unsigned char ) name [i ]) && name [i ] != '_' )
1046
+ if (IS_HIGHBIT_SET (* ptr ) ||
1047
+ strchr ("ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz"
1048
+ "_0123456789" , * ptr ) != NULL )
1049
+ ptr ++ ;
1050
+ else
1032
1051
return false;
1033
1052
}
1034
1053
1035
- return ( i > 0 ); /* must be non-empty */
1054
+ return true;
1036
1055
}
1037
1056
1038
1057
/*
@@ -1054,7 +1073,7 @@ lookupCreateVariable(CState *st, const char *context, char *name)
1054
1073
* Check for the name only when declaring a new variable to avoid
1055
1074
* overhead.
1056
1075
*/
1057
- if (!isLegalVariableName (name ))
1076
+ if (!valid_variable_name (name ))
1058
1077
{
1059
1078
fprintf (stderr , "%s: invalid variable name: \"%s\"\n" ,
1060
1079
context , name );
@@ -1139,6 +1158,14 @@ putVariableInt(CState *st, const char *context, char *name, int64 value)
1139
1158
return putVariableNumber (st , context , name , & val );
1140
1159
}
1141
1160
1161
+ /*
1162
+ * Parse a possible variable reference (:varname).
1163
+ *
1164
+ * "sql" points at a colon. If what follows it looks like a valid
1165
+ * variable name, return a malloc'd string containing the variable name,
1166
+ * and set *eaten to the number of characters consumed.
1167
+ * Otherwise, return NULL.
1168
+ */
1142
1169
static char *
1143
1170
parseVariable (const char * sql , int * eaten )
1144
1171
{
@@ -1148,9 +1175,11 @@ parseVariable(const char *sql, int *eaten)
1148
1175
do
1149
1176
{
1150
1177
i ++ ;
1151
- } while (isalnum ((unsigned char ) sql [i ]) || sql [i ] == '_' );
1178
+ } while (IS_HIGHBIT_SET (sql [i ]) ||
1179
+ strchr ("ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz"
1180
+ "_0123456789" , sql [i ]) != NULL );
1152
1181
if (i == 1 )
1153
- return NULL ;
1182
+ return NULL ; /* no valid variable name chars */
1154
1183
1155
1184
name = pg_malloc (i );
1156
1185
memcpy (name , & sql [1 ], i - 1 );
0 commit comments