Skip to content

Commit 9e092c1

Browse files
committed
core/storage: Improve error handling in sqlite3_ondisk.rs
1 parent 476ae1f commit 9e092c1

File tree

2 files changed

+28
-19
lines changed

2 files changed

+28
-19
lines changed

core/storage/btree.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3128,7 +3128,7 @@ impl BTreeCursor {
31283128
max_local,
31293129
min_local,
31303130
page_type,
3131-
);
3131+
)?;
31323132
let buf = old_page_contents.as_ptr();
31333133
let cell_buf = &mut buf[cell_start..cell_start + cell_len];
31343134
// TODO(pere): make this reference and not copy

core/storage/sqlite3_ondisk.rs

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,9 @@ impl PageContent {
468468
}
469469

470470
pub fn page_type(&self) -> PageType {
471-
self.read_u8(BTREE_PAGE_TYPE).try_into().unwrap()
471+
self.read_u8(BTREE_PAGE_TYPE)
472+
.try_into()
473+
.expect("Invalid page type")
472474
}
473475

474476
pub fn maybe_page_type(&self) -> Option<PageType> {
@@ -804,6 +806,7 @@ impl PageContent {
804806
min_local,
805807
page_type,
806808
)
809+
.expect("Failed to get cell raw region")
807810
}
808811

809812
/// Get region(start end length) of a cell's payload
@@ -815,13 +818,13 @@ impl PageContent {
815818
max_local: usize,
816819
min_local: usize,
817820
page_type: PageType,
818-
) -> (usize, usize) {
821+
) -> Result<(usize, usize)> {
819822
let buf = self.as_ptr();
820823
assert!(idx < cell_count, "cell_get: idx out of bounds");
821824
let start = self.cell_get_raw_start_offset(idx);
822825
let len = match page_type {
823826
PageType::IndexInterior => {
824-
let (len_payload, n_payload) = read_varint(&buf[start + 4..]).unwrap();
827+
let (len_payload, n_payload) = read_varint(&buf[start + 4..])?;
825828
let (overflows, to_read) =
826829
payload_overflows(len_payload as usize, max_local, min_local, usable_size);
827830
if overflows {
@@ -831,11 +834,11 @@ impl PageContent {
831834
}
832835
}
833836
PageType::TableInterior => {
834-
let (_, n_rowid) = read_varint(&buf[start + 4..]).unwrap();
837+
let (_, n_rowid) = read_varint(&buf[start + 4..])?;
835838
4 + n_rowid
836839
}
837840
PageType::IndexLeaf => {
838-
let (len_payload, n_payload) = read_varint(&buf[start..]).unwrap();
841+
let (len_payload, n_payload) = read_varint(&buf[start..])?;
839842
let (overflows, to_read) =
840843
payload_overflows(len_payload as usize, max_local, min_local, usable_size);
841844
if overflows {
@@ -849,8 +852,8 @@ impl PageContent {
849852
}
850853
}
851854
PageType::TableLeaf => {
852-
let (len_payload, n_payload) = read_varint(&buf[start..]).unwrap();
853-
let (_, n_rowid) = read_varint(&buf[start + n_payload..]).unwrap();
855+
let (len_payload, n_payload) = read_varint(&buf[start..])?;
856+
let (_, n_rowid) = read_varint(&buf[start + n_payload..])?;
854857
let (overflows, to_read) =
855858
payload_overflows(len_payload as usize, max_local, min_local, usable_size);
856859
if overflows {
@@ -864,7 +867,7 @@ impl PageContent {
864867
}
865868
}
866869
};
867-
(start, len)
870+
Ok((start, len))
868871
}
869872

870873
pub fn is_leaf(&self) -> bool {
@@ -1269,7 +1272,10 @@ impl<T: Default + Copy, const N: usize> SmallVec<T, N> {
12691272
if self.extra_data.is_none() {
12701273
self.extra_data = Some(Vec::new());
12711274
}
1272-
self.extra_data.as_mut().unwrap().push(value);
1275+
self.extra_data
1276+
.as_mut()
1277+
.expect("extra_data was just initialized")
1278+
.push(value);
12731279
self.len += 1;
12741280
}
12751281
}
@@ -1278,7 +1284,10 @@ impl<T: Default + Copy, const N: usize> SmallVec<T, N> {
12781284
assert!(self.extra_data.is_some());
12791285
assert!(index >= self.data.len());
12801286
let extra_data_index = index - self.data.len();
1281-
let extra_data = self.extra_data.as_ref().unwrap();
1287+
let extra_data = self
1288+
.extra_data
1289+
.as_ref()
1290+
.expect("extra_data existence was just asserted");
12821291
assert!(extra_data_index < extra_data.len());
12831292
extra_data[extra_data_index]
12841293
}
@@ -1806,14 +1815,14 @@ impl StreamingWalReader {
18061815
let (page_sz, c1, c2, use_native, ok) = {
18071816
let mut h = self.header.lock();
18081817
let s = buf.as_slice();
1809-
h.magic = u32::from_be_bytes(s[0..4].try_into().unwrap());
1810-
h.file_format = u32::from_be_bytes(s[4..8].try_into().unwrap());
1811-
h.page_size = u32::from_be_bytes(s[8..12].try_into().unwrap());
1812-
h.checkpoint_seq = u32::from_be_bytes(s[12..16].try_into().unwrap());
1813-
h.salt_1 = u32::from_be_bytes(s[16..20].try_into().unwrap());
1814-
h.salt_2 = u32::from_be_bytes(s[20..24].try_into().unwrap());
1815-
h.checksum_1 = u32::from_be_bytes(s[24..28].try_into().unwrap());
1816-
h.checksum_2 = u32::from_be_bytes(s[28..32].try_into().unwrap());
1818+
h.magic = u32::from_be_bytes([s[0], s[1], s[2], s[3]]);
1819+
h.file_format = u32::from_be_bytes([s[4], s[5], s[6], s[7]]);
1820+
h.page_size = u32::from_be_bytes([s[8], s[9], s[10], s[11]]);
1821+
h.checkpoint_seq = u32::from_be_bytes([s[12], s[13], s[14], s[15]]);
1822+
h.salt_1 = u32::from_be_bytes([s[16], s[17], s[18], s[19]]);
1823+
h.salt_2 = u32::from_be_bytes([s[20], s[21], s[22], s[23]]);
1824+
h.checksum_1 = u32::from_be_bytes([s[24], s[25], s[26], s[27]]);
1825+
h.checksum_2 = u32::from_be_bytes([s[28], s[29], s[30], s[31]]);
18171826
tracing::debug!("WAL header: {:?}", *h);
18181827

18191828
let use_native = cfg!(target_endian = "big") == ((h.magic & 1) != 0);

0 commit comments

Comments
 (0)