Skip to content

Commit f7c1489

Browse files
authored
Fix error handling in array and map length checks (#95)
1 parent ff16379 commit f7c1489

File tree

2 files changed

+7
-11
lines changed

2 files changed

+7
-11
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "libipld"
3-
version = "3.3.0"
3+
version = "3.3.1"
44
edition = "2021"
55
license = "MIT"
66
description = "Python binding to the Rust IPLD library"

src/lib.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,9 @@ where
201201
.into()
202202
}
203203
major::ARRAY => {
204-
let len: ffi::Py_ssize_t =
205-
types::Array::len(r)?.expect("contains length").try_into()?;
204+
let len: ffi::Py_ssize_t = types::Array::len(r)?
205+
.ok_or_else(|| anyhow!("Array must contain length"))?
206+
.try_into()?;
206207

207208
unsafe {
208209
let ptr = ffi::PyList_New(len);
@@ -220,7 +221,7 @@ where
220221
}
221222
}
222223
major::MAP => {
223-
let len = types::Map::len(r)?.expect("contains length");
224+
let len = types::Map::len(r)?.ok_or_else(|| anyhow!("Map must contain length"))?;
224225
let dict = PyDict::new(py);
225226

226227
let mut prev_key: Option<&[u8]> = None;
@@ -368,7 +369,7 @@ where
368369

369370
for (key, i) in keys {
370371
key.get(..)
371-
.expect("whole range is a valid string")
372+
.ok_or_else(|| anyhow!("Invalid string range while encoding map key"))?
372373
.encode(w)?;
373374
let value = unsafe { values.get_item_unchecked(i) };
374375
encode_dag_cbor_from_pyobject(_py, &value, w)?;
@@ -542,12 +543,7 @@ pub fn decode_dag_cbor(py: Python, data: &[u8]) -> PyResult<Py<PyAny>> {
542543
let py_object = decode_dag_cbor_to_pyobject(py, &mut reader, 0);
543544
if let Ok(py_object) = py_object {
544545
// check for any remaining data in the reader
545-
if reader
546-
.fill(1)
547-
.expect("SliceReader never fails")
548-
.as_ref()
549-
.is_empty()
550-
{
546+
if reader.fill(1)?.as_ref().is_empty() {
551547
Ok(py_object)
552548
} else {
553549
Err(get_err(

0 commit comments

Comments
 (0)