How can I crash Python 3.5?

We use the built-in cpython as the scripting language in our application. We modify our sys.path at startup to make sure that we are not executing code from outside our application, but the user with sitecustomize.py in his PYTHONPATH causes the code to execute before we can fix sys.path and we believe that in their code a serious failure occurred (not an exception that site.py will cleverly handle and process).

I believe that the correct solution would be to simply clear the PYTHONPATH variable from the environment before we initialize python, but I cannot verify this properly because I cannot recreate the problem.

The easiest way I've found is to use ctypes to write to memory, for example:

import ctypes
p = (ctypes.c_char).from_address(0)
while True:
  p[0] = 0
  p = p + 1

But in Python 3.5, it does not allow me to write to c_char types, throwing an error "TypeError: object" c_char "does not support element assignment."

I tried several methods available at https://wiki.python.org/moin/CrashingPython, but to no avail.

Is there any reliable way to crash Python 3.5 from pure Python code?

+5
source share
4 answers

There are many ways to get through ctypes. For example, a revised version of your code:

p = ctypes.pointer(ctypes.c_char.from_address(5))
p[0] = b'x'

If you do not want to use ctypes, you can cause the C stack overflow in the implementation dict.__repr__:

x = {}
for i in range(1000000):
    x = {1: x}
repr(x)

Python, .

, -, Python , , - .

+2

:

exec(type((lambda:0).__code__)(0,1,0,0,0,b'',(),(),(),'','',1,b''))

code-golf:

+1

Python 2.7, , , . Python, , , SystemExit. :

for i in range (0, 10):
    if (i == 6): #Stop the program at 6 just.. because
        raise SystemExit
    print(i)

, !

!

0

Python:

def crash():
    try:
        crash()
    except:
        crash()

crash()
0

Source: https://habr.com/ru/post/1671196/


All Articles