clarete / forbiddenfruit

Patch built-in python objects

Home Page:https://clarete.li/forbiddenfruit/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Crash on Windows if dunder methods cursed into `ctypes.c_int` raises an exception

EZForever opened this issue · comments

In addition to #56, if the dunder method is cursed into ctypes' integer types, a raised exception causes Python to crash on Windows.

Proof of concept:

#!/usr/bin/env python3

from ctypes import c_int
from forbiddenfruit import curse

def c_int_add(self: c_int, other: int) -> c_int:
    raise TypeError('This exception should be caught')

curse(c_int, '__add__', c_int_add)

try:
    print(c_int(1) + 1) # TypeError (correct) then crash on Windows (wrong)
except TypeError:
    print('TypeError caught')

print('If this gets printed then nothing has crashed')

Test result on Windows (version 10.0.19043.1052, Python 3.8.6, both 64-bit):

(venv) C:\dev\test> python bug2.py 
Exception ignored on calling ctypes callback function: <function c_int_add at 0x000001E12AC1FDC0>
Traceback (most recent call last):
  File "E:\src\pytrickz\venv\lib\site-packages\forbiddenfruit\__init__.py", line 328, in wrapper
    return func(*args, **kwargs)
  File "bug2.py", line 7, in c_int_add
    raise TypeError('This exception should be caught')
TypeError: This exception should be caught

(venv) C:\dev\test> echo %=ExitCode%
C0000005

(venv) C:\dev\test>