Skip to content

Commit 5d626cb

Browse files
authored
Improve validation of unquote_splicing AST (#13961)
1 parent 9bf8233 commit 5d626cb

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

lib/elixir/src/elixir_quote.erl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -531,11 +531,12 @@ tail_list(Left, Right, Tail) when is_list(Left) ->
531531
[H | T] = lists:reverse(Tail ++ Left),
532532
lists:reverse([{'|', [], [H, Right]} | T]).
533533

534-
validate_list(List) when is_list(List) ->
535-
ok;
536-
validate_list(List) when not is_list(List) ->
537-
argument_error(<<"expected a list with quoted expressions in unquote_splicing/1, got: ",
538-
('Elixir.Kernel':inspect(List))/binary>>).
534+
validate_list(List) ->
535+
case valid_ast_list(List) of
536+
true -> ok;
537+
false -> argument_error(<<"expected a list with quoted expressions in unquote_splicing/1, got: ",
538+
('Elixir.Kernel':inspect(List))/binary>>)
539+
end.
539540

540541
argument_error(Message) ->
541542
error('Elixir.ArgumentError':exception([{message, Message}])).

lib/elixir/test/elixir/kernel/quote_test.exs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -814,4 +814,23 @@ defmodule Kernel.QuoteTest.HasUnquoteTest do
814814
assert quote do: unquote(foo: [1 | 2]) == [foo: [1 | 2]]
815815
assert quote do: unquote(foo: [bar: %{}]) == [foo: [bar: %{}]]
816816
end
817+
818+
test "unquote_splicing with invalid AST" do
819+
for args <- [
820+
"not_a_list",
821+
[:improper | :list],
822+
[%{unescaped: :map}],
823+
[1..10],
824+
[{:bad_meta, nil, []}],
825+
[{:bad_arg, nil, 1}],
826+
[{:bad_tuple}],
827+
[make_ref()],
828+
[nested: {}]
829+
] do
830+
message =
831+
"expected a list with quoted expressions in unquote_splicing/1, got: #{inspect(args)}"
832+
833+
assert_raise ArgumentError, message, fn -> quote do: [unquote_splicing(args)] end
834+
end
835+
end
817836
end

0 commit comments

Comments
 (0)