Skip to content

Commit b875917

Browse files
[3.13] gh-127903: Fix a crash on debug builds when calling Objects/unicodeobject::_copy_characters (GH-127876) (#128458)
gh-127903: Fix a crash on debug builds when calling `Objects/unicodeobject::_copy_characters`` (GH-127876) (cherry picked from commit 46cb634) Co-authored-by: Alexander Shadchin <[email protected]>
1 parent f8b24cd commit b875917

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
@@ -1495,11 +1495,14 @@ _copy_characters(PyObject *to, Py_ssize_t to_start,
14951495
assert(PyUnicode_Check(from));
14961496
assert(from_start + how_many <= PyUnicode_GET_LENGTH(from));
14971497

1498-
assert(PyUnicode_Check(to));
1499-
assert(to_start + how_many <= PyUnicode_GET_LENGTH(to));
1498+
assert(to == NULL || PyUnicode_Check(to));
15001499

1501-
if (how_many == 0)
1500+
if (how_many == 0) {
15021501
return 0;
1502+
}
1503+
1504+
assert(to != NULL);
1505+
assert(to_start + how_many <= PyUnicode_GET_LENGTH(to));
15031506

15041507
from_kind = PyUnicode_KIND(from);
15051508
from_data = PyUnicode_DATA(from);

0 commit comments

Comments
 (0)