Skip to content

Commit

Permalink
fix false alarm when constant variable was initialized by default value
Browse files Browse the repository at this point in the history
  • Loading branch information
okbob committed May 5, 2022
1 parent c484572 commit b5561bc
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
13 changes: 13 additions & 0 deletions expected/plpgsql_check_active-15.out
Original file line number Diff line number Diff line change
Expand Up @@ -588,3 +588,16 @@ select * from plpgsql_check_function('f1');

drop function f1();
drop procedure p1(int, int);
create or replace function f1()
returns int as $$
declare c constant int default 100;
begin
return c;
end;
$$ language plpgsql;
-- should be ok
select * from plpgsql_check_function('f1');
plpgsql_check_function
------------------------
(0 rows)

11 changes: 11 additions & 0 deletions sql/plpgsql_check_active-15.sql
Original file line number Diff line number Diff line change
Expand Up @@ -361,3 +361,14 @@ select * from plpgsql_check_function('f1');

drop function f1();
drop procedure p1(int, int);

create or replace function f1()
returns int as $$
declare c constant int default 100;
begin
return c;
end;
$$ language plpgsql;

-- should be ok
select * from plpgsql_check_function('f1');
8 changes: 7 additions & 1 deletion src/assign.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,13 @@ plpgsql_check_target(PLpgSQL_checkstate *cstate, int varno, Oid *expected_typoid
{
PLpgSQL_datum *target = cstate->estate->datums[varno];

plpgsql_check_is_assignable(cstate->estate, varno);
/*
* The target should be not constant, but we can allow assignment to
* constant variable at block statement - it is using default value.
*/
if (cstate->estate->err_stmt->cmd_type != PLPGSQL_STMT_BLOCK)
plpgsql_check_is_assignable(cstate->estate, varno);

plpgsql_check_record_variable_usage(cstate, varno, true);

switch (target->dtype)
Expand Down

0 comments on commit b5561bc

Please sign in to comment.