Skip to content

Commit

Permalink
Fix crash in Macro.to_string/1 on invalid sigils (#13905)
Browse files Browse the repository at this point in the history
  • Loading branch information
sabiwara committed Oct 17, 2024
1 parent cacb175 commit 444cd7e
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 9 deletions.
19 changes: 10 additions & 9 deletions lib/elixir/lib/code/normalizer.ex
Original file line number Diff line number Diff line change
Expand Up @@ -174,16 +174,17 @@ defmodule Code.Normalizer do

# Sigils
defp do_normalize({sigil, meta, [{:<<>>, _, args} = string, modifiers]} = quoted, state)
when is_list(args) and is_atom(sigil) do
case Atom.to_string(sigil) do
"sigil_" <> _ ->
meta =
meta
|> patch_meta_line(state.parent_meta)
|> Keyword.put_new(:delimiter, "\"")

{sigil, meta, [do_normalize(string, %{state | parent_meta: meta}), modifiers]}
when is_atom(sigil) and is_list(args) and is_list(modifiers) do
with "sigil_" <> _ <- Atom.to_string(sigil),
true <- binary_interpolated?(args),
true <- List.ascii_printable?(modifiers) do
meta =
meta
|> patch_meta_line(state.parent_meta)
|> Keyword.put_new(:delimiter, "\"")

{sigil, meta, [do_normalize(string, %{state | parent_meta: meta}), modifiers]}
else
_ ->
normalize_call(quoted, state)
end
Expand Down
14 changes: 14 additions & 0 deletions lib/elixir/test/elixir/code_normalizer/quoted_ast_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,20 @@ defmodule Code.Normalizer.QuotedASTTest do
) == ~s[~S"""\n"123"\n"""]
end

test "regression: invalid sigil calls" do
assert quoted_to_string(quote do: sigil_r(<<"foo", 123>>, [])) ==
"sigil_r(<<\"foo\", 123>>, [])"

assert quoted_to_string(quote do: sigil_r(<<"foo">>, :invalid_modifiers)) ==
"sigil_r(\"foo\", :invalid_modifiers)"

assert quoted_to_string(quote do: sigil_r(<<"foo">>, [:invalid_modifier])) ==
"sigil_r(\"foo\", [:invalid_modifier])"

assert quoted_to_string(quote do: sigil_r(<<"foo">>, [])) == "~r\"foo\""
assert quoted_to_string(quote do: sigil_r(<<"foo">>, [?a, ?b, ?c])) == "~r\"foo\"abc"
end

test "tuple" do
assert quoted_to_string(quote do: {1, 2}) == "{1, 2}"
assert quoted_to_string(quote do: {1}) == "{1}"
Expand Down

0 comments on commit 444cd7e

Please sign in to comment.