[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 46cb6340d7)

Co-authored-by: Alexander Shadchin <shadchin@yandex-team.com>
This commit is contained in:
Miss Islington (bot) 2025-01-03 20:20:30 +01:00 committed by GitHub
parent f8b24cdbb9
commit b875917e21
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 15 additions and 3 deletions

View file

@ -7,6 +7,7 @@
"""
import _string
import codecs
import datetime
import itertools
import operator
import pickle
@ -1908,6 +1909,12 @@ def test_utf8_decode_invalid_sequences(self):
self.assertRaises(UnicodeDecodeError,
(b'\xF4'+cb+b'\xBF\xBF').decode, 'utf-8')
def test_issue127903(self):
# gh-127903: ``_copy_characters`` crashes on DEBUG builds when
# there is nothing to copy.
d = datetime.datetime(2013, 11, 10, 14, 20, 59)
self.assertEqual(d.strftime('%z'), '')
def test_issue8271(self):
# Issue #8271: during the decoding of an invalid UTF-8 byte sequence,
# only the start byte and the continuation byte(s) are now considered

View file

@ -0,0 +1,2 @@
``Objects/unicodeobject.c``: fix a crash on DEBUG builds in ``_copy_characters``
when there is nothing to copy.

View file

@ -1495,11 +1495,14 @@ _copy_characters(PyObject *to, Py_ssize_t to_start,
assert(PyUnicode_Check(from));
assert(from_start + how_many <= PyUnicode_GET_LENGTH(from));
assert(PyUnicode_Check(to));
assert(to_start + how_many <= PyUnicode_GET_LENGTH(to));
assert(to == NULL || PyUnicode_Check(to));
if (how_many == 0)
if (how_many == 0) {
return 0;
}
assert(to != NULL);
assert(to_start + how_many <= PyUnicode_GET_LENGTH(to));
from_kind = PyUnicode_KIND(from);
from_data = PyUnicode_DATA(from);