Skip to content

Commit aa18ef1

Browse files
committed
fix false alarm - unreachable code
1 parent 6a8074e commit aa18ef1

File tree

3 files changed

+106
-1
lines changed

3 files changed

+106
-1
lines changed

expected/plpgsql_check_active.out

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3554,3 +3554,57 @@ select * from plpgsql_check_function('test_crash', fatal_errors := true);
35543554
(5 rows)
35553555

35563556
drop function test_crash();
3557+
-- fix false alarm reported by Piotr Stepniewski
3558+
create or replace function public.fx()
3559+
returns void
3560+
language plpgsql
3561+
as $function$
3562+
begin
3563+
raise exception 'xxx';
3564+
end;
3565+
$function$;
3566+
-- show raise nothing
3567+
select * from plpgsql_check_function('fx()');
3568+
plpgsql_check_function
3569+
------------------------
3570+
(0 rows)
3571+
3572+
create table errtab(
3573+
message text,
3574+
code character(5)
3575+
);
3576+
create or replace function public.fx()
3577+
returns void
3578+
language plpgsql
3579+
as $function$
3580+
declare
3581+
var errtab%rowtype;
3582+
begin
3583+
raise exception using message = var.message, errcode = var.code;
3584+
end;
3585+
$function$;
3586+
-- should not to crash
3587+
select * from plpgsql_check_function('fx()');
3588+
plpgsql_check_function
3589+
------------------------
3590+
(0 rows)
3591+
3592+
create or replace function public.fx()
3593+
returns void
3594+
language plpgsql
3595+
as $function$
3596+
declare
3597+
var errtab%rowtype;
3598+
begin
3599+
raise exception using message = var.message, errcode = var.code, hint = var.hint;
3600+
end;
3601+
$function$;
3602+
-- should not to crash
3603+
select * from plpgsql_check_function('fx()');
3604+
plpgsql_check_function
3605+
------------------------------------------------------
3606+
error:42703:5:RAISE:record "var" has no field "hint"
3607+
Context: SQL statement "SELECT var.hint"
3608+
(2 rows)
3609+
3610+
drop function fx();

sql/plpgsql_check_active.sql

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2580,3 +2580,53 @@ select * from plpgsql_check_function('test_crash', fatal_errors := false);
25802580
select * from plpgsql_check_function('test_crash', fatal_errors := true);
25812581

25822582
drop function test_crash();
2583+
2584+
-- fix false alarm reported by Piotr Stepniewski
2585+
create or replace function public.fx()
2586+
returns void
2587+
language plpgsql
2588+
as $function$
2589+
begin
2590+
raise exception 'xxx';
2591+
end;
2592+
$function$;
2593+
2594+
-- show raise nothing
2595+
select * from plpgsql_check_function('fx()');
2596+
2597+
create table errtab(
2598+
message text,
2599+
code character(5)
2600+
);
2601+
2602+
create or replace function public.fx()
2603+
returns void
2604+
language plpgsql
2605+
as $function$
2606+
declare
2607+
var errtab%rowtype;
2608+
begin
2609+
raise exception using message = var.message, errcode = var.code;
2610+
end;
2611+
$function$;
2612+
2613+
-- should not to crash
2614+
select * from plpgsql_check_function('fx()');
2615+
2616+
create or replace function public.fx()
2617+
returns void
2618+
language plpgsql
2619+
as $function$
2620+
declare
2621+
var errtab%rowtype;
2622+
begin
2623+
raise exception using message = var.message, errcode = var.code, hint = var.hint;
2624+
end;
2625+
$function$;
2626+
2627+
-- should not to crash
2628+
select * from plpgsql_check_function('fx()');
2629+
2630+
drop function fx();
2631+
2632+

src/stmtwalk.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1347,7 +1347,8 @@ check_stmts(PLpgSQL_checkstate *cstate, List *stmts, int *closing, List **except
13471347
exceptions_local = NIL;
13481348
plpgsql_check_stmt(cstate, stmt, &closing_local, &exceptions_local);
13491349

1350-
if (dead_code_alert)
1350+
/* raise dead_code_alert only for visible statements */
1351+
if (dead_code_alert && stmt->lineno > 0)
13511352
{
13521353
plpgsql_check_put_error(cstate,
13531354
0, stmt->lineno,

0 commit comments

Comments
 (0)