From c6c90e991b776a7d2a4231e14220b91c804d650f Mon Sep 17 00:00:00 2001 From: Terra854 Date: Sat, 21 Nov 2026 07:16:40 +0800 Subject: [PATCH] sqlite: share B-tree local payload sizing (#2696) --- crates/sqlite/src/lib.rs | 40 ++++++++++++++++++++++++++++ crates/sqlite/tests/wire.rs | 52 ++++++++++++++++++++++++++++++++++++- 2 files changed, 91 insertions(+), 1 deletion(-) diff --git a/crates/sqlite/src/lib.rs b/crates/sqlite/src/lib.rs index a86c4bcbb..d397f62b3 100644 --- a/crates/sqlite/src/lib.rs +++ b/crates/sqlite/src/lib.rs @@ -10,6 +10,46 @@ use core::fmt; const MAX_VARINT_BYTES: usize = 9; +/// The b-tree cell layouts that carry payload bytes. +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub enum PayloadKind { + /// A table leaf cell; table interior cells have no payload. + TableLeaf, + /// An index leaf or interior cell, which share the same payload thresholds. + Index, +} + +/// Return the number of payload bytes stored in the b-tree page before overflow. +/// +/// `usable` is the page size minus its reserved bytes, not the space remaining after the +/// cell header. It may be odd. Sizes outside 480..=65,536 return `None`; validating the +/// database header and bounding payload reads remain the caller's responsibility. +/// +/// This implements the [SQLite b-tree cell payload formula](https://www.sqlite.org/fileformat.html#b_tree_pages). +/// The calculation allocates nothing and accepts the full `u64` payload length without +/// implying that the payload fits in the database or in memory. +pub fn local_payload_len(usable: u32, payload_len: u64, kind: PayloadKind) -> Option { + // SQLite requires at least 480 usable bytes; this also keeps every divisor and + // subtraction below valid even when the caller supplies an untrusted header. + if !(480..=65_536).contains(&usable) { + return None; + } + let usable = u64::from(usable); + let maximum = match kind { + PayloadKind::TableLeaf => usable - 35, + PayloadKind::Index => (usable - 12) * 64 / 255 - 23, + }; + let local = if payload_len <= maximum { + payload_len + } else { + let minimum = (usable - 12) * 32 / 255 - 23; + let candidate = minimum + (payload_len - minimum) % (usable - 4); + if candidate <= maximum { candidate } else { minimum } + }; + // Both branches bound the result by the supported page's maximum local payload. + Some(local as u32) +} + /// A failure to decode part of a SQLite record or page. #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub enum Error { diff --git a/crates/sqlite/tests/wire.rs b/crates/sqlite/tests/wire.rs index 9ef7bb278..c28e85a13 100644 --- a/crates/sqlite/tests/wire.rs +++ b/crates/sqlite/tests/wire.rs @@ -1,4 +1,54 @@ -use craft_sqlite::{Error, read_varint, serial_type_len}; +use craft_sqlite::{Error, PayloadKind, local_payload_len, read_varint, serial_type_len}; + +#[test] +fn local_payload_preserves_inline_and_overflow_boundaries() { + // Original literal cases calculated from SQLite file format section 1.6, including + // the 512-byte page with 32 reserved bytes and an odd usable size with one reserved byte. + for (usable, minimum, table_max, index_max) in [ + (480, 35, 445, 94), + (511, 39, 476, 102), + (512, 39, 477, 102), + (1_024, 103, 989, 230), + (2_048, 232, 2_013, 487), + (4_096, 489, 4_061, 1_002), + (8_192, 1_003, 8_157, 2_030), + (16_384, 2_031, 16_349, 4_086), + (32_768, 4_087, 32_733, 8_198), + (65_536, 8_199, 65_501, 16_422), + ] { + for (kind, maximum) in [(PayloadKind::TableLeaf, table_max), (PayloadKind::Index, index_max)] { + for (payload, expected) in [(0, 0), (maximum, maximum), (maximum + 1, minimum)] { + assert_eq!(local_payload_len(usable, u64::from(payload), kind), Some(expected), "usable {usable}, payload {payload}, {kind:?}"); + } + // A complete overflow-page span returns to the upper local boundary; one + // further byte must fall back to the minimum rather than exceed the maximum. + let payload = u64::from(maximum) + u64::from(usable) - 4; + assert_eq!(local_payload_len(usable, payload, kind), Some(maximum), "usable {usable}, {kind:?}"); + assert_eq!(local_payload_len(usable, payload + 1, kind), Some(minimum), "usable {usable}, {kind:?}"); + } + } + assert_eq!(local_payload_len(512, 600, PayloadKind::TableLeaf), Some(92)); +} + +#[test] +fn local_payload_refuses_unsupported_usable_sizes() { + for usable in [0, 4, 35, 479, 65_537, u32::MAX] { + for kind in [PayloadKind::TableLeaf, PayloadKind::Index] { + assert_eq!(local_payload_len(usable, 0, kind), None); + assert_eq!(local_payload_len(usable, u64::MAX, kind), None); + } + } +} + +#[test] +fn local_payload_keeps_full_width_payload_lengths() { + // Literal remainders for the largest wire length catch host-size truncation and + // overflow in the minimum-plus-modulo calculation without allocating a payload. + for (usable, table_local, index_local) in [(480, 323, 35), (511, 93, 93), (512, 255, 39), (32_768, 4_095, 4_095), (65_536, 8_199, 8_199)] { + assert_eq!(local_payload_len(usable, u64::MAX, PayloadKind::TableLeaf), Some(table_local)); + assert_eq!(local_payload_len(usable, u64::MAX, PayloadKind::Index), Some(index_local)); + } +} #[test] fn varints_decode_at_each_width() {