Skip to content

Commit

Permalink
Merge pull request #11736 from rabbitmq/mergify/bp/v4.0.x/pr-11715
Browse files Browse the repository at this point in the history
mc: increase utf8 scanning limit for longstr conversions. (backport #11715)
  • Loading branch information
acogoluegnes authored Jul 17, 2024
2 parents faa9966 + cc00235 commit 6ca765b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
18 changes: 11 additions & 7 deletions deps/rabbit/src/mc_amqpl.erl
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
-define(AMQP10_FOOTER, <<"x-amqp-1.0-footer">>).
-define(PROTOMOD, rabbit_framing_amqp_0_9_1).
-define(CLASS_ID, 60).
-define(LONGSTR_UTF8_LIMIT, 4096).

-opaque state() :: #content{}.

Expand Down Expand Up @@ -663,16 +664,19 @@ wrap(_Type, undefined) ->
wrap(Type, Val) ->
{Type, Val}.

from_091(longstr, V) ->
case mc_util:is_valid_shortstr(V) of
from_091(longstr, V)
when is_binary(V) andalso
byte_size(V) =< ?LONGSTR_UTF8_LIMIT ->
%% if a longstr is longer than 4096 bytes we just assume it is binary
%% it _may_ still be valid utf8 but checking this for every longstr header
%% value is going to be excessively slow
case mc_util:is_utf8_no_null(V) of
true ->
{utf8, V};
false ->
%% if a string is longer than 255 bytes we just assume it is binary
%% it _may_ still be valid utf8 but checking this is going to be
%% excessively slow
{binary, V}
end;
from_091(longstr, V) -> {binary, V};
from_091(long, V) -> {long, V};
from_091(unsignedbyte, V) -> {ubyte, V};
from_091(short, V) -> {short, V};
Expand Down Expand Up @@ -743,7 +747,7 @@ to_091(Key, {int, V}) -> {Key, signedint, V};
to_091(Key, {double, V}) -> {Key, double, V};
to_091(Key, {float, V}) -> {Key, float, V};
to_091(Key, {timestamp, V}) -> {Key, timestamp, V div 1000};
to_091(Key, {binary, V}) -> {Key, binary, V};
to_091(Key, {binary, V}) -> {Key, longstr, V};
to_091(Key, {boolean, V}) -> {Key, bool, V};
to_091(Key, true) -> {Key, bool, true};
to_091(Key, false) -> {Key, bool, false};
Expand All @@ -766,7 +770,7 @@ to_091({int, V}) -> {signedint, V};
to_091({double, V}) -> {double, V};
to_091({float, V}) -> {float, V};
to_091({timestamp, V}) -> {timestamp, V div 1000};
to_091({binary, V}) -> {binary, V};
to_091({binary, V}) -> {longstr, V};
to_091({boolean, V}) -> {bool, V};
to_091(true) -> {bool, true};
to_091(false) -> {bool, false};
Expand Down
2 changes: 1 addition & 1 deletion deps/rabbit/test/mc_unit_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ amqp_amqpl(_Config) ->
?assertMatch({_, long, 5}, header(<<"long">>, HL)),
?assertMatch({_, long, 5}, header(<<"ulong">>, HL)),
?assertMatch({_, longstr, <<"a-string">>}, header(<<"utf8">>, HL)),
?assertMatch({_, binary, <<"data">>}, header(<<"binary">>, HL)),
?assertMatch({_, longstr, <<"data">>}, header(<<"binary">>, HL)),
?assertMatch({_, longstr, <<"symbol">>}, header(<<"symbol">>, HL)),
?assertMatch({_, unsignedbyte, 255}, header(<<"ubyte">>, HL)),
?assertMatch({_, short, 2}, header(<<"short">>, HL)),
Expand Down

0 comments on commit 6ca765b

Please sign in to comment.