Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 4cd63ef

Browse files
Issue #26198: ValueError is now raised instead of TypeError on buffer
overflow in parsing "es#" and "et#" format units. SystemError is now raised instead of TypeError on programmical error in parsing format string.
1 parent 3e17c78 commit 4cd63ef

4 files changed

Lines changed: 20 additions & 8 deletions

File tree

Lib/test/test_capi.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -488,10 +488,10 @@ def test_skipitem(self):
488488
_testcapi.parse_tuple_and_keywords(tuple_1, dict_b,
489489
format.encode("ascii"), keywords)
490490
when_not_skipped = False
491-
except TypeError as e:
491+
except SystemError as e:
492492
s = "argument 1 (impossible<bad format char>)"
493493
when_not_skipped = (str(e) == s)
494-
except RuntimeError as e:
494+
except (TypeError, RuntimeError):
495495
when_not_skipped = False
496496

497497
# test the format unit when skipped

Lib/test/test_getargs2.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -636,10 +636,10 @@ def test_es_hash(self):
636636
self.assertEqual(getargs_es_hash('abc\xe9', 'latin1', buf), b'abc\xe9')
637637
self.assertEqual(buf, bytearray(b'abc\xe9\x00'))
638638
buf = bytearray(b'x'*4)
639-
self.assertRaises(TypeError, getargs_es_hash, 'abc\xe9', 'latin1', buf)
639+
self.assertRaises(ValueError, getargs_es_hash, 'abc\xe9', 'latin1', buf)
640640
self.assertEqual(buf, bytearray(b'x'*4))
641641
buf = bytearray()
642-
self.assertRaises(TypeError, getargs_es_hash, 'abc\xe9', 'latin1', buf)
642+
self.assertRaises(ValueError, getargs_es_hash, 'abc\xe9', 'latin1', buf)
643643

644644
def test_et_hash(self):
645645
from _testcapi import getargs_et_hash
@@ -662,10 +662,10 @@ def test_et_hash(self):
662662
self.assertEqual(getargs_et_hash('abc\xe9', 'latin1', buf), b'abc\xe9')
663663
self.assertEqual(buf, bytearray(b'abc\xe9\x00'))
664664
buf = bytearray(b'x'*4)
665-
self.assertRaises(TypeError, getargs_et_hash, 'abc\xe9', 'latin1', buf)
665+
self.assertRaises(ValueError, getargs_et_hash, 'abc\xe9', 'latin1', buf)
666666
self.assertEqual(buf, bytearray(b'x'*4))
667667
buf = bytearray()
668-
self.assertRaises(TypeError, getargs_et_hash, 'abc\xe9', 'latin1', buf)
668+
self.assertRaises(ValueError, getargs_et_hash, 'abc\xe9', 'latin1', buf)
669669

670670
def test_u(self):
671671
from _testcapi import getargs_u

Misc/NEWS

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,13 @@ Tools/Demos
713713
- Issue #25154: The pyvenv script has been deprecated in favour of
714714
`python3 -m venv`.
715715

716+
C API
717+
-----
718+
719+
- Issue #26198: ValueError is now raised instead of TypeError on buffer
720+
overflow in parsing "es#" and "et#" format units. SystemError is now raised
721+
instead of TypeError on programmical error in parsing format string.
722+
716723

717724
What's New in Python 3.5.1 final?
718725
=================================

Python/getargs.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,12 @@ seterror(Py_ssize_t iarg, const char *msg, int *levels, const char *fname,
394394
PyOS_snprintf(p, sizeof(buf) - (p - buf), " %.256s", msg);
395395
message = buf;
396396
}
397-
PyErr_SetString(PyExc_TypeError, message);
397+
if (msg[0] == '(') {
398+
PyErr_SetString(PyExc_SystemError, message);
399+
}
400+
else {
401+
PyErr_SetString(PyExc_TypeError, message);
402+
}
398403
}
399404

400405

@@ -1129,7 +1134,7 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
11291134
} else {
11301135
if (size + 1 > BUFFER_LEN) {
11311136
Py_DECREF(s);
1132-
PyErr_Format(PyExc_TypeError,
1137+
PyErr_Format(PyExc_ValueError,
11331138
"encoded string too long "
11341139
"(%zd, maximum length %zd)",
11351140
(Py_ssize_t)size, (Py_ssize_t)(BUFFER_LEN-1));

0 commit comments

Comments
 (0)