fix(frame): fix empty topic check error

This commit is contained in:
firest 2022-04-08 17:58:45 +08:00
parent 7af9919823
commit 907e643e20
2 changed files with 20 additions and 14 deletions

View File

@ -301,7 +301,7 @@ parse_packet(
Bin, Bin,
#{strict_mode := StrictMode, version := Ver} #{strict_mode := StrictMode, version := Ver}
) -> ) ->
{TopicName, Rest} = parse_topic_name(Bin, StrictMode), {TopicName, Rest} = parse_utf8_string(Bin, StrictMode),
{PacketId, Rest1} = {PacketId, Rest1} =
case QoS of case QoS of
?QOS_0 -> {undefined, Rest}; ?QOS_0 -> {undefined, Rest};
@ -310,6 +310,7 @@ parse_packet(
(PacketId =/= undefined) andalso (PacketId =/= undefined) andalso
StrictMode andalso validate_packet_id(PacketId), StrictMode andalso validate_packet_id(PacketId),
{Properties, Payload} = parse_properties(Rest1, Ver, StrictMode), {Properties, Payload} = parse_properties(Rest1, Ver, StrictMode),
ok = ensure_topic_name_valid(StrictMode, TopicName, Properties),
Publish = #mqtt_packet_publish{ Publish = #mqtt_packet_publish{
topic_name = TopicName, topic_name = TopicName,
packet_id = PacketId, packet_id = PacketId,
@ -422,8 +423,9 @@ parse_will_message(
StrictMode StrictMode
) -> ) ->
{Props, Rest} = parse_properties(Bin, Ver, StrictMode), {Props, Rest} = parse_properties(Bin, Ver, StrictMode),
{Topic, Rest1} = parse_topic_name(Rest, StrictMode), {Topic, Rest1} = parse_utf8_string(Rest, StrictMode),
{Payload, Rest2} = parse_binary_data(Rest1), {Payload, Rest2} = parse_binary_data(Rest1),
ok = ensure_topic_name_valid(StrictMode, Topic, Props),
{ {
Packet#mqtt_packet_connect{ Packet#mqtt_packet_connect{
will_props = Props, will_props = Props,
@ -621,13 +623,14 @@ parse_binary_data(Bin) when
-> ->
?PARSE_ERR(malformed_binary_data_length). ?PARSE_ERR(malformed_binary_data_length).
parse_topic_name(Bin, false) -> ensure_topic_name_valid(false, _TopicName, _Properties) ->
parse_utf8_string(Bin, false); ok;
parse_topic_name(Bin, true) -> ensure_topic_name_valid(true, TopicName, _Properties) when TopicName =/= <<>> ->
case parse_utf8_string(Bin, true) of ok;
{<<>>, _Rest} -> ?PARSE_ERR(empty_topic_name); ensure_topic_name_valid(true, <<>>, #{'Topic-Alias' := _}) ->
Result -> Result ok;
end. ensure_topic_name_valid(true, <<>>, _) ->
error(empty_topic_name).
%%-------------------------------------------------------------------- %%--------------------------------------------------------------------
%% Serialize MQTT Packet %% Serialize MQTT Packet

View File

@ -158,12 +158,15 @@ t_parse_malformed_utf8_string(_) ->
?ASSERT_FRAME_THROW(utf8_string_invalid, emqx_frame:parse(MalformedPacket, ParseState)). ?ASSERT_FRAME_THROW(utf8_string_invalid, emqx_frame:parse(MalformedPacket, ParseState)).
t_parse_empty_topic_name(_) -> t_parse_empty_topic_name(_) ->
Packet = <<48, 4, 0, 0, 0, 1>>, Packet = ?PUBLISH_PACKET(?QOS_1, <<>>, 1, #{}, <<>>),
NormalState = emqx_frame:initial_parse_state(#{strict_mode => false}), ?assertEqual(Packet, parse_serialize(Packet, #{strict_mode => false})),
?assertMatch({_, _}, emqx_frame:parse(Packet, NormalState)), ?ASSERT_FRAME_THROW(empty_topic_name, parse_serialize(Packet, #{strict_mode => true})).
StrictState = emqx_frame:initial_parse_state(#{strict_mode => true}), t_parse_empty_topic_name_with_alias(_) ->
?ASSERT_FRAME_THROW(empty_topic_name, emqx_frame:parse(Packet, StrictState)). Props = #{'Topic-Alias' => 16#AB},
Packet = ?PUBLISH_PACKET(?QOS_1, <<>>, 1, Props, <<>>),
?assertEqual(Packet, parse_serialize(Packet, #{strict_mode => false})),
?assertEqual(Packet, parse_serialize(Packet, #{strict_mode => true})).
t_serialize_parse_v3_connect(_) -> t_serialize_parse_v3_connect(_) ->
Bin = Bin =