Skip to content

Commit d948655

Browse files
authored
fix: Do not miss freeing pointers on error (#271)
1 parent ddfc090 commit d948655

1 file changed

Lines changed: 26 additions & 11 deletions

File tree

src/c2pa/c2pa.py

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,7 +1425,12 @@ def __init__(self):
14251425
super().__init__()
14261426

14271427
ptr = _lib.c2pa_settings_new()
1428-
_check_ffi_operation_result(ptr, "Failed to create Settings")
1428+
try:
1429+
_check_ffi_operation_result(ptr, "Failed to create Settings")
1430+
except Exception:
1431+
if ptr:
1432+
ManagedResource._free_native_ptr(ptr)
1433+
raise
14291434

14301435
self._handle = ptr
14311436
self._lifecycle_state = LifecycleState.ACTIVE
@@ -2403,11 +2408,16 @@ def _init_from_context(self, context, format_or_path,
24032408
reader_ptr = _lib.c2pa_reader_from_context(
24042409
context.execution_context,
24052410
)
2406-
_check_ffi_operation_result(reader_ptr,
2407-
Reader._ERROR_MESSAGES[
2408-
'reader_error'
2409-
].format("Unknown error")
2410-
)
2411+
try:
2412+
_check_ffi_operation_result(reader_ptr,
2413+
Reader._ERROR_MESSAGES[
2414+
'reader_error'
2415+
].format("Unknown error")
2416+
)
2417+
except Exception:
2418+
if reader_ptr:
2419+
ManagedResource._free_native_ptr(reader_ptr)
2420+
raise
24112421

24122422
if manifest_data is not None:
24132423
if not isinstance(manifest_data, bytes):
@@ -3176,11 +3186,16 @@ def _init_from_context(self, context, json_str):
31763186
builder_ptr = _lib.c2pa_builder_from_context(
31773187
context.execution_context,
31783188
)
3179-
_check_ffi_operation_result(builder_ptr,
3180-
Builder._ERROR_MESSAGES[
3181-
'builder_error'
3182-
].format("Unknown error")
3183-
)
3189+
try:
3190+
_check_ffi_operation_result(builder_ptr,
3191+
Builder._ERROR_MESSAGES[
3192+
'builder_error'
3193+
].format("Unknown error")
3194+
)
3195+
except Exception:
3196+
if builder_ptr:
3197+
ManagedResource._free_native_ptr(builder_ptr)
3198+
raise
31843199

31853200
# Consume-and-return: builder_ptr is consumed,
31863201
# new_ptr is the valid pointer going forward

0 commit comments

Comments
 (0)