diff --git a/src/serialize/writer.rs b/src/serialize/writer.rs index 85e07e00..4edbc446 100644 --- a/src/serialize/writer.rs +++ b/src/serialize/writer.rs @@ -27,6 +27,9 @@ impl BytesWriter { pub fn finish(&mut self) -> NonNull { unsafe { + unsafe { + std::ptr::write(self.buffer_ptr(), 0); + }; (*self.bytes.cast::()).ob_size = self.len as Py_ssize_t; self.resize(self.len as isize); NonNull::new_unchecked(self.bytes as *mut PyObject) diff --git a/test/test_api.py b/test/test_api.py index 13215d5f..0e5a6f14 100644 --- a/test/test_api.py +++ b/test/test_api.py @@ -200,3 +200,10 @@ def test_bytes_buffer(self): self.assertEqual( orjson.dumps([a, b, c]), f'["{a}","{b}","{c}"]'.encode("utf-8") ) + + def test_bytes_null_terminated(self): + """ + dumps() PyBytesObject buffer is null-terminated + """ + # would raise ValueError: invalid literal for int() with base 10: b'1596728892' + int(orjson.dumps(1596728892))