Skip to content

safe_range can consume all available memory on Python 2 #933

@alekseyev

Description

@alekseyev

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions