Skip to content

Commit 603d208

Browse files
authored
Merge pull request #74 from dancergraham/fix/panic-to-runtime-error
🤖 fix: raise RuntimeError instead of panicking on corrupt point data
2 parents 7923cdf + 28aebb3 commit 603d208

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

‎src/lib.rs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::io::BufReader;
44
use ::e57::{CartesianCoordinate, E57Reader};
55
use numpy::ndarray::Array2;
66
use numpy::{IntoPyArray, PyArray2};
7+
use pyo3::exceptions::PyRuntimeError;
78
use pyo3::prelude::*;
89

910
// Keep these docs in sync with e57.pyi.
@@ -76,15 +77,15 @@ unsafe fn read_points(py: Python<'_>, filepath: &str) -> PyResult<E57> {
7677
for pointcloud in pointclouds {
7778
let mut iter = file
7879
.pointcloud_simple(&pointcloud)
79-
.expect("this file should contain a pointcloud");
80+
.map_err(|e| PyRuntimeError::new_err(e.to_string()))?;
8081
iter.spherical_to_cartesian(true);
8182
iter.cartesian_to_spherical(false);
8283
iter.intensity_to_color(true);
8384
iter.normalize_intensity(false);
8485
iter.apply_pose(true);
8586

8687
for p in iter {
87-
let p = p.expect("Unable to read next point");
88+
let p = p.map_err(|e| PyRuntimeError::new_err(e.to_string()))?;
8889
// Invalid points are skipped entirely, so colour and intensity
8990
// stay row-aligned with the points that are kept.
9091
let CartesianCoordinate::Valid { x, y, z } = p.cartesian else {

‎tests/test_e57.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import pathlib
2+
13
import numpy as np
24
import pytest
35

@@ -202,3 +204,15 @@ def test_invalid_points_rows_aligned():
202204
np.testing.assert_allclose(pointcloud.points, xyz, atol=1e-9)
203205
np.testing.assert_allclose(pointcloud.color, rgb, atol=1e-6)
204206
np.testing.assert_allclose(pointcloud.intensity[:, 0], intensity, atol=1e-6)
207+
208+
209+
def test_corrupt_point_data(tmp_path):
210+
"""Valid header and XML but garbage point data raises RuntimeError, not a panic."""
211+
good = pathlib.Path("testdata/bunnyFloat.e57").read_bytes()
212+
xml_offset = int.from_bytes(good[24:32], "little") # header: XML physical offset
213+
bad = bytearray(good)
214+
bad[2000 : xml_offset - 2000] = b"\xff" * (xml_offset - 4000)
215+
path = tmp_path / "corrupt.e57"
216+
path.write_bytes(bad)
217+
with pytest.raises(RuntimeError, match="Failed to read data packet"):
218+
e57.read_points(str(path))

0 commit comments

Comments
 (0)