[New-bugs-announce] [issue34468] An always-false condition in range_repr() from Objects/rangeobject.c

Alexey Izbyshev report at bugs.python.org
Wed Aug 22 19:57:14 EDT 2018

New submission from Alexey Izbyshev <izbyshev at ispras.ru>:

range_repr() contains the following snippet:

    /* Check for special case values for printing.  We don't always
       need the step value.  We don't care about errors
       (it means overflow), so clear the errors. */
    istep = PyNumber_AsSsize_t(r->step, NULL);
    if (istep != 1 || (istep == -1 && PyErr_Occurred())) {

The second condition in 'if' statement is always false (reported by Svace static analyzer). Moreover, the comment is incorrect. It implies that any error from PyNumber_AsSsize_t() means that an overflow occurred, but according to its implementation and documentation, any error is guaranteed *not* to be an OverflowError if the second parameter is NULL (the result is clamped on overflow).

In practice, 'range' invariants currently guarantee that no error can occur in this case because 'r->step' is a subtype of int (enforced in validate_step()). So the 'if' statement can be replaced either with an assert:

    assert(!(istep == -1 && PyErr_Occurred()))

or with a conservative check:

    if (istep == -1 && PyErr_Occurred())) {
        // may also add assert(!PyErr_ExceptionMatches( PyExc_OverflowError);
        return NULL;

Suggestions are appreciated.

components: Interpreter Core
messages: 323912
nosy: izbyshev, ncoghlan, serhiy.storchaka
priority: normal
severity: normal
status: open
title: An always-false condition in range_repr() from Objects/rangeobject.c
type: behavior
versions: Python 3.6, Python 3.7, Python 3.8

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list