Skip to content
Permalink
Browse files

check parameter, and variables against reserved keyword list

  • Loading branch information...
okbob committed Feb 28, 2019
1 parent b8c7fc2 commit 5b9ef57d570c1d11fb92b9cff76655a03767f662
Showing with 80 additions and 0 deletions.
  1. +27 −0 src/check_function.c
  2. +1 −0 src/plpgsql_check.h
  3. +52 −0 src/stmtwalk.c
@@ -485,13 +485,40 @@ function_check(PLpgSQL_function *func, FunctionCallInfo fcinfo,
int i;
int closing = PLPGSQL_CHECK_UNCLOSED;
List *exceptions;
ListCell *lc;

/*
* Make local execution copies of all the datums
*/
for (i = 0; i < cstate->estate->ndatums; i++)
cstate->estate->datums[i] = copy_plpgsql_datum(cstate, func->datums[i]);

/*
* check function's parameters to not be reserved keywords
*/
foreach(lc, cstate->argnames)
{
char *argname = (char *) lfirst(lc);

if (plpgsql_check_is_reserved_keyword(argname))
{
StringInfoData str;

initStringInfo(&str);
appendStringInfo(&str, "name of parameter \"%s\" is reserved keyword",
argname);

plpgsql_check_put_error(cstate,
0, 0,
str.data,
"The reserved keyword was used as parameter name.",
NULL,
PLPGSQL_CHECK_WARNING_OTHERS,
0, NULL, NULL);
pfree(str.data);
}
}

/*
* Store the actual call argument values (fake) into the appropriate
* variables
@@ -213,6 +213,7 @@ extern void plpgsql_check_report_too_high_volatility(PLpgSQL_checkstate *cstate)
/*
* functions from stmtwalk.c
*/
extern bool plpgsql_check_is_reserved_keyword(char *name);
extern void plpgsql_check_stmt(PLpgSQL_checkstate *cstate, PLpgSQL_stmt *stmt, int *closing, List **exceptions);

/*
@@ -13,6 +13,7 @@

#include "access/tupconvert.h"
#include "catalog/pg_type.h"
#include "common/keywords.h"

static void check_stmts(PLpgSQL_checkstate *cstate, List *stmts, int *closing, List **exceptions);
static PLpgSQL_stmt_stack_item * push_stmt_to_stmt_stack(PLpgSQL_checkstate *cstate);
@@ -71,6 +72,39 @@ check_variable(PLpgSQL_checkstate *cstate, PLpgSQL_variable *var)

#endif

bool
plpgsql_check_is_reserved_keyword(char *name)
{
int i;

#if PG_VERSION_NUM <= 120000

for (i = 0; i < NumScanKeywords; i++)
{
if (ScanKeywords[i].category == RESERVED_KEYWORD &&
strcmp(name, ScanKeywords[i].name) == 0)
return true;
}

#else

for (i = 0; i < ScanKeywords.num_keywords; i++)
{
if (ScanKeywordCategories[i] == RESERVED_KEYWORD)
{
char *value;

value = unconstify(char *, GetScanKeyword(i, &ScanKeywords));
if (strcmp(name, value) == 0)
return true;
}
}

#endif

return false;
}

/*
* walk over all plpgsql statements - search and check expressions
*
@@ -152,6 +186,24 @@ plpgsql_check_stmt(PLpgSQL_checkstate *cstate, PLpgSQL_stmt *stmt, int *closing,
{
ListCell *l;

if (plpgsql_check_is_reserved_keyword(refname))
{
StringInfoData str;

initStringInfo(&str);
appendStringInfo(&str, "name of variable \"%s\" is reserved keyword",
refname);

plpgsql_check_put_error(cstate,
0, 0,
str.data,
"The reserved keyword was used as variable name.",
NULL,
PLPGSQL_CHECK_WARNING_OTHERS,
0, NULL, NULL);
pfree(str.data);
}

foreach(l, cstate->argnames)
{
char *argname = (char *) lfirst(l);

0 comments on commit 5b9ef57

Please sign in to comment.
You can’t perform that action at this time.