Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
19b28fc
CVE-2020-10735: Prevent DoS by very large int()
tiran May 5, 2020
0a96b20
Default to disable, improve tests and docs
tiran Jan 19, 2022
88f6d5d
fix typo
tiran Jan 19, 2022
70c195e
More docs (WIP)
tiran Jan 19, 2022
e17e93b
Basic documentation for sys functions
tiran Jan 19, 2022
fbd14b7
Use ValueError, ignore underscore, scale limit
tiran Jan 20, 2022
dd74d70
Fix CI
tiran Jan 20, 2022
0e01461
Address Greg's review
tiran Aug 1, 2022
0b21e5f
Fix sys.flags len and docs
tiran Aug 1, 2022
3b38abe
Keep the warning, but remove advice about limiting input length in th…
gpshead Aug 2, 2022
37193ed
Renamed the APIs & too many other refactorings.
gpshead Aug 5, 2022
c90b79f
Improve the configuring docs.
gpshead Aug 7, 2022
fea25ea
Stop tying to base10, just use string digits.
gpshead Aug 7, 2022
ac9f22f
Remove the added now-unneeded helper log tbl fn.
gpshead Aug 7, 2022
da72dd1
prevent intdostimeit from emitting errors in test_tools.
gpshead Aug 7, 2022
d7e4d7b
Remove a leftover base 10 reference. clarify.
gpshead Aug 7, 2022
5c7e6d5
versionadded/changed to 3.12
gpshead Aug 7, 2022
61a5bc9
Link to the CVE from the main doc.
gpshead Aug 7, 2022
c15adde
Add a What's New entry.
gpshead Aug 7, 2022
76ae1c2
Add a Misc/NEWS.d entry.
gpshead Aug 7, 2022
1ad88f5
Undo addition to PyConfig to ease backporting.
gpshead Aug 8, 2022
0c83111
Remove the Tools/scripts/ example and timing code.
gpshead Aug 8, 2022
5d39ab6
un-add the <math.h> include (not needed for PR anymore)
gpshead Aug 8, 2022
5b77b3e
Remove added unused imports.
gpshead Aug 8, 2022
de00cdc
Tabs -> Spaces
gpshead Aug 8, 2022
3cc8553
make html and make doctest in Doc pass.
gpshead Aug 8, 2022
da97e65
Raise the default limit and the threshold.
gpshead Aug 10, 2022
ef03a16
Remove xmlrpc.client changes, test-only.
gpshead Aug 12, 2022
e916845
Rearrange the new stdtypes docs, w/limits + caution.
gpshead Aug 13, 2022
101502e
Make a huge int a SyntaxError with lineno when parsing.
gpshead Aug 16, 2022
fa8a58a
Mention the chosen default in the NEWS entry.
gpshead Aug 16, 2022
313ab6d
Properly clear & free the prior exception.
gpshead Aug 16, 2022
614cd02
Add a note to the float.as_integer_ratio() docs.
gpshead Aug 17, 2022
16ad090
Clarify the documentation wording and error msg.
gpshead Aug 17, 2022
4eb72e6
Fix test_idle, it used a long int on a line.
gpshead Aug 17, 2022
da36550
Rename the test.support context manager and document it.
gpshead Aug 19, 2022
f4372cc
Documentation cleanup.
gpshead Aug 19, 2022
c421853
Update attribution in Misc/NEWS.d
gpshead Aug 25, 2022
9f2168a
Regen global strings
tiran Sep 1, 2022
3c8504b
Make the doctest actually run & fix it.
gpshead Sep 1, 2022
1586419
Fix the docs build.
gpshead Sep 2, 2022
94bd3ee
Rename the news file to appease the Bedevere bot.
gpshead Sep 2, 2022
0b91f65
Regen argument clinic after the rebase merge.
gpshead Sep 2, 2022
02776f9
Hexi hexa
tiran Sep 2, 2022
173fa4e
Hexi hexa 2
tiran Sep 2, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
More docs (WIP)
  • Loading branch information
tiran authored and gpshead committed Sep 2, 2022
commit 70c195ea1841cf6fbf069699248138f81ab2ced4
3 changes: 2 additions & 1 deletion Doc/library/functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -911,7 +911,8 @@ are always available. They are listed here in alphabetical order.
The delegation to :meth:`__trunc__` is deprecated.

.. versionchanged:: 3.12
:class:`int` are now limited, :func:`sys.setintmaxdigits` TODO
:class:`int` string inputs are now limited, see :ref:`int maximum
digits limitation <intmaxdigits>`.

.. function:: isinstance(object, classinfo)

Expand Down
66 changes: 66 additions & 0 deletions Doc/library/stdtypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5460,6 +5460,72 @@ types, where they are relevant. Some of these are not reported by the
[<class 'bool'>]


.. _intmaxdigits:

Integer maximum digits limitation
=================================

CPython has a global limit for converting between :class:`int` and class:`str`
to mitigate denial of service attacks. The limit is necessary because there
exists no efficient algorithm, that can convert a string to an integer or
an integer to a string in linear time, unless the base is a power of *2*. Even
the best known algorithms for base *10* have sub-quadratic complexity. A large
input like::

int('1' * 500_000)

takes about a second at 100% CPU load on an X86_64 CPU from 2020 with 4.2 GHz
max frequency.

Configure limitations
---------------------

* :data:`sys.int_info.default_max_digits` is the compiled-in default value.
* :data:`sys.int_info.max_digits_check_threshold` is the minimum limit for
digit limitation. For performance reasons Python does not check

* :envvar:`PYTHONINTMAXDIGITS`, e.g. ``PYTHONINTMAXDIGITS=4096 python3`` to
set the limit to ``4096`` or ``PYTHONINTMAXDIGITS=0 python3`` to disable
the limitation
* :option:`-X intmaxdigits <-X>`, e.g. ``python3 -X intmaxdigits=4096``
* :data:`sys.flags.intmaxdigits` contains the value of
:envvar:`PYTHONINTMAXDIGITS` or :option:`-X intmaxdigits <-X>`. In case
both the env var and the ``-X`` option are set, the ``-X`` option takes
precedence. The flag defaults to *-1*.

* :func:`sys.getintmaxdigits` and :func:`sys.setintmaxdigits` are getter
and setter for interpreter-wide limit.

Recommended configuration::

import sys
if hasattr(sys.flags, "intmaxdigits") and sys.flags.intmaxdigits == -1:
sys.setintmaxdigits(4096)

Affected APIs
-------------

The limition only applies to slow conversions between :class:`int` and
class:`str`:

* ``int(string)`` with default base 10.
* ``int(string, base)`` for all bases that are not power of 2.
* ``str(integer)``
* ``repr(integer)``
* any other string conversion to base 10, for example ``f"{integer}"``,
``"{}".format(integer)``, or ``"%d" % integer``.

The limitations do not apply to functions with a linear algorithm:

* ``int(string, base)`` with base 2, 4, 8, 16, or 32
* :func:`int.from_bytes` and :func:`int.to_bytes`
* :func:`hex`, :func:`oct`, :func:`bin` (the resulting string may consume
a substantial amount of memory)
* :ref:`formatspec` for hex, octet, and binary types
* :class:`str` to :class:`float`
* :class:`str` to :class:`decimal.Decimal`


.. rubric:: Footnotes

.. [1] Additional information on these special methods may be found in the Python
Expand Down
8 changes: 5 additions & 3 deletions Doc/library/sys.rst
Original file line number Diff line number Diff line change
Expand Up @@ -729,9 +729,10 @@ always available.

.. function:: getintmaxdigits()

Return limit for int digits, :func:`setintmaxdigits` TODO
Return current global value for :ref:`int maximum digits limitation
<intmaxdigits>`. See also :func:`setintmaxdigits`

.. versionadded:: 3.9
.. versionadded:: 3.11

.. function:: getrefcount(object)

Expand Down Expand Up @@ -1328,7 +1329,8 @@ always available.

.. function:: setintmaxdigits(n)

Set maximum amount of int digits, :func:`getintmaxdigits` TODO
Set global interpreter limit for :ref:`int maximum digits limitation
<intmaxdigits>`. See also :func:`getintmaxdigits`

.. versionadded:: 3.9

Expand Down
7 changes: 4 additions & 3 deletions Doc/using/cmdline.rst
Original file line number Diff line number Diff line change
Expand Up @@ -505,8 +505,8 @@ Miscellaneous options
stored in a traceback of a trace. Use ``-X tracemalloc=NFRAME`` to start
tracing with a traceback limit of *NFRAME* frames. See the
:func:`tracemalloc.start` for more information.
* ``-X intmaxdigits`` to enable or disable int conversion limit.
See also :envvar:`PYTHONPROFILEIMPORTTIME`.
* ``-X intmaxdigits`` configures :ref:`int maximum digits limitation
<intmaxdigits>`. See also :envvar:`PYTHONPROFILEIMPORTTIME`.
* ``-X importtime`` to show how long each import takes. It shows module
name, cumulative time (including nested imports) and self time (excluding
nested imports). Note that its output may be broken in multi-threaded
Expand Down Expand Up @@ -767,7 +767,8 @@ conflict.

.. envvar:: PYTHONINTMAXDIGITS

TODO
If this variable is set to an integer, it is used to configure the interpreter's
global :ref:`int maximum digits limitation <intmaxdigits>`.

.. versionadded:: 3.11

Expand Down