Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

string length overflows to negative when executing #107450

Closed
2 tasks done
mqyhlkahu opened this issue Jul 29, 2023 · 4 comments
Closed
2 tasks done

string length overflows to negative when executing #107450

mqyhlkahu opened this issue Jul 29, 2023 · 4 comments
Labels
3.11 only security fixes 3.12 bugs and security fixes 3.13 bugs and security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) topic-parser type-bug An unexpected behavior, bug, or error

Comments

@mqyhlkahu
Copy link

mqyhlkahu commented Jul 29, 2023

Bug report

Checklist

  • I am confident this is a bug in CPython,
    not a bug in a third-party project
  • I have searched the CPython issue tracker,
    and am confident this bug has not been reported before

A clear and concise description of the bug

The following code causes an error to be raised in all tested versions of Python.

exec(f"if True:\n {' ' * 2**31}print('hello world')")

Which error is raised varies by version, and as such the specific error message is listed with its corresponding version in the below section.

Some (hopefully) helpful information:

  • I have confirmed that the creation of the string is not causing a problem (by creating it separately, storing it in a variable, and then exec'ing it), the problem occurs when attempting to exec it.
  • I have confirmed that the issue is does not occur solely because of the large string. (The same issue does not occur when I use a value of, for example, 2**33).

NOTE:

I believe that this error will also occur when running real file rather than building a string and using exec, but I have not yet confirmed that.

Your environment

I tested this in three environments, with as many versions of Python as I could conveniently use.

Arch GNU/Linux

  • Operating system and architecture:
    • Operating system: Arch GNU/Linux
    • CPU: Intel(R) Core(TM) i7-4980HQ CPU @ 2.80GHz 2.80 GHz
  • CPython versions:
    • Python 3.10.10 (main, Mar 5 2023, 22:56:53) [GCC 12.2.1 20230201]
      • IndentationError: unindent does not match any outer indentation level
    • Python 3.11.3 (main, Jun 5 2023, 09:32:32) [GCC 13.1.1 20230429]
      • SystemError: Negative size passed to PyUnicode_New

Microsoft Windows 10

  • Operating system and architecture:
    • Operating system: Microsoft Windows 10 (version 22H2 build 19045.3208) inside a VirtualBox virtual machine
    • CPU: Intel(R) Core(TM) i7-4980HQ CPU @ 2.80GHz 2.80 GHz
  • CPython versions:
    • Python 3.7.9 (tags/v3.7.9:13c94747c7, Aug 17 2020, 16:30:00) [MSC v.1900 64 bit (AMD64)]
      • IndentationError: None
    • Python 3.8.10 (tags/v3.8.10:3d8993a, May 3 2021, 11:48:03) [MSC v.1928 64 bit (AMD64)]
      • IndentationError: None
    • Python 3.9.13 (tags/v3.9.13:6de2ca5, May 17 2022, 16:36:42) [MSC v.1929 64 bit (AMD64)]
      • IndentationError: unindent does not match any outer indentation level
    • Python 3.10.11 (tags/v3.10.11:7d4cc5a, Apr 5 2023, 00:38:17) [MSC v.1929 64 bit (AMD64)]
      • IndentationError: unindent does not match any outer indentation level
    • Python 3.11.4 (tags/v3.11.4:d2340ef, Jun 7 2023, 05:45:37) [MSC v.1934 64 bit (AMD64)]
      • SystemError: Negative size passed to PyUnicode_New

Ubuntu GNU/Linux

  • Operating system and architecture:
    • Operating system: Ubuntu 22.04 GNU/Linux inside a VirtualBox virtual machine
    • CPU: Intel(R) Core(TM) i7-4980HQ CPU @ 2.80GHz 2.80 GHz
  • CPython versions:
    • Python 3.7.17 (default, Jun 6 2023, 20:10:09) [GCC 11.3.0]
      • IndentationError: None
    • Python 3.8.17 (default, Jun 6 2023, 20:10:50) [GCC 11.3.0]
      • IndentationError: None
    • Python 3.9.17 (main, Jun 6 2023, 20:11:21) [GCC 11.3.0]
      • IndentationError: unindent does not match any outer indentation level
    • Python 3.10.6 (main, May 29 2023, 11:10:38) [GCC 11.3.0]
      • IndentationError: unindent does not match any outer indentation level
    • Python 3.11.4 (main, Jun 7 2023, 12:45:48) [GCC 11.3.0]
      • SystemError: Negative size passed to PyUnicode_New

Should any further information be required about the environments that I used, please ask.

What I believe causes the problem

I suspect very strongly that the error occurs as a result of an integer overflow. (A value larger than 2**31 - 1 will overflow and become negative when using 2's complement with 32 bit signed integers).

Possible solution

I think that this can be fixed by using a 64 bit integer to hold the length of the string while 64 bit platforms. This, while still leaving the error theoretically possible, would in practice avoid the error entirely because one would encounter a MemoryError first.

Linked PRs

@mqyhlkahu mqyhlkahu added the type-bug An unexpected behavior, bug, or error label Jul 29, 2023
@mqyhlkahu mqyhlkahu changed the title Indentation string length overflow string length overflows to negative when executing Jul 30, 2023
@terryjreedy terryjreedy added the interpreter-core (Objects, Python, Grammar, and Parser dirs) label Jul 30, 2023
@terryjreedy
Copy link
Member

There have been multiple reports that extreme artificial code produces compile problems or runtime problems, either exceptions or crashes. The answer for some is "don't do that; all compilers have limits".

'exec' compiles and runs. IndentationErrors are compile errors. Installed 3.12.0b4 Win 10, running compile on IDLE gives the same SystemError. Fresh 3.13.0a0 debug REPL crashes after perhaps a minute with C Runtime debug error: abort() has been called.

@serhiy-storchaka serhiy-storchaka added topic-parser 3.11 only security fixes 3.12 bugs and security fixes 3.13 bugs and security fixes labels Jul 30, 2023
@serhiy-storchaka
Copy link
Member

I agree that "don't do that" is the answer, but SystemError should never be triggered by the user Python code. It signals about improper use of the C API or some issues in the core. Although the former IndentationError was also not very useful. An error should explicitly report about the compiler limitation, not rely on the undefined behavior of integer overflow in C.

On main in debug build it crashes after long delay.

python: Parser/pegen_errors.c:237: _PyPegen_raise_error: Assertion `_Py_STATIC_CAST(intptr_t, _Py_STATIC_CAST(int, (p->tok->cur - start))) == (p->tok->cur - start)' failed.
Aborted (core dumped)

@mqyhlkahu
Copy link
Author

Thank you both for your response!

There have been multiple reports that extreme artificial code produces compile problems or runtime problems, either exceptions or crashes. The answer for some is "don't do that; all compilers have limits".

Yes, I am aware that compilers have limits, and I don't think that's a bad thing! It would take a lot more work, and quite probably be a lot slower, were these limitations to be removed.

However, I am of the opinion that:

  1. SystemErrors shouldn't be raised because of user code (except for raise SystemError and the like, of course).
  2. The previous IndentationErrors are also bad, because they don't really explain the problem (and, in the case of 3.9 / 3.10, actively mislead the user).

While I would certainly prefer this to be fixed, I understand that it probably won't be. However, I feel that this should at least provide a useful error message (and not provide a SystemError), perhaps something along the lines of the exception that results from

raise BaseException("compiler error") from OverflowError("string length overflowed")

It would hopefully utilise BaseException so that the error is less likely to be caught unintentionally by someone. (Ideally, there would be an exception-type derived from BaseException called CompilerError or something like that, but introducing a new exception-type is FAR beyond the scope of this issue).

miss-islington pushed a commit to miss-islington/cpython that referenced this issue Oct 12, 2023
…ows (pythonGH-110754)

(cherry picked from commit fb7843e)

Co-authored-by: Lysandros Nikolaou <lisandrosnik@gmail.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Oct 12, 2023
…ows (pythonGH-110754)

(cherry picked from commit fb7843e)

Co-authored-by: Lysandros Nikolaou <lisandrosnik@gmail.com>
lysnikolaou added a commit that referenced this issue Oct 12, 2023
…lows (GH-110754) (#110763)

(cherry picked from commit fb7843e)

Co-authored-by: Lysandros Nikolaou <lisandrosnik@gmail.com>
lysnikolaou added a commit that referenced this issue Oct 12, 2023
…lows (GH-110754) (#110762)

(cherry picked from commit fb7843e)

Co-authored-by: Lysandros Nikolaou <lisandrosnik@gmail.com>
@serhiy-storchaka
Copy link
Member

The test should be decorated with @bigmemtest.

lysnikolaou added a commit to lysnikolaou/cpython that referenced this issue Oct 12, 2023
ambv pushed a commit that referenced this issue Oct 13, 2023
Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Oct 13, 2023
…ythonGH-110768)

(cherry picked from commit 05439d3)

Co-authored-by: Lysandros Nikolaou <lisandrosnik@gmail.com>
Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Oct 13, 2023
…ythonGH-110768)

(cherry picked from commit 05439d3)

Co-authored-by: Lysandros Nikolaou <lisandrosnik@gmail.com>
Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
lysnikolaou added a commit to lysnikolaou/cpython that referenced this issue Oct 13, 2023
lysnikolaou added a commit to lysnikolaou/cpython that referenced this issue Oct 13, 2023
lysnikolaou added a commit that referenced this issue Oct 13, 2023
…H-110768) (#110808)

(cherry picked from commit 05439d3)

Co-authored-by: Lysandros Nikolaou <lisandrosnik@gmail.com>
Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
lysnikolaou added a commit that referenced this issue Oct 13, 2023
…H-110768) (#110809)

(cherry picked from commit 05439d3)

Co-authored-by: Lysandros Nikolaou <lisandrosnik@gmail.com>
Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
lysnikolaou added a commit to lysnikolaou/cpython that referenced this issue Oct 13, 2023
lysnikolaou added a commit to lysnikolaou/cpython that referenced this issue Oct 13, 2023
… test

Co-authored-by: Filipe Laíns <lains@riseup.net>
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Oct 14, 2023
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Oct 14, 2023
serhiy-storchaka added a commit to serhiy-storchaka/cpython that referenced this issue Oct 14, 2023
lysnikolaou added a commit that referenced this issue Oct 16, 2023
…110832)

Co-authored-by: Filipe Laíns <lains@riseup.net>
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
lysnikolaou added a commit to lysnikolaou/cpython that referenced this issue Oct 16, 2023
…verflow test (pythonGH-110832)

(cherry picked from commit a1ac559)

Co-authored-by: Lysandros Nikolaou <lisandrosnik@gmail.com>
Co-authored-by: Filipe Laíns <lains@riseup.net>
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
lysnikolaou added a commit to lysnikolaou/cpython that referenced this issue Oct 16, 2023
…verflow test (pythonGH-110832)

(cherry picked from commit a1ac559)

Co-authored-by: Lysandros Nikolaou <lisandrosnik@gmail.com>
Co-authored-by: Filipe Laíns <lains@riseup.net>
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
lysnikolaou added a commit to lysnikolaou/cpython that referenced this issue Oct 16, 2023
lysnikolaou added a commit that referenced this issue Oct 16, 2023
…w test (GH-110832) (#110931)

(cherry picked from commit a1ac559)

Co-authored-by: Lysandros Nikolaou <lisandrosnik@gmail.com>
Co-authored-by: Filipe Laíns <lains@riseup.net>
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
vstinner pushed a commit that referenced this issue Oct 17, 2023
…w test (GH-110832) (#110939)

(cherry picked from commit a1ac559)

Co-authored-by: Filipe Laíns <lains@riseup.net>
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
aisk pushed a commit to aisk/cpython that referenced this issue Feb 11, 2024
… test (python#110832)

Co-authored-by: Filipe Laíns <lains@riseup.net>
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
aisk pushed a commit to aisk/cpython that referenced this issue Feb 11, 2024
Glyphack pushed a commit to Glyphack/cpython that referenced this issue Sep 2, 2024
Glyphack pushed a commit to Glyphack/cpython that referenced this issue Sep 2, 2024
Glyphack pushed a commit to Glyphack/cpython that referenced this issue Sep 2, 2024
Glyphack pushed a commit to Glyphack/cpython that referenced this issue Sep 2, 2024
… test (python#110832)

Co-authored-by: Filipe Laíns <lains@riseup.net>
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Glyphack pushed a commit to Glyphack/cpython that referenced this issue Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.11 only security fixes 3.12 bugs and security fixes 3.13 bugs and security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) topic-parser type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

4 participants