Skip to content

Commit

Permalink
enhance message of unmodified OUT variables when was used RETURN QUER…
Browse files Browse the repository at this point in the history
…Y EXECUTE command
  • Loading branch information
okbob committed Apr 27, 2020
1 parent b1f681e commit 2e9d4bd
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/check_function.c
Original file line number Diff line number Diff line change
Expand Up @@ -1042,6 +1042,7 @@ plpgsql_check_setup_cstate(PLpgSQL_checkstate *cstate,
#endif

cstate->found_return_query = false;
cstate->found_return_dyn_query = false;

cstate->fake_rtd = fake_rtd;

Expand Down
2 changes: 2 additions & 0 deletions src/plpgsql_check.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ typedef struct PLpgSQL_checkstate
Bitmapset *modif_variables; /* track which variables had been changed; bit per varno */
PLpgSQL_stmt_stack_item *top_stmt_stack; /* list of known labels + related command */
bool found_return_query; /* true, when code contains RETURN query */
bool found_return_dyn_query; /* true, when code contains RETURN QUERY EXECUTE */
Bitmapset *func_oids; /* list of used (and displayed) functions */
Bitmapset *rel_oids; /* list of used (and displayed) relations */
bool fake_rtd; /* true when functions returns record */
Expand Down Expand Up @@ -324,6 +325,7 @@ extern plpgsql_check__recognize_err_condition_t plpgsql_check__recognize_err_con
#define UNUSED_PARAMETER_TEXT "unused parameter \"%s\""
#define NEVER_READ_PARAMETER_TEXT "parameter \"%s\" is never read"
#define UNMODIFIED_VARIABLE_TEXT "unmodified OUT variable \"%s\""
#define MAYBE_UNMODIFIED_VARIABLE_TEXT "OUT variable \"%s\" is maybe unmodified"
#define OUT_COMPOSITE_IS_NOT_SINGLE_TEXT "composite OUT variable \"%s\" is not single argument"
#define UNSAFE_EXECUTE "the expression used by EXECUTE command is possibly sql injection vulnerable"

Expand Down
20 changes: 16 additions & 4 deletions src/report.c
Original file line number Diff line number Diff line change
Expand Up @@ -357,12 +357,18 @@ plpgsql_check_report_unused_variables(PLpgSQL_checkstate *cstate)

if (!datum_is_used(cstate, varno2, true))
{
const char *fmt = cstate->found_return_dyn_query ?
MAYBE_UNMODIFIED_VARIABLE_TEXT : UNMODIFIED_VARIABLE_TEXT;

const char *detail = cstate->found_return_dyn_query ?
"cannot to determine result of dynamic SQL" : NULL;

initStringInfo(&message);
appendStringInfo(&message, UNMODIFIED_VARIABLE_TEXT, var->refname);
appendStringInfo(&message, fmt, var->refname);
plpgsql_check_put_error(cstate,
0, 0,
message.data,
NULL,
detail,
NULL,
PLPGSQL_CHECK_WARNING_EXTRA,
0, NULL, NULL);
Expand All @@ -379,13 +385,19 @@ plpgsql_check_report_unused_variables(PLpgSQL_checkstate *cstate)
PLpgSQL_variable *var = (PLpgSQL_variable *) estate->datums[varno];
StringInfoData message;

const char *fmt = cstate->found_return_dyn_query ?
MAYBE_UNMODIFIED_VARIABLE_TEXT : UNMODIFIED_VARIABLE_TEXT;

const char *detail = cstate->found_return_dyn_query ?
"cannot to determine result of dynamic SQL" : NULL;

initStringInfo(&message);

appendStringInfo(&message, UNMODIFIED_VARIABLE_TEXT, var->refname);
appendStringInfo(&message, fmt, var->refname);
plpgsql_check_put_error(cstate,
0, 0,
message.data,
NULL,
detail,
NULL,
PLPGSQL_CHECK_WARNING_EXTRA,
0, NULL, NULL);
Expand Down
6 changes: 6 additions & 0 deletions src/stmtwalk.c
Original file line number Diff line number Diff line change
Expand Up @@ -1956,6 +1956,12 @@ check_dynamic_sql(PLpgSQL_checkstate *cstate,
NULL);
}

/* in this case we don't know number of output columns */
if (stmt->cmd_type == PLPGSQL_STMT_RETURN_QUERY)
{
cstate->found_return_dyn_query = true;
}

/*
* In this case, we don't know a result type, and we should
* to raise warning about this situation.
Expand Down

0 comments on commit 2e9d4bd

Please sign in to comment.