Skip to content

Commit

Permalink
Null terminate PyBytesObject.ob_sval
Browse files Browse the repository at this point in the history
  • Loading branch information
ijl committed Jun 3, 2022
1 parent 651b700 commit d4146d1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/serialize/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ impl BytesWriter {

pub fn finish(&mut self) -> NonNull<PyObject> {
unsafe {
unsafe {
std::ptr::write(self.buffer_ptr(), 0);
};
(*self.bytes.cast::<PyVarObject>()).ob_size = self.len as Py_ssize_t;
self.resize(self.len as isize);
NonNull::new_unchecked(self.bytes as *mut PyObject)
Expand Down
7 changes: 7 additions & 0 deletions test/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

0 comments on commit d4146d1

Please sign in to comment.