|
18 | 18 | import tempfile |
19 | 19 | import threading |
20 | 20 | import unittest |
| 21 | +import subprocess |
| 22 | +import textwrap |
21 | 23 | from test import support |
22 | 24 | from test.support import _4G, bigmemtest |
23 | 25 | from test.support import hashlib_helper |
@@ -1201,6 +1203,43 @@ def test_readonly_types(self): |
1201 | 1203 | with self.assertRaisesRegex(TypeError, "immutable type"): |
1202 | 1204 | hash_type.value = False |
1203 | 1205 |
|
| 1206 | + @unittest.skipUnless(HASH is not None, 'need _hashlib') |
| 1207 | + def test_hashlib_init_memory_error_no_df(self): |
| 1208 | + """gh-145301 regression test.""" |
| 1209 | + |
| 1210 | + try: |
| 1211 | + import _testcapi |
| 1212 | + if not hasattr(_testcapi, 'set_nomemory'): |
| 1213 | + self.skipTest('requires _testcapi.set_nomemory') |
| 1214 | + except ImportError: |
| 1215 | + self.skipTest('requires _testcapi') |
| 1216 | + |
| 1217 | + code = textwrap.dedent(""" |
| 1218 | + import sys |
| 1219 | + import _testcapi |
| 1220 | +
|
| 1221 | + if '_hashlib' in sys.modules: |
| 1222 | + del sys.modules['_hashlib'] |
| 1223 | +
|
| 1224 | + _testcapi.set_nomemory(40, 41) |
| 1225 | + try: |
| 1226 | + import _hashlib |
| 1227 | + except (MemoryError, ImportError): |
| 1228 | + pass |
| 1229 | + finally: |
| 1230 | + _testcapi.remove_mem_hooks() |
| 1231 | + """) |
| 1232 | + |
| 1233 | + rc = subprocess.call( |
| 1234 | + [sys.executable, '-c', code], |
| 1235 | + stdout=subprocess.DEVNULL, |
| 1236 | + stderr=subprocess.DEVNULL, |
| 1237 | + ) |
| 1238 | + # rc < 0 means crash (signal on Unix), which indicates double-free |
| 1239 | + # rc >= 0 means normal exit (even with MemoryError), which is expected |
| 1240 | + self.assertGreaterEqual(rc, 0, |
| 1241 | + "Process crashed - Loss double-free in _hashlib") |
| 1242 | + |
1204 | 1243 |
|
1205 | 1244 | class KDFTests(unittest.TestCase): |
1206 | 1245 |
|
|
0 commit comments