Skip to content

Commit

Permalink
fix: Fixed crash recovery issue (#914)
Browse files Browse the repository at this point in the history
* Sync position of writeRaf during recovery

* BinaryReader.readFrame return null when frameLength < 8
  • Loading branch information
maRci002 authored Feb 23, 2022
1 parent ae4e0a8 commit ea58f40
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 5 deletions.
1 change: 1 addition & 0 deletions hive/lib/src/backend/vm/storage_backend_vm.dart
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ class StorageBackendVm extends StorageBackend {
if (_crashRecovery) {
print('Recovering corrupted box.');
await writeRaf.truncate(recoveryOffset);
await writeRaf.setPosition(recoveryOffset);
writeOffset = recoveryOffset;
} else {
throw HiveError('Wrong checksum in hive file. Box may be corrupted.');
Expand Down
10 changes: 6 additions & 4 deletions hive/lib/src/binary/binary_reader_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -243,13 +243,14 @@ class BinaryReaderImpl extends BinaryReader {
/// Not part of public API
Frame? readFrame(
{HiveCipher? cipher, bool lazy = false, int frameOffset = 0}) {
// frame length is stored on 4 bytes
if (availableBytes < 4) return null;

// frame length should be at least 8 bytes
var frameLength = readUint32();
if (frameLength < 8) {
throw HiveError(
'This should not happen. Please open an issue on GitHub.');
}
if (frameLength < 8) return null;

// frame is bigger than avaible bytes
if (availableBytes < frameLength - 4) return null;

var crc = _buffer.readUint32(_offset + frameLength - 8);
Expand All @@ -260,6 +261,7 @@ class BinaryReaderImpl extends BinaryReader {
crc: cipher?.calculateKeyCrc() ?? 0,
);

// frame is corrupted or provided chiper is different
if (computedCrc != crc) return null;

_limitAvailableBytes(frameLength - 8);
Expand Down
3 changes: 3 additions & 0 deletions hive/test/tests/backend/vm/storage_backend_vm_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,13 @@ void main() {
when(() => lockRaf.lock()).thenAnswer((i) => Future.value(lockRaf));
when(() => writeRaf.truncate(20))
.thenAnswer((i) => Future.value(writeRaf));
when(() => writeRaf.setPosition(20))
.thenAnswer((i) => Future.value(writeRaf));

await backend.initialize(
TypeRegistryImpl.nullImpl, MockKeystore(), lazy);
verify(() => writeRaf.truncate(20));
verify(() => writeRaf.setPosition(20));
});

test('recoveryOffset without crash recovery', () async {
Expand Down
35 changes: 34 additions & 1 deletion hive/test/tests/binary/binary_reader_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,39 @@ void main() {
});

group('.readFrame()', () {
final List<Uint8List> nullFramesBytes = [
// availableBytes < 4
// there is ONLY 3 bytes provided
Uint8List.fromList([8, 0, 0]),
// frameLength < 8
// frame is 7 length
Uint8List.fromList([7, 0, 0, 0, 0, 0, 0, 0, 0, 0]),
// availableBytes < frameLength - 4
// frame is 10 length however ONLY 9 bytes provided
Uint8List.fromList([10, 0, 0, 0, 0, 0, 0, 0, 0]),
// computedCrc != crc
// 0, 0, 0, 0 crc is: 0 and computedCrc is: 274301637
Uint8List.fromList([10, 0, 0, 0, 0, 0, 0, 0, 0, 0]),
];

test('null', () {
for (final bytes in nullFramesBytes) {
final reader = BinaryReaderImpl(bytes, testRegistry);
final frame = reader.readFrame(lazy: false);

expect(frame, null);
}
});

test('null lazy', () {
for (final bytes in nullFramesBytes) {
final reader = BinaryReaderImpl(bytes, testRegistry);
final frame = reader.readFrame(lazy: true);

expect(frame, null);
}
});

test('normal', () {
var frames = framesSetLengthOffset(testFrames, frameBytes);
var offset = 0;
Expand Down Expand Up @@ -397,7 +430,7 @@ void main() {
}
});

test('lazy', () {
test('encrypted lazy', () {
var frames = framesSetLengthOffset(testFrames, frameBytesEncrypted);
var offset = 0;
for (var i = 0; i < frames.length; i++) {
Expand Down

0 comments on commit ea58f40

Please sign in to comment.