https://github.com/python/cpython/commit/46cb6340d7bad955edfc0a20f6a52dabc03b0932
commit: 46cb6340d7bad955edfc0a20f6a52dabc03b0932
branch: main
author: Alexander Shadchin <[email protected]>
committer: Eclips4 <[email protected]>
date: 2025-01-03T18:47:58Z
summary:

gh-127903: Fix a crash on debug builds when calling 
`Objects/unicodeobject::_copy_characters`` (#127876)

files:
A 
Misc/NEWS.d/next/Core_and_Builtins/2024-12-13-14-17-24.gh-issue-127903.vemHSl.rst
M Lib/test/test_str.py
M Objects/unicodeobject.c

diff --git a/Lib/test/test_str.py b/Lib/test/test_str.py
index 4de6c1cba152bd..d1c9542c7d1317 100644
--- a/Lib/test/test_str.py
+++ b/Lib/test/test_str.py
@@ -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
diff --git 
a/Misc/NEWS.d/next/Core_and_Builtins/2024-12-13-14-17-24.gh-issue-127903.vemHSl.rst
 
b/Misc/NEWS.d/next/Core_and_Builtins/2024-12-13-14-17-24.gh-issue-127903.vemHSl.rst
new file mode 100644
index 00000000000000..ad479b52d1675c
--- /dev/null
+++ 
b/Misc/NEWS.d/next/Core_and_Builtins/2024-12-13-14-17-24.gh-issue-127903.vemHSl.rst
@@ -0,0 +1,2 @@
+``Objects/unicodeobject.c``: fix a crash on DEBUG builds in 
``_copy_characters``
+when there is nothing to copy.
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index 9f0a4d4785eda6..5e532ce0f348c4 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -1463,11 +1463,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);

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: [email protected]

Reply via email to