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

Skip to content 8000

Commit e440459

Browse files
[3.12] gh-127903: Fix a crash on debug builds when calling Objects/unicodeobject::_copy_characters (GH-127876) (#128459)
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 <shadchin@yandex-team.com>
1 parent d9e199b commit e440459

File tree

3 files changed

+15
-3
lines changed

3 files changed

+15
-3
lines changed

Lib/test/test_unicode.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
@@ -1921,6 +1922,12 @@ def test_utf8_decode_invalid_sequences(self):
19211922
self.assertRaises(UnicodeDecodeError,
19221923
(b'\xF4'+cb+b'\xBF\xBF').decode, 'utf-8')
19231924

1925+
def test_issue127903(self):
1926+
# gh-127903: ``_copy_characters`` crashes on DEBUG builds when
1927+
# there is nothing to copy.
1928+
d = datetime.datetime(2013, 11, 10, 14, 20, 59)
1929+
self.assertEqual(d.strftime('%z'), '')
1930+
19241931
def test_issue8271(self):
19251932
# Issue #8271: during the decoding of an invalid UTF-8 byte sequence,
19261933
# 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
@@ -1472,11 +1472,14 @@ _copy_characters(PyObject *to, Py_ssize_t to_start,
14721472
assert(PyUnicode_Check(from));
14731473
assert(from_start + how_many <= PyUnicode_GET_LENGTH(from));
14741474

1475-
assert(PyUnicode_Check(to));
1476-
assert(to_start + how_many <= PyUnicode_GET_LENGTH(to));
1475+
assert(to == NULL || PyUnicode_Check(to));
14771476

1478-
if (how_many == 0)
1477+
if (how_many == 0) {
14791478
return 0;
1479+
}
1480+
1481+
assert(to != NULL);
1482+
assert(to_start + how_many <= PyUnicode_GET_LENGTH(to));
14801483

14811484
from_kind = PyUnicode_KIND(from);
14821485
from_data = PyUnicode_DATA(from);

0 commit comments

Comments
 (0)
0