Repository: pdfminer/pdfminer.six

Issue:
OSS-Fuzz issue 412756041: OverflowError: int too large to convert to float
OSS-Fuzz has found a bug in this project. Please see https://oss-fuzz.com/testcase?key=4682737892261888 for details and reproducers.

This issue is mirrored from https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=412756041 and will auto-close if the status changes there.

If you have trouble accessing this report, please file an issue at https://github.com/google/oss-fuzz/issues/new.



Discussion:
```
 	OverflowError: int too large to convert to float
	Traceback (most recent call last):
	  File "fuzzing/page_extraction_fuzzer.py", line 27, in fuzz_one_input
	  File "pdfminer/high_level.py", line 224, in extract_pages
	  File "pdfminer/pdfinterp.py", line 1214, in process_page
	  File "pdfminer/pdfinterp.py", line 1226, in render_contents
	  File "pdfminer/pdfinterp.py", line 1261, in execute
	  File "pdfminer/pdfinterp.py", line 551, in do_c
	  File "pdfminer/casting.py", line 18, in safe_float
	OverflowError: int too large to convert to float
```
[clusterfuzz-testcase-minimized-page_extraction_fuzzer-4682737892261888.pdf](https://github.com/user-attachments/files/19871471/clusterfuzz-testcase-minimized-page_extraction_fuzzer-4682737892261888.pdf)



Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 1f04ad6c..f3dc3a00 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -12,7 +12,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
 ## Fixed
 
 - Correct tightest fitting bounding boxes for rotated content ([#1114](https://github.com/pdfminer/pdfminer.six/pull/1114))
- 
+- `OverflowError` in `safe_float` when input is too large ([#1121](https://github.com/pdfminer/pdfminer.six/pull/1121))
+
 ## [20250416]
 
 ### Fixed
@@ -253,7 +254,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
 - Hiding fallback xref by default from dumppdf.py output ([#431](https://github.com/pdfminer/pdfminer.six/pull/431))
 - Raise a warning instead of an error when extracting text from a non-extractable PDF ([#453](https://github.com/pdfminer/pdfminer.six/pull/453))
 - Switched from pycryptodome to cryptography package for AES decryption ([#456](https://github.com/pdfminer/pdfminer.six/pull/456))
-  
+
 ## [20200517]
 
 ### Added
diff --git a/pdfminer/casting.py b/pdfminer/casting.py
index 712e529a..63f94926 100644
--- a/pdfminer/casting.py
+++ b/pdfminer/casting.py
@@ -17,7 +17,7 @@ def safe_int(o: Any) -> Optional[int]:
 def safe_float(o: Any) -> Optional[float]:
     try:
         return float(o)
-    except (TypeError, ValueError):
+    except (TypeError, ValueError, OverflowError):
         return None
 
 


Test patch:
diff --git a/tests/test_casting.py b/tests/test_casting.py
index d3178f48..b771839b 100644
--- a/tests/test_casting.py
+++ b/tests/test_casting.py
@@ -2,7 +2,7 @@
 
 import pytest
 
-from pdfminer.casting import safe_rect_list
+from pdfminer.casting import safe_float, safe_rect_list
 from pdfminer.utils import Rect
 
 
@@ -22,3 +22,19 @@
 )
 def test_safe_rect_list(arg: Any, expected: Optional[Rect]) -> None:
     assert safe_rect_list(arg) == expected
+
+
+@pytest.mark.parametrize(
+    ("arg", "expected"),
+    [
+        (0, 0.0),
+        (1, 1.0),
+        ("0", 0.0),
+        ("1.5", 1.5),
+        (None, None),
+        (object(), None),
+        (2**1024, None),  # Integer too large to convert to float
+    ],
+)
+def test_safe_float(arg: Any, expected: Optional[float]) -> None:
+    assert safe_float(arg) == expected