Skip to content

Commit 4c308f0

Browse files
committed
fix: preserve nul bytes in Go buffers
1 parent 5ada4ed commit 4c308f0

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

neonize/_binder.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,10 @@ def load_goneonize():
4040

4141

4242
class Bytes(ctypes.Structure):
43-
_fields_ = [("ptr", ctypes.c_char_p), ("size", ctypes.c_size_t)]
43+
# Go returns raw binary buffers that may contain NUL bytes. Do not use
44+
# c_char_p here because ctypes treats it as a NUL-terminated C string and
45+
# can truncate/corrupt media bytes such as JPEG/PNG downloads.
46+
_fields_ = [("ptr", ctypes.c_void_p), ("size", ctypes.c_size_t)]
4447

4548
def get_bytes(self):
4649
return ctypes.string_at(self.ptr, self.size)

tests/test_binder.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import ctypes
2+
import os
3+
4+
os.environ["SPHINX"] = "1"
5+
6+
from neonize._binder import Bytes
7+
8+
9+
def test_bytes_get_bytes_preserves_nul_bytes():
10+
payload = b"\xff\xd8\xff\xe0\x00\x10JFIF\x00binary\x00tail"
11+
buffer = ctypes.create_string_buffer(payload)
12+
data = Bytes(ctypes.cast(buffer, ctypes.c_void_p), len(payload))
13+
14+
assert data.get_bytes() == payload

0 commit comments

Comments
 (0)