Merge pull request #13140 from kjellwinblad/kjell/republish_trace_crash/EMQX-12474

fix: trace formatter should not crash when given a non-binary payload
This commit is contained in:
Kjell Winblad 2024-05-29 15:54:02 +02:00 committed by GitHub
commit 3769bbb7f4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 17 additions and 3 deletions

View File

@ -89,8 +89,17 @@ weight({packet, _}) -> {0, packet};
weight({payload, _}) -> {2, payload}; weight({payload, _}) -> {2, payload};
weight({K, _}) -> {1, K}. weight({K, _}) -> {1, K}.
format_packet(undefined, _) -> ""; format_packet(undefined, _) ->
format_packet(Packet, Encode) -> emqx_packet:format(Packet, Encode). "";
format_packet(Packet, Encode) ->
try
emqx_packet:format(Packet, Encode)
catch
_:_ ->
%% We don't want to crash if there is a field named packet with
%% some other type of value
Packet
end.
format_payload(undefined, _) -> format_payload(undefined, _) ->
""; "";
@ -100,7 +109,11 @@ format_payload(Payload, text) when ?MAX_PAYLOAD_FORMAT_LIMIT(Payload) ->
unicode:characters_to_list(Payload); unicode:characters_to_list(Payload);
format_payload(Payload, hex) when ?MAX_PAYLOAD_FORMAT_LIMIT(Payload) -> binary:encode_hex(Payload); format_payload(Payload, hex) when ?MAX_PAYLOAD_FORMAT_LIMIT(Payload) -> binary:encode_hex(Payload);
format_payload(<<Part:?TRUNCATED_PAYLOAD_SIZE/binary, _/binary>> = Payload, Type) -> format_payload(<<Part:?TRUNCATED_PAYLOAD_SIZE/binary, _/binary>> = Payload, Type) ->
emqx_packet:format_truncated_payload(Part, byte_size(Payload), Type). emqx_packet:format_truncated_payload(Part, byte_size(Payload), Type);
format_payload(Payload, _) ->
%% We don't want to crash if there is a field named payload with some other
%% type of value
Payload.
to_iolist(Atom) when is_atom(Atom) -> atom_to_list(Atom); to_iolist(Atom) when is_atom(Atom) -> atom_to_list(Atom);
to_iolist(Int) when is_integer(Int) -> integer_to_list(Int); to_iolist(Int) when is_integer(Int) -> integer_to_list(Int);

View File

@ -0,0 +1 @@
The issue causing text traces for the republish action to crash and not display correctly has been resolved.