8000 gh-127903: Fix a crash on debug builds when calling `Objects/unicodeo… · python/cpython@46cb634 · GitHub
[go: up one dir, main page]

Skip to content

Commit 46cb634

Browse files
authored
gh-127903: Fix a crash on debug builds when calling Objects/unicodeobject::_copy_characters` (#127876)
1 parent 4c14f03 commit 46cb634

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

Lib/test/test_str.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"""
88
import _string
99
import codecs
10+
import datetime
1011
import itertools
1112
import operator
1213
import pickle
@@ -1908,6 +1909,12 @@ def test_utf8_decode_invalid_sequences(self):
19081909
self.assertRaises(UnicodeDecodeError,
19091910
(b'\xF4'+cb+b'\xBF\xBF').decode, 'utf-8')
19101911

1912+
def test_issue127903(self):
1913+
# gh-127903: ``_copy_characters`` crashes on DEBUG builds when
1914+
# there is nothing to copy.
1915+
d = datetime.datetime(2013, 11, 10, 14, 20, 59)
1916+
self.assertEqual(d.strftime('%z'), '')
1917+
19111918
def test_issue8271(self):
19121919
# Issue #8271: during the decoding of an invalid UTF-8 byte sequence,
19131920
# only the start byte and the continuation byte(s) are now considered
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
``Objects/unicodeobject.c``: fix a crash on DEBUG builds in ``_copy_characters``
2+
when there is nothing to copy.

Objects/unicodeobject.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1463,11 +1463,14 @@ _copy_characters(PyObject *to, Py_ssize_t to_start,
14631463
assert(PyUnicode_Check(from));
14641464
assert(from_start + how_many <= PyUnicode_GET_LENGTH(from));
14651465

1466-
assert(PyUnicode_Check(to));
1467-
assert(to_start + how_many <= PyUnicode_GET_LENGTH(to));
1466+
assert(to == NULL || PyUnicode_Check(to));
14681467

1469-
if (how_many == 0)
1468+
if (how_many == 0) {
14701469
return 0;
1470+
}
1471+
1472+
assert(to != NULL);
1473+
assert(to_start + how_many <= PyUnicode_GET_LENGTH(to));
14711474

14721475
from_kind = PyUnicode_KIND(from);
14731476
from_data = PyUnicode_DATA(from);

0 commit comments

Comments
 (0)
0