Permalink
Browse files

Merge remote-tracking branch 'hdima/read_check_crc_first'

  • Loading branch information...
2 parents d8958d9 + 4089b7f commit 9afbf81bcf73ce184ac9346582af9c78e458409d @jonmeredith jonmeredith committed Feb 6, 2012
Showing with 23 additions and 4 deletions.
  1. +19 −0 src/bitcask.erl
  2. +4 −4 src/bitcask_fileops.erl
View
@@ -1472,6 +1472,25 @@ corrupt_file_test() ->
ok.
+invalid_data_size_test() ->
+ TestDir = "/tmp/bc.test.invalid_data_size_test",
+ TestDataFile = TestDir ++ "/1.bitcask.data",
+
+ os:cmd("rm -rf " ++ TestDir),
+ B = bitcask:open(TestDir, [read_write]),
+ ok = bitcask:put(B,<<"k">>,<<"v">>),
+ close(B),
+
+ % Alter data size
+ {ok, F} = file:open(TestDataFile, [read, write, raw, binary]),
+ {ok, _} = file:position(F, {eof, -6}),
+ ok = file:write(F, <<255:?VALSIZEFIELD>>),
+ file:close(F),
+ B2 = bitcask:open(TestDir),
+ ?assertEqual({error, bad_crc}, bitcask:get(B2, <<"k">>)),
+ close(B2),
+ ok.
+
testhelper_keydir_count(B) ->
KD = (get_state(B))#bc_state.keydir,
{KeyCount,_,_} = bitcask_nifs:keydir_info(KD),
View
@@ -168,13 +168,13 @@ read(Filename, Offset, Size) when is_list(Filename) ->
read(#filestate { fd = FD }, Offset, Size) ->
case bitcask_nifs:file_pread(FD, Offset, Size) of
{ok, <<Crc32:?CRCSIZEFIELD/unsigned, Bytes/binary>>} ->
- %% Unpack the actual data
- <<_Tstamp:?TSTAMPFIELD, KeySz:?KEYSIZEFIELD, ValueSz:?VALSIZEFIELD,
- Key:KeySz/bytes, Value:ValueSz/bytes>> = Bytes,
-
%% Verify the CRC of the data
case erlang:crc32(Bytes) of
Crc32 ->
+ %% Unpack the actual data
+ <<_Tstamp:?TSTAMPFIELD,
+ KeySz:?KEYSIZEFIELD, ValueSz:?VALSIZEFIELD,
+ Key:KeySz/bytes, Value:ValueSz/bytes>> = Bytes,
{ok, Key, Value};
_BadCrc ->
{error, bad_crc}

0 comments on commit 9afbf81

Please sign in to comment.