Skip to content

Commit d455777

Browse files
authored
Return Serde errors for corrupt RLE and RunEnd metadata (#10852)
## Summary - Tracking Issue: #10756 Corrupt or crafted metadata can make a file read panic instead of returning a Serde error. #10760 covers the ALP-RD side of that issue. This PR covers the RLE and RunEnd paths called out there: - RLE `deserialize` called `dtype.as_ptype()`, which panics when the file dtype is not primitive. It now uses `PType::try_from(dtype)?`. - RunEnd `deserialize` used `vortex_expect` on `usize::try_from` for `num_runs` and `offset`, which panics on 32-bit targets when the stored value exceeds `usize::MAX`. Both conversions now return a Serde error. Partially addresses #10756. Signed-off-by: Vincent <[email protected]>
1 parent cb613ac commit d455777

4 files changed

Lines changed: 106 additions & 3 deletions

File tree

‎encodings/fastlanes/src/rle/vtable/mod.rs‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,4 +339,35 @@ mod tests {
339339

340340
Ok(())
341341
}
342+
343+
#[test]
344+
fn test_deserialize_rejects_non_primitive_dtype() -> VortexResult<()> {
345+
let metadata = RLEMetadata {
346+
values_len: 0,
347+
indices_len: 0,
348+
indices_ptype: PType::U16 as i32,
349+
values_idx_offsets_len: 0,
350+
values_idx_offsets_ptype: PType::U64 as i32,
351+
offset: 0,
352+
}
353+
.encode_to_vec();
354+
let children: Vec<ArrayRef> = Vec::new();
355+
let result = ArrayPlugin::deserialize(
356+
&RLE,
357+
ArrayDeserialization::new(
358+
VTable::id(&RLE),
359+
&DType::Utf8(Nullability::NonNullable),
360+
0,
361+
&metadata,
362+
&[],
363+
&children,
364+
),
365+
&SESSION,
366+
);
367+
assert!(result.is_err_and(|err| {
368+
err.to_string()
369+
.contains("Cannot convert DType utf8 into PType")
370+
}));
371+
Ok(())
372+
}
342373
}

‎encodings/fastlanes/src/rle/vtable/plugin.rs‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,10 @@ impl ArrayPlugin for RLE {
8686
"RLEArray expects 0 buffers, got {}",
8787
buffers.len()
8888
);
89+
let values_ptype = PType::try_from(dtype)?;
8990
let values = children.get(
9091
0,
91-
&DType::Primitive(dtype.as_ptype(), Nullability::NonNullable),
92+
&DType::Primitive(values_ptype, Nullability::NonNullable),
9293
usize::try_from(metadata.values_len)?,
9394
)?;
9495

‎vortex-array/src/arrays/runend/array/mod.rs‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -927,4 +927,64 @@ mod tests {
927927

928928
Ok(())
929929
}
930+
931+
// On 32-bit targets, a crafted num_runs / offset above usize::MAX used to panic via
932+
// vortex_expect. Keep a regression that only builds where the conversion can fail.
933+
#[cfg(target_pointer_width = "32")]
934+
#[test]
935+
fn test_deserialize_rejects_num_runs_overflow() -> VortexResult<()> {
936+
let metadata = RunEndMetadata {
937+
ends_ptype: PType::U32 as i32,
938+
num_runs: u64::from(u32::MAX) + 1,
939+
offset: 0,
940+
}
941+
.encode_to_vec();
942+
let children: Vec<ArrayRef> = Vec::new();
943+
let result = ArrayPlugin::deserialize(
944+
&RunEnd,
945+
ArrayDeserialization::new(
946+
VTable::id(&RunEnd),
947+
&DType::Primitive(PType::I32, Nullability::NonNullable),
948+
0,
949+
&metadata,
950+
&[],
951+
&children,
952+
),
953+
&SESSION,
954+
);
955+
assert!(result.is_err_and(|err| {
956+
err.to_string().contains("RunEndMetadata num_runs")
957+
&& err.to_string().contains("exceeds usize range")
958+
}));
959+
Ok(())
960+
}
961+
962+
#[cfg(target_pointer_width = "32")]
963+
#[test]
964+
fn test_deserialize_rejects_offset_overflow() -> VortexResult<()> {
965+
let metadata = RunEndMetadata {
966+
ends_ptype: PType::U32 as i32,
967+
num_runs: 0,
968+
offset: u64::from(u32::MAX) + 1,
969+
}
970+
.encode_to_vec();
971+
let children: Vec<ArrayRef> = Vec::new();
972+
let result = ArrayPlugin::deserialize(
973+
&RunEnd,
974+
ArrayDeserialization::new(
975+
VTable::id(&RunEnd),
976+
&DType::Primitive(PType::I32, Nullability::NonNullable),
977+
0,
978+
&metadata,
979+
&[],
980+
&children,
981+
),
982+
&SESSION,
983+
);
984+
assert!(result.is_err_and(|err| {
985+
err.to_string().contains("RunEndMetadata offset")
986+
&& err.to_string().contains("exceeds usize range")
987+
}));
988+
Ok(())
989+
}
930990
}

‎vortex-array/src/arrays/runend/array/plugin.rs‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use prost::Message;
77
use vortex_error::VortexExpect;
88
use vortex_error::VortexResult;
99
use vortex_error::vortex_ensure_eq;
10+
use vortex_error::vortex_err;
1011
use vortex_session::VortexSession;
1112

1213
use super::RunEnd;
@@ -79,11 +80,21 @@ impl ArrayPlugin for RunEnd {
7980
let metadata = RunEndMetadata::decode(metadata)?;
8081
let ends_ptype = metadata.get_ends_ptype()?;
8182
let ends_dtype = DType::Primitive(ends_ptype, Nullability::NonNullable);
82-
let runs = usize::try_from(metadata.num_runs).vortex_expect("Must be a valid usize");
83+
let runs = usize::try_from(metadata.num_runs).map_err(|_| {
84+
vortex_err!(
85+
"RunEndMetadata num_runs {} exceeds usize range",
86+
metadata.num_runs
87+
)
88+
})?;
8389
let ends = children.get(0, &ends_dtype, runs)?;
8490

8591
let values = children.get(1, dtype, runs)?;
86-
let offset = usize::try_from(metadata.offset).vortex_expect("Offset must be a valid usize");
92+
let offset = usize::try_from(metadata.offset).map_err(|_| {
93+
vortex_err!(
94+
"RunEndMetadata offset {} exceeds usize range",
95+
metadata.offset
96+
)
97+
})?;
8798
RunEndData::validate_parts(
8899
&ends,
89100
&values,

0 commit comments

Comments
 (0)