Skip to content

Commit e037563

Browse files
committed
Style fixes; make the conversion from unsigned long to long safe
1 parent 9f8caef commit e037563

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

Objects/rangeobject.c

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -183,30 +183,35 @@ static long compute_range_length_long(PyObject *start,
183183
int overflow = 0;
184184

185185
long long_start = PyLong_AsLongAndOverflow(start, &overflow);
186-
if (overflow)
186+
if (overflow) {
187187
return -2;
188-
if (long_start==-1 && PyErr_Occurred()) {
188+
}
189+
if (long_start == -1 && PyErr_Occurred()) {
189190
return -1;
190191
}
191192
long long_stop = PyLong_AsLongAndOverflow(stop, &overflow);
192-
if (overflow)
193+
if (overflow) {
193194
return -2;
194-
if (long_stop==-1 && PyErr_Occurred()) {
195+
}
196+
if (long_stop == -1 && PyErr_Occurred()) {
195197
return -1;
196198
}
197199
long long_step = PyLong_AsLongAndOverflow(step, &overflow);
198-
if (overflow)
200+
if (overflow) {
199201
return -2;
200-
if (long_step==-1 && PyErr_Occurred()) {
202+
}
203+
if (long_step == -1 && PyErr_Occurred()) {
201204
return -1;
202205
}
203-
long len = get_len_of_range(long_start, long_stop, long_step);
204206

205-
if (len<0) {
206-
/* we have an overflow */
207-
len = -2;
207+
unsigned long ulen = get_len_of_range(long_start, long_stop, long_step);
208+
if (ulen > (unsigned long)LONG_MAX) {
209+
/* length too large for a long */
210+
return -2;
211+
}
212+
else {
213+
return (long)ulen;
208214
}
209-
return len;
210215
}
211216

212217
/* Return number of items in range (lo, hi, step) as a PyLong object,
@@ -226,23 +231,23 @@ compute_range_length(PyObject *start, PyObject *stop, PyObject *step)
226231
PyObject *tmp1 = NULL, *tmp2 = NULL, *result;
227232
/* holds sub-expression evaluations */
228233

234+
PyObject *zero = _PyLong_GetZero(); // borrowed reference
235+
PyObject *one = _PyLong_GetOne(); // borrowed reference
236+
229237
assert(PyLong_Check(start));
230238
assert(PyLong_Check(stop));
231239
assert(PyLong_Check(step));
232240

233241
/* fast path when all arguments fit into a long integer */
234242
long len = compute_range_length_long(start, stop, step);
235-
if (len>=0) {
243+
if (len >= 0) {
236244
return PyLong_FromLong(len);
237245
}
238-
else if (len==-1) {
246+
else if (len == -1) {
239247
/* unexpected error from compute_range_length_long, we propagate to the caller */
240248
return NULL;
241249
}
242-
243-
244-
PyObject *zero = _PyLong_GetZero(); // borrowed reference
245-
PyObject *one = _PyLong_GetOne(); // borrowed reference
250+
assert(len == -2);
246251

247252
cmp_result = PyObject_RichCompareBool(step, zero, Py_GT);
248253
if (cmp_result == -1)

0 commit comments

Comments
 (0)