1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-07 00:36:50 +03:00

SQL/JSON: Correct jsonpath variable name matching

Previously, GetJsonPathVar() allowed a jsonpath expression to
reference any prefix of a PASSING variable's name. For example, the
following query would incorrectly work:

SELECT JSON_QUERY(context_item, jsonpath '$xy' PASSING val AS xyz);

The fix ensures that the length of the variable name mentioned in a
jsonpath expression matches exactly with the name of the PASSING
variable before comparing the strings using strncmp().

Reported-by: Alvaro Herrera (off-list)
Discussion: https://postgr.es/m/CA+HiwqFGkLWMvELBH6E4SQ45qUHthgcRH6gCJL20OsYDRtFx_w@mail.gmail.com
This commit is contained in:
Amit Langote
2024-06-19 15:22:06 +09:00
parent 5e05a0e992
commit 0f271e8e8d
5 changed files with 21 additions and 1 deletions

View File

@ -4278,6 +4278,7 @@ ExecInitJsonExpr(JsonExpr *jsexpr, ExprState *state,
JsonPathVariable *var = palloc(sizeof(*var));
var->name = argname->sval;
var->namelen = strlen(var->name);
var->typid = exprType((Node *) argexpr);
var->typmod = exprTypmod((Node *) argexpr);

View File

@ -2994,7 +2994,8 @@ GetJsonPathVar(void *cxt, char *varName, int varNameLen,
{
JsonPathVariable *curvar = lfirst(lc);
if (!strncmp(curvar->name, varName, varNameLen))
if (curvar->namelen == varNameLen &&
strncmp(curvar->name, varName, varNameLen) == 0)
{
var = curvar;
break;
@ -4118,6 +4119,7 @@ JsonTableInitOpaque(TableFuncScanState *state, int natts)
JsonPathVariable *var = palloc(sizeof(*var));
var->name = pstrdup(name->sval);
var->namelen = strlen(var->name);
var->typid = exprType((Node *) state->expr);
var->typmod = exprTypmod((Node *) state->expr);