New submission from Alexey Izbyshev <izbys...@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())) {
        PyErr_Clear();
    }

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 <rep...@bugs.python.org>
<https://bugs.python.org/issue34468>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to