Skip to content

Commit 1e2f998

Browse files
committed
Do not panic when opening queue file with corrupted header.
1 parent a36e49b commit 1e2f998

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

src/lib.rs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -292,10 +292,18 @@ impl QueueFile {
292292
first_pos = buf.get_u64();
293293
last_pos = buf.get_u64();
294294

295-
assert!(i64::try_from(file_len).is_ok());
296-
assert!(i32::try_from(elem_cnt).is_ok());
297-
assert!(i64::try_from(first_pos).is_ok());
298-
assert!(i64::try_from(last_pos).is_ok());
295+
ensure!(i64::try_from(file_len).is_ok(), CorruptedFileSnafu {
296+
msg: "file length in header is greater than i64::MAX"
297+
});
298+
ensure!(i32::try_from(elem_cnt).is_ok(), CorruptedFileSnafu {
299+
msg: "element count in header is greater than i32::MAX"
300+
});
301+
ensure!(i64::try_from(first_pos).is_ok(), CorruptedFileSnafu {
302+
msg: "first element position in header is greater than i64::MAX"
303+
});
304+
ensure!(i64::try_from(last_pos).is_ok(), CorruptedFileSnafu {
305+
msg: "last element position in header is greater than i64::MAX"
306+
});
299307
} else {
300308
header_len = 16;
301309

@@ -304,10 +312,18 @@ impl QueueFile {
304312
first_pos = u64::from(buf.get_u32());
305313
last_pos = u64::from(buf.get_u32());
306314

307-
assert!(i32::try_from(file_len).is_ok());
308-
assert!(i32::try_from(elem_cnt).is_ok());
309-
assert!(i32::try_from(first_pos).is_ok());
310-
assert!(i32::try_from(last_pos).is_ok());
315+
ensure!(i32::try_from(file_len).is_ok(), CorruptedFileSnafu {
316+
msg: "file length in header is greater than i32::MAX"
317+
});
318+
ensure!(i32::try_from(elem_cnt).is_ok(), CorruptedFileSnafu {
319+
msg: "element count in header is greater than i32::MAX"
320+
});
321+
ensure!(i32::try_from(first_pos).is_ok(), CorruptedFileSnafu {
322+
msg: "first element position in header is greater than i32::MAX"
323+
});
324+
ensure!(i32::try_from(last_pos).is_ok(), CorruptedFileSnafu {
325+
msg: "last element position in header is greater than i32::MAX"
326+
});
311327
}
312328

313329
let real_file_len = file.metadata()?.len();

0 commit comments

Comments
 (0)