8000 GH-127903: Fix a crash on DEBUG builds when calling `_copy_characters` by shadchin · Pull Request #127876 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

GH-127903: Fix a crash on DEBUG builds when calling _copy_characters #127876

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Lib/test/test_str.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"""
import _string
import codecs
import datetime
import itertools
import operator
import pickle
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
``Objects/unicodeobject.c``: fix a crash on DEBUG builds in ``_copy_characters``
when there is nothing to copy.
9 changes: 6 additions & 3 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -1429,11 +1429,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);
Expand Down
Loading
0