Skip to content

Commit

Permalink
feat: set printable characters range to unicode
Browse files Browse the repository at this point in the history
  • Loading branch information
thalesmg committed Oct 24, 2024
1 parent c79cb86 commit 2c0f490
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
3 changes: 3 additions & 0 deletions apps/emqx/etc/vm.args.cloud
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,6 @@

## Disable os_mon's disksup by default
-os_mon start_disksup false

## Sets unicode as the printable character range when formatting binaries
+pc unicode
2 changes: 1 addition & 1 deletion apps/emqx_utils/src/emqx_utils.erl
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,7 @@ tcp_keepalive_opts(OS, _Idle, _Interval, _Probes) ->
{error, {unsupported_os, OS}}.

format(Term) ->
unicode:characters_to_binary(io_lib:format("~0p", [Term])).
unicode:characters_to_binary(io_lib:format("~0tp", [Term])).

format(Fmt, Args) ->
unicode:characters_to_binary(io_lib:format(Fmt, Args)).
Expand Down
27 changes: 27 additions & 0 deletions apps/emqx_utils/test/emqx_utils_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,30 @@ foldl_while_test_() ->
)
)
].

to_binary_representation(Bin) when is_binary(Bin) ->
Bytes = binary_to_list(Bin),
iolist_to_binary([
[$<, $<],
lists:join($,, [ integer_to_binary(B)
|| B <- Bytes
]),
[$>, $>]
]).

readable_error_msg_test_() ->
[
{ "binary in nested structure with non-latin1 characters"
, ?_assert(begin
%% An unexpected error that could occur and be returned via HTTP API.
Text = <<"test中文"/utf8>>,
Error = {badmatch, #{description => Text}},
%% Output shouldn't contain the "exploded" bytes
Exploded = to_binary_representation(Text),
Formatted = emqx_utils:readable_error_msg(Error),
%% Precondition: `+pc unicode' must be set on the VM.
?assertEqual(unicode, io:printable_range()),
nomatch =:= re:run(Formatted, Exploded, [{capture, all, binary}])
end)
}
].

0 comments on commit 2c0f490

Please sign in to comment.