Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions pytests/test_dag_cbor.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,15 @@ def test_recursion_limit_exceed_on_nested_maps() -> None:
assert 'in DAG-CBOR decoding' in str(exc_info.value)



def test_dag_cbor_decode_invalid_utf8() -> None:
with pytest.raises(ValueError) as exc_info:
libipld.decode_dag_cbor(bytes.fromhex('62c328'))


assert 'Invalid UTF-8 string' in str(exc_info.value)


def test_dab_cbor_decode_map_int_key() -> None:
dag_cbor = bytes.fromhex('a10000')
with pytest.raises(ValueError) as exc_info:
Expand All @@ -151,3 +160,4 @@ def test_dab_cbor_encode_map_int_key() -> None:
libipld.encode_dag_cbor(obj)

assert 'Map keys must be strings' in str(exc_info.value)

10 changes: 6 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,13 @@ fn get_bytes_from_py_any<'py>(obj: &'py Bound<'py, PyAny>) -> PyResult<&'py [u8]
}
}

fn string_new_bound<'py>(py: Python<'py>, s: &[u8]) -> Bound<'py, PyString> {
fn string_new_bound<'py>(py: Python<'py>, s: &[u8]) -> Result<Bound<'py, PyString>> {
std::str::from_utf8(s).map_err(|e| anyhow!("Invalid UTF-8 string: {}", e))?;

let ptr = s.as_ptr() as *const c_char;
let len = s.len() as ffi::Py_ssize_t;
unsafe {
Bound::from_owned_ptr(py, ffi::PyUnicode_FromStringAndSize(ptr, len)).downcast_into_unchecked()
Ok(Bound::from_owned_ptr(py, ffi::PyUnicode_FromStringAndSize(ptr, len)).downcast_into_unchecked())
}
}

Expand Down Expand Up @@ -135,7 +137,7 @@ fn decode_dag_cbor_to_pyobject<R: Read + Seek>(
}
MajorKind::TextString => {
let len = decode::read_uint(r, major)?;
string_new_bound(py, &decode::read_bytes(r, len)?).into_pyobject(py)?.into()
string_new_bound(py, &decode::read_bytes(r, len)?)?.into_pyobject(py)?.into()
}
MajorKind::Array => {
let len: ffi::Py_ssize_t = decode_len(decode::read_uint(r, major)?)?.try_into()?;
Expand Down Expand Up @@ -173,7 +175,7 @@ fn decode_dag_cbor_to_pyobject<R: Read + Seek>(
}
}

let key_py = string_new_bound(py, key.as_slice()).into_pyobject(py)?;
let key_py = string_new_bound(py, key.as_slice())?.into_pyobject(py)?;
prev_key = Some(key);

let value_py = decode_dag_cbor_to_pyobject(py, r, depth + 1)?;
Expand Down