Skip to content

Commit 9902989

Browse files
committed
Properly ignore syntax errors from placeholders
The old code tried to skip parsing when there are any string literals in the dynamic SQL but due to a suspected typo (false instead of true) it did not actually do so. An example where literal placeholders can cause syntax errors in valid code is format('SELECT json %L', v) which check_dynamic_sql() transforms into "SELECT json null" which is invalid SQL in PostgreSQL <=13.
1 parent fea2ee4 commit 9902989

File tree

6 files changed

+58
-1
lines changed

6 files changed

+58
-1
lines changed

expected/plpgsql_check_active.out

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8187,3 +8187,15 @@ select lineno, exec_stmts, exec_stmts_err, source from plpgsql_profiler_function
81878187
drop function test_function(int);
81888188
drop function test_function1(int);
81898189
set plpgsql_check.profiler to off;
8190+
-- ignores syntax errors when literals placehodlers are used
8191+
create function test_function()
8192+
returns void as $$
8193+
begin
8194+
execute format('do %L', 'begin end');
8195+
end
8196+
$$ language plpgsql;
8197+
select * from plpgsql_check_function('test_function');
8198+
plpgsql_check_function
8199+
------------------------
8200+
(0 rows)
8201+

expected/plpgsql_check_active_1.out

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8206,3 +8206,15 @@ select lineno, exec_stmts, exec_stmts_err, source from plpgsql_profiler_function
82068206
drop function test_function(int);
82078207
drop function test_function1(int);
82088208
set plpgsql_check.profiler to off;
8209+
-- ignores syntax errors when literals placehodlers are used
8210+
create function test_function()
8211+
returns void as $$
8212+
begin
8213+
execute format('do %L', 'begin end');
8214+
end
8215+
$$ language plpgsql;
8216+
select * from plpgsql_check_function('test_function');
8217+
plpgsql_check_function
8218+
------------------------
8219+
(0 rows)
8220+

expected/plpgsql_check_active_2.out

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8186,3 +8186,15 @@ select lineno, exec_stmts, exec_stmts_err, source from plpgsql_profiler_function
81868186
drop function test_function(int);
81878187
drop function test_function1(int);
81888188
set plpgsql_check.profiler to off;
8189+
-- ignores syntax errors when literals placehodlers are used
8190+
create function test_function()
8191+
returns void as $$
8192+
begin
8193+
execute format('do %L', 'begin end');
8194+
end
8195+
$$ language plpgsql;
8196+
select * from plpgsql_check_function('test_function');
8197+
plpgsql_check_function
8198+
------------------------
8199+
(0 rows)
8200+

expected/plpgsql_check_active_3.out

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8186,3 +8186,15 @@ select lineno, exec_stmts, exec_stmts_err, source from plpgsql_profiler_function
81868186
drop function test_function(int);
81878187
drop function test_function1(int);
81888188
set plpgsql_check.profiler to off;
8189+
-- ignores syntax errors when literals placehodlers are used
8190+
create function test_function()
8191+
returns void as $$
8192+
begin
8193+
execute format('do %L', 'begin end');
8194+
end
8195+
$$ language plpgsql;
8196+
select * from plpgsql_check_function('test_function');
8197+
plpgsql_check_function
8198+
------------------------
8199+
(0 rows)
8200+

sql/plpgsql_check_active.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4748,3 +4748,12 @@ drop function test_function1(int);
47484748

47494749
set plpgsql_check.profiler to off;
47504750

4751+
-- ignores syntax errors when literals placehodlers are used
4752+
create function test_function()
4753+
returns void as $$
4754+
begin
4755+
execute format('do %L', 'begin end');
4756+
end
4757+
$$ language plpgsql;
4758+
4759+
select * from plpgsql_check_function('test_function');

src/stmtwalk.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1900,7 +1900,7 @@ check_dynamic_sql(PLpgSQL_checkstate *cstate,
19001900
* so most safe value is NULL instead.
19011901
*/
19021902
appendStringInfo(&sinfo, " null ");
1903-
found_literal_placeholder = false;
1903+
found_literal_placeholder = true;
19041904
expr_is_const = false;
19051905
}
19061906
else

0 commit comments

Comments
 (0)