@@ -1381,11 +1381,15 @@ def wrapped_callback(
13811381 # which can become tedious in handling. So we let the native code deal with it and
13821382 # raise the errors accordingly, since it already does checks.
13831383 try :
1384- if not data_ptr or data_len <= 0 :
1384+ if not data_ptr or data_len <= 0 or not signed_bytes_ptr or signed_len <= 0 :
13851385 # Error: invalid input, invalid so return -1,
13861386 # native code will handle it!
13871387 return - 1
13881388
1389+ # Validate buffer sizes before memory operations
1390+ if data_len > 1024 * 1024 : # 1MB limit
1391+ return - 1
1392+
13891393 # Convert C pointer to Python bytes
13901394 data = bytes (data_ptr [:data_len ])
13911395 if not data :
@@ -1418,8 +1422,9 @@ def wrapped_callback(
14181422
14191423 # Encode strings with error handling in case it's invalid UTF8
14201424 try :
1421- certs_bytes = certs .encode ('utf-8' )
1422- tsa_url_bytes = tsa_url .encode ('utf-8' ) if tsa_url else None
1425+ # Only encode if not already bytes, avoid unnecessary encoding
1426+ certs_bytes = certs .encode ('utf-8' ) if isinstance (certs , str ) else certs
1427+ tsa_url_bytes = tsa_url .encode ('utf-8' ) if tsa_url and isinstance (tsa_url , str ) else tsa_url
14231428 except UnicodeError as e :
14241429 raise C2paError .Encoding (
14251430 error_messages ['encoding_error' ].format (
0 commit comments