Skip to content

Commit 124f5da

Browse files
lhoguinmergify[bot]
authored andcommitted
CQ shared: Fix off-by-nine error leading to lost messages
And `eof` crashes. The problem is that we may end up trying to read more data from the file when scanning, despite being at the end of the file. This results in the current Acc to be returned instead of the remaining data being parsed. This results in some messages at the end of the file being truncated off despite still being in memory (and still pointing to the end of the original file, well past the truncation point). (cherry picked from commit f363854)
1 parent 9fd8231 commit 124f5da

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

deps/rabbit/src/rabbit_msg_store.erl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1599,7 +1599,7 @@ scan_data(<<Size:64, MsgIdInt:128, _Rest/bits>> = Data, Fd, Fun, Offset, FileSiz
15991599
end;
16001600
%% This might be the start of a message.
16011601
scan_data(<<Size:64, Rest/bits>> = Data, Fd, Fun, Offset, FileSize, MsgIdsFound, Acc)
1602-
when byte_size(Rest) < Size + 1, Size < FileSize - Offset ->
1602+
when byte_size(Rest) < Size + 1, Size + 9 =< FileSize - Offset ->
16031603
scan(Data, Fd, Fun, Offset, FileSize, MsgIdsFound, Acc);
16041604
scan_data(Data, Fd, Fun, Offset, FileSize, MsgIdsFound, Acc)
16051605
when byte_size(Data) < 8 ->

deps/rabbit/test/backing_queue_SUITE.erl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,17 @@ msg_store_file_scan1(Config) ->
710710
[{bin, <<0, 0:48, 17, 17, "idididididididid", 255, 0:4352/unit:8, 255>>}],
711711
{ok, [{<<"idididididididid">>, 4378, 1}]},
712712
fun(Obj = {<<"idididididididid">>, 4378, 1}) -> {valid, Obj}; (_) -> invalid end),
713+
%% Off-by-nine regression testing. The file scanning could miss
714+
%% some messages if previous data looked like a message but its
715+
%% size went past the end of the file.
716+
lists:foreach(fun(N) ->
717+
ok = Scan([
718+
{bin, <<(4194304 + N):64, 0:(4194304 - 8 - 25 - 10)/unit:8>>},
719+
{msg, gen_id(), <<>>},
720+
%% Padding ensures there's no 255 at the end of the size indicated by 'bin'.
721+
{pad, 10}
722+
])
723+
end, lists:seq(-9, -1)),
713724
%% All good!!
714725
passed.
715726

0 commit comments

Comments
 (0)