Skip to content

Instantly share code, notes, and snippets.

@qexat
Created January 29, 2024 10:32
Show Gist options
  • Save qexat/819805b105168e26fd95fd12d7da2bb7 to your computer and use it in GitHub Desktop.
Save qexat/819805b105168e26fd95fd12d7da2bb7 to your computer and use it in GitHub Desktop.
everything is fine.. until it's not!
import ctypes
import dataclasses
import faulthandler
import typing
@dataclasses.dataclass(slots=True, repr=False)
class Human:
name: str
age: int = 0
def main() -> None:
faulthandler.enable()
john = Human("John", age=33)
john_c = typing.cast(
"ctypes.py_object[Human]", ctypes.cast(id(john), ctypes.py_object)
)
ctypes.resize(john_c, 16)
john = typing.cast(
"ctypes.py_object[ctypes.py_object[Human]]",
ctypes.cast(id(john_c), ctypes.py_object),
).value.value
print(john) # it's fine
if __name__ == "__main__":
main()
import ctypes
import dataclasses
import faulthandler
import typing
@dataclasses.dataclass(slots=True, repr=False)
class Human:
name: str
age: int = 0
def main() -> None:
faulthandler.enable()
john = Human("John", age=33)
john_c = typing.cast(
"ctypes.py_object[Human]", ctypes.cast(id(john), ctypes.py_object)
)
ctypes.resize(john_c, 16)
john = typing.cast(
"ctypes.py_object[ctypes.py_object[Human]]",
ctypes.cast(id(john_c), ctypes.py_object),
).value
print(john.value) # segfaults!
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment