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

safe_range can consume all available memory on Python 2 #933

Closed
alekseyev opened this issue Dec 13, 2018 · 1 comment
Closed

safe_range can consume all available memory on Python 2 #933

alekseyev opened this issue Dec 13, 2018 · 1 comment

Comments

@alekseyev
Copy link

@alekseyev alekseyev commented Dec 13, 2018

safe_range has previously used xrange to get the length of a sequence.

    rng = xrange(*args)
    if len(rng) > MAX_RANGE:
        raise OverflowError('range too big, maximum size for range is %d' %
                            MAX_RANGE)

However since porting to Python 3 it uses range which behaves differently in Python 2 and 3. On Python 2 it will try to generate the whole sequence in memory before measuring the length -- which can cause an out of memory error.


Expected Behavior

safe_range(100000000000) should raise an OverflowError

Actual Behavior

safe_range(100000000000) attempts to generate a huge list in memory

Template Code

{%for n in range(100000000000) %}{{n}}{% endfor %}

Your Environment

  • Python version: 2.7
  • Jinja version: 2.10
@mitsuhiko
Copy link
Member

@mitsuhiko mitsuhiko commented Feb 18, 2019

Looks like this was an oversight on my part when I pulled in some Python 3 changes. This should be changed to use xrange again on 2.x

Loading

@davidism davidism closed this Jul 22, 2019
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 13, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

3 participants