Skip to content

Commit 41525e3

Browse files
Issue #23466: Raised OverflowError if %c argument is out of range.
1 parent 45ec328 commit 41525e3

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

Lib/test/test_format.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -358,12 +358,12 @@ def test_exc(formatstr, args, exception, excmsg):
358358
"not all arguments converted during bytes formatting")
359359
test_exc(b'no format', bytearray(b'1'), TypeError,
360360
"not all arguments converted during bytes formatting")
361-
test_exc(b"%c", -1, TypeError,
362-
"%c requires an integer in range(256) or a single byte")
363-
test_exc(b"%c", 256, TypeError,
364-
"%c requires an integer in range(256) or a single byte")
365-
test_exc(b"%c", 2**128, TypeError,
366-
"%c requires an integer in range(256) or a single byte")
361+
test_exc(b"%c", -1, OverflowError,
362+
"%c arg not in range(256)")
363+
test_exc(b"%c", 256, OverflowError,
364+
"%c arg not in range(256)")
365+
test_exc(b"%c", 2**128, OverflowError,
366+
"%c arg not in range(256)")
367367
test_exc(b"%c", b"Za", TypeError,
368368
"%c requires an integer in range(256) or a single byte")
369369
test_exc(b"%c", "Y", TypeError,

Objects/bytesobject.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -496,10 +496,15 @@ byte_converter(PyObject *arg, char *p)
496496
ival = PyLong_AsLongAndOverflow(iobj, &overflow);
497497
Py_DECREF(iobj);
498498
}
499-
if (!overflow && 0 <= ival && ival <= 255) {
500-
*p = (char)ival;
501-
return 1;
499+
if (!overflow && ival == -1 && PyErr_Occurred())
500+
goto onError;
501+
if (overflow || !(0 <= ival && ival <= 255)) {
502+
PyErr_SetString(PyExc_OverflowError,
503+
"%c arg not in range(256)");
504+
return 0;
502505
}
506+
*p = (char)ival;
507+
return 1;
503508
}
504509
onError:
505510
PyErr_SetString(PyExc_TypeError,

0 commit comments

Comments
 (0)