Skip to content

Improve characters_to_binary! error handling #1199

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion apps/debug_adapter/lib/debug_adapter/utils.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ defmodule ElixirLS.DebugAdapter.Utils do

defp characters_to_binary!(binary, from, to) do
case :unicode.characters_to_binary(binary, from, to) do
result when is_binary(result) -> result
result when is_binary(result) ->
result

other ->
raise ArgumentError,
message: "could not convert characters to binary: #{inspect(other)}"
end
end

Expand Down
6 changes: 6 additions & 0 deletions apps/debug_adapter/test/utils_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,11 @@ defmodule ElixirLS.DebugAdapter.UtilsTest do
assert 6 == Utils.dap_character_to_elixir("Hello 🙌 World", 7)
assert 7 == Utils.dap_character_to_elixir("Hello 🙌 World", 8)
end

test "dap_character_to_elixir invalid utf8" do
assert_raise ArgumentError, ~r/could not convert characters/, fn ->
Utils.dap_character_to_elixir(<<0x80>>, 1)
end
end
end
end
7 changes: 6 additions & 1 deletion apps/language_server/lib/language_server/source_file.ex
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,12 @@ defmodule ElixirLS.LanguageServer.SourceFile do

defp characters_to_binary!(binary, from, to) do
case :unicode.characters_to_binary(binary, from, to) do
result when is_binary(result) -> result
result when is_binary(result) ->
result

other ->
raise ArgumentError,
message: "could not convert characters to binary: #{inspect(other)}"
end
end

Expand Down
8 changes: 8 additions & 0 deletions apps/language_server/test/source_file_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,14 @@ defmodule ElixirLS.LanguageServer.SourceFileTest do
end
end

describe "characters_to_binary!/3" do
test "raises for invalid utf8" do
assert_raise ArgumentError, ~r/could not convert characters/, fn ->
SourceFile.line_length_utf16(<<0x80>>)
end
end
end

describe "positions" do
test "lsp_position_to_elixir empty" do
assert {1, 1} == SourceFile.lsp_position_to_elixir("", {0, 0})
Expand Down
Loading