Skip to content

Commit 7b9698f

Browse files
committed
fix: Memory handling change
1 parent b20c9db commit 7b9698f

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

src/c2pa/c2pa.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,8 +1388,11 @@ def wrapped_callback(
13881388
if data_len > 1024 * 1024: # 1MB limit
13891389
return -1
13901390

1391-
# Convert C pointer to Python bytes using direct memory access
1392-
data = bytes(ctypes.cast(data_ptr, ctypes.POINTER(ctypes.c_ubyte * data_len)).contents)
1391+
# Recover signed data (copy, to avoid lifetime issues)
1392+
temp_buffer = (ctypes.c_ubyte * data_len)()
1393+
ctypes.memmove(temp_buffer, data_ptr, data_len)
1394+
data = bytes(temp_buffer)
1395+
13931396
if not data:
13941397
# Error: empty data, invalid so return -1,
13951398
# native code will also handle it!
@@ -1874,7 +1877,9 @@ def _sign_internal(
18741877
if manifest_bytes_ptr and result > 0:
18751878
try:
18761879
# Convert the C pointer to Python bytes
1877-
manifest_bytes = bytes(manifest_bytes_ptr[:result])
1880+
temp_buffer = (ctypes.c_ubyte * result)()
1881+
ctypes.memmove(temp_buffer, manifest_bytes_ptr, result)
1882+
manifest_bytes = bytes(temp_buffer)
18781883
except Exception:
18791884
# If there's any error accessing the memory, just return
18801885
# empty bytes

0 commit comments

Comments
 (0)