Skip to content

Commit 5caab38

Browse files
committed
Elixir library: fix crash with inspect(:atom)
inspect was returning a list instead of a binary. Also fix handling of atom special cases. Signed-off-by: Davide Bettio <davide@uninstall.it>
1 parent 678fcd8 commit 5caab38

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ instead
3838
- Fix memory corruption in `unicode:characters_to_binary`
3939
- Fix handling of large literal indexes
4040
- `unicode:characters_to_list`: fixed bogus out_of_memory error on some platforms such as ESP32
41+
- Fix crash in Elixir library when doing `inspect(:atom)`
4142

4243
## [0.6.4] - 2024-08-18
4344

libs/exavmlib/lib/Kernel.ex

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ defmodule Kernel do
4242
def inspect(term, opts \\ []) when is_list(opts) do
4343
case term do
4444
t when is_atom(t) ->
45-
[?:, atom_to_string(t)]
45+
atom_to_string(t, ":")
4646

4747
t when is_integer(t) ->
4848
:erlang.integer_to_binary(t)
@@ -118,10 +118,26 @@ defmodule Kernel do
118118
)
119119
end
120120

121-
defp atom_to_string(atom) do
122-
# TODO: use unicode rather than plain latin1
123-
# handle spaces and special characters
124-
:erlang.atom_to_binary(atom, :latin1)
121+
defp atom_to_string(atom, prefix \\ "") do
122+
case atom do
123+
true ->
124+
"true"
125+
126+
false ->
127+
"false"
128+
129+
nil ->
130+
"nil"
131+
132+
any_atom ->
133+
case :erlang.atom_to_binary(any_atom) do
134+
<<"Elixir.", displayable::binary>> ->
135+
displayable
136+
137+
other ->
138+
<<prefix::binary, other::binary>>
139+
end
140+
end
125141
end
126142

127143
@doc """

tests/libs/exavmlib/Tests.ex

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ defmodule Tests do
2626
:ok = test_enum()
2727
:ok = test_exception()
2828
:ok = test_chars_protocol()
29+
:ok = test_inspect()
2930
:ok = IO.puts("Finished Elixir tests")
3031
end
3132

@@ -234,6 +235,18 @@ defmodule Tests do
234235
:ok
235236
end
236237

238+
def test_inspect() do
239+
"true" = inspect(true)
240+
"false" = inspect(false)
241+
"nil" = inspect(nil)
242+
243+
":test" = inspect(:test)
244+
":アトム" = inspect(:アトム)
245+
"Test" = inspect(Test)
246+
247+
:ok
248+
end
249+
237250
defp fact(n) when n < 0, do: :test
238251
defp fact(0), do: 1
239252
defp fact(n), do: fact(n - 1) * n

0 commit comments

Comments
 (0)