Skip to content

Commit 02f04f6

Browse files
committed
fix: use usize::try_from for footer_size casts to prevent silent truncation
Replace bare `footer_size as usize` casts with `usize::try_from(footer_size)` to prevent silent truncation on 32-bit platforms where a positive i64 value exceeding u32::MAX would wrap. Apply consistently across table.rs and table_changes.rs to match the pattern already used in table_deletions.rs.
1 parent 2d391e9 commit 02f04f6

2 files changed

Lines changed: 12 additions & 6 deletions

File tree

src/table.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,9 @@ impl DuckLakeTable {
301301
);
302302
if let Some(footer_size) = delete_file.footer_size
303303
&& footer_size > 0
304+
&& let Ok(hint) = usize::try_from(footer_size)
304305
{
305-
pf = pf.with_metadata_size_hint(footer_size as usize);
306+
pf = pf.with_metadata_size_hint(hint);
306307
}
307308

308309
// Create file scan config for the delete file
@@ -372,8 +373,9 @@ impl DuckLakeTable {
372373
// This reduces I/O from 2 reads to 1 read per file (especially beneficial for S3/MinIO)
373374
if let Some(footer_size) = table_file.file.footer_size
374375
&& footer_size > 0
376+
&& let Ok(hint) = usize::try_from(footer_size)
375377
{
376-
pf = pf.with_metadata_size_hint(footer_size as usize);
378+
pf = pf.with_metadata_size_hint(hint);
377379
}
378380

379381
Ok(pf)
@@ -452,8 +454,9 @@ impl DuckLakeTable {
452454
);
453455
if let Some(footer_size) = table_file.file.footer_size
454456
&& footer_size > 0
457+
&& let Ok(hint) = usize::try_from(footer_size)
455458
{
456-
pf = pf.with_metadata_size_hint(footer_size as usize);
459+
pf = pf.with_metadata_size_hint(hint);
457460
}
458461

459462
// Use read_schema (with original Parquet names) for reading

src/table_changes.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,8 @@ impl TableChangesTable {
440440
&self.table_path,
441441
&data_file.path,
442442
data_file.path_is_relative,
443-
);
443+
)
444+
.map_err(|e| DataFusionError::External(Box::new(e)))?;
444445

445446
// Create PartitionedFile with footer size hint if available
446447
let mut pf = PartitionedFile::new(
@@ -449,8 +450,9 @@ impl TableChangesTable {
449450
);
450451
if let Some(footer_size) = data_file.footer_size
451452
&& footer_size > 0
453+
&& let Ok(hint) = usize::try_from(footer_size)
452454
{
453-
pf = pf.with_metadata_size_hint(footer_size as usize);
455+
pf = pf.with_metadata_size_hint(hint);
454456
}
455457

456458
// Determine what to read from Parquet
@@ -562,7 +564,8 @@ impl TableProvider for TableChangesTable {
562564
&self.table_path,
563565
&data_file.path,
564566
data_file.path_is_relative,
565-
);
567+
)
568+
.map_err(|e| DataFusionError::External(Box::new(e)))?;
566569
builder.add_file(&resolved_path, data_file.encryption_key.as_deref());
567570
}
568571
let factory = builder.build();

0 commit comments

Comments
 (0)