Skip to content

Commit ee41983

Browse files
committed
Fix crash caused by mishandling of non-ascii amqp_error explaination
`rabbit_binary_generator:map_exception/3` will crash when there are unicode characters in the `explaination` field of `Reason#amqp_error` parameter. The explaination string (list) is assumed to be ascii, with each character/member in the range of a byte. Any unicode characters in the string will trigger `badarg` crash of `list_to_binary/1` in `rabbit_binary_generator:amqp_exception_explanation/2`. Amqp091 shovel crash due to this is reported, #12874 When a queue as shovel source/destination does not exist, and its name contains non-ascii characters, the explaination of amqp_error will be like `no queue non_ascii_name_😍 in vhost /`. It will subsequently crash and even affect management console. To fix this, `unicode:characters_to_binary/1` is used instead of `list_to_binary/1`, and unicode-safe truncation of long explaination with `io_lib:format/3` chars_limit replaces direct bytes truncation.
1 parent 3809143 commit ee41983

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

deps/rabbit_common/src/rabbit_binary_generator.erl

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,5 @@ lookup_amqp_exception(Other, Protocol) ->
228228
{ShouldClose, Code, Text, none}.
229229

230230
amqp_exception_explanation(Text, Expl) ->
231-
ExplBin = list_to_binary(Expl),
232-
CompleteTextBin = <<Text/binary, " - ", ExplBin/binary>>,
233-
if size(CompleteTextBin) > 255 -> <<CompleteTextBin:252/binary, "...">>;
234-
true -> CompleteTextBin
235-
end.
231+
LimitedText = io_lib:format("~ts - ~ts", [Text, Expl], [{chars_limit, 255}]),
232+
unicode:characters_to_binary(LimitedText).

deps/rabbit_common/test/unit_SUITE.erl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ groups() ->
4646
pid_decompose_compose,
4747
platform_and_version,
4848
frame_encoding_does_not_fail_with_empty_binary_payload,
49+
map_exception_does_not_fail_with_unicode_explaination,
4950
amqp_table_conversion,
5051
name_type,
5152
get_erl_path,
@@ -415,6 +416,13 @@ frame_encoding_does_not_fail_with_empty_binary_payload(_Config) ->
415416
]],
416417
ok.
417418

419+
map_exception_does_not_fail_with_unicode_explaination(_Config) ->
420+
NonAsciiExplaination = "no queue 'non_ascii_name_😍_你好' in vhost '/'",
421+
rabbit_binary_generator:map_exception(0,
422+
#amqp_error{name = not_found, explanation = NonAsciiExplaination, method = 'queue.declare'},
423+
rabbit_framing_amqp_0_9_1),
424+
ok.
425+
418426
amqp_table_conversion(_Config) ->
419427
assert_table(#{}, []),
420428
assert_table(#{<<"x-expires">> => 1000},

0 commit comments

Comments
 (0)