Skip to content

Commit 4ee40c7

Browse files
committed
fix: Improve error handling
1 parent 84ca45e commit 4ee40c7

File tree

1 file changed

+104
-13
lines changed

1 file changed

+104
-13
lines changed

src/c2pa/c2pa.py

Lines changed: 104 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,21 +1194,67 @@ class Reader:
11941194

11951195
@classmethod
11961196
def get_supported_mime_types(cls) -> list[str]:
1197+
"""Get the list of supported MIME types for the Reader.
1198+
1199+
This method retrieves supported MIME types from the native library
1200+
with proper pointer validation and error handling.
1201+
1202+
Returns:
1203+
List of supported MIME type strings
1204+
1205+
Raises:
1206+
C2paError: If there was an error retrieving the MIME types
1207+
"""
11971208
if cls._supported_mime_types_cache is not None:
11981209
return cls._supported_mime_types_cache
11991210

12001211
count = ctypes.c_size_t()
12011212
arr = _lib.c2pa_reader_supported_mime_types(ctypes.byref(count))
12021213

1214+
# Validate the returned array pointer
1215+
if not arr:
1216+
# If no array returned, check for errors
1217+
error = _parse_operation_result_for_error(_lib.c2pa_error())
1218+
if error:
1219+
raise C2paError(f"Failed to get supported MIME types: {error}")
1220+
# Return empty list if no error but no array
1221+
return []
1222+
1223+
# Validate count value
1224+
if count.value <= 0:
1225+
# Free the array even if count is invalid
1226+
try:
1227+
_lib.c2pa_free_string_array(arr, count.value)
1228+
except Exception:
1229+
pass
1230+
return []
1231+
12031232
try:
1204-
# CDecode values to place them in Python managed memory
1205-
result = [arr[i].decode("utf-8") for i in range(count.value)]
1233+
result = []
1234+
for i in range(count.value):
1235+
try:
1236+
# Validate each array element before accessing
1237+
if arr[i] is None:
1238+
continue
1239+
1240+
mime_type = arr[i].decode("utf-8", errors='replace')
1241+
if mime_type:
1242+
result.append(mime_type)
1243+
except Exception:
1244+
# Ignore cleanup errors
1245+
continue
12061246
finally:
1207-
# Release native memory, as per API contract
1208-
# c2pa_reader_supported_mime_types must call c2pa_free_string_array
1209-
_lib.c2pa_free_string_array(arr, count.value)
1247+
# Always free the native memory, even if string extraction fails
1248+
try:
1249+
_lib.c2pa_free_string_array(arr, count.value)
1250+
except Exception:
1251+
# Ignore cleanup errors
1252+
pass
1253+
1254+
# Cache the result
1255+
if result:
1256+
cls._supported_mime_types_cache = result
12101257

1211-
cls._supported_mime_types_cache = result
12121258
return cls._supported_mime_types_cache
12131259

12141260
def __init__(self,
@@ -1798,22 +1844,67 @@ class Builder:
17981844

17991845
@classmethod
18001846
def get_supported_mime_types(cls) -> list[str]:
1847+
"""Get the list of supported MIME types for the Builder.
1848+
1849+
This method retrieves supported MIME types from the native library
1850+
with proper pointer validation and error handling.
1851+
1852+
Returns:
1853+
List of supported MIME type strings
1854+
1855+
Raises:
1856+
C2paError: If there was an error retrieving the MIME types
1857+
"""
18011858
if cls._supported_mime_types_cache is not None:
18021859
return cls._supported_mime_types_cache
18031860

18041861
count = ctypes.c_size_t()
18051862
arr = _lib.c2pa_builder_supported_mime_types(ctypes.byref(count))
18061863

1864+
# Validate the returned array pointer
1865+
if not arr:
1866+
# If no array returned, check for errors
1867+
error = _parse_operation_result_for_error(_lib.c2pa_error())
1868+
if error:
1869+
raise C2paError(f"Failed to get supported MIME types: {error}")
1870+
# Return empty list if no error but no array
1871+
return []
1872+
1873+
# Validate count value
1874+
if count.value <= 0:
1875+
# Free the array even if count is invalid
1876+
try:
1877+
_lib.c2pa_free_string_array(arr, count.value)
1878+
except Exception:
1879+
pass
1880+
return []
1881+
18071882
try:
1808-
# CDecode values to place them in Python managed memory
1809-
result = [arr[i].decode("utf-8") for i in range(count.value)]
1883+
result = []
1884+
for i in range(count.value):
1885+
try:
1886+
# Validate each array element before accessing
1887+
if arr[i] is None:
1888+
continue
1889+
1890+
mime_type = arr[i].decode("utf-8", errors='replace')
1891+
if mime_type:
1892+
result.append(mime_type)
1893+
except Exception:
1894+
# Ignore decoding failures
1895+
continue
18101896
finally:
1811-
# Release native memory, as per API contract
1812-
# c2pa_builder_supported_mime_types must call
1813-
# c2pa_free_string_array
1814-
_lib.c2pa_free_string_array(arr, count.value)
1897+
# Always free the native memory, even if string extraction fails
1898+
try:
1899+
_lib.c2pa_free_string_array(arr, count.value)
1900+
except Exception:
1901+
# Ignore cleanup errors
1902+
pass
1903+
1904+
# Cache the result
1905+
if result:
1906+
cls._supported_mime_types_cache = result
18151907

1816-
cls._supported_mime_types_cache = result
18171908
return cls._supported_mime_types_cache
18181909

18191910
def __init__(self, manifest_json: Any):

0 commit comments

Comments
 (0)