]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-127903: Fix a crash on debug builds when calling `Objects/unicodeobject...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Fri, 3 Jan 2025 19:21:08 +0000 (20:21 +0100)
committerGitHub <noreply@github.com>
Fri, 3 Jan 2025 19:21:08 +0000 (21:21 +0200)
gh-127903: Fix a crash on debug builds when calling `Objects/unicodeobject::_copy_characters`` (GH-127876)
(cherry picked from commit 46cb6340d7bad955edfc0a20f6a52dabc03b0932)

Co-authored-by: Alexander Shadchin <shadchin@yandex-team.com>
Lib/test/test_unicode.py
Misc/NEWS.d/next/Core_and_Builtins/2024-12-13-14-17-24.gh-issue-127903.vemHSl.rst [new file with mode: 0644]
Objects/unicodeobject.c

index 19556eecc3c6464c7ef2bae84c8cf08fae0e9563..a24acb7af2ff96f32a55290377b8ea7b51734aa6 100644 (file)
@@ -7,6 +7,7 @@ Written by Marc-Andre Lemburg (mal@lemburg.com).
 """
 import _string
 import codecs
+import datetime
 import itertools
 import operator
 import pickle
@@ -1921,6 +1922,12 @@ class UnicodeTest(string_tests.CommonTest,
             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 (file)
index 0000000..ad479b5
--- /dev/null
@@ -0,0 +1,2 @@
+``Objects/unicodeobject.c``: fix a crash on DEBUG builds in ``_copy_characters``
+when there is nothing to copy.
index c885be5b6682aaace4324b04a4defe57be729e95..3e5b5d03241ae001ba2d104dbf97b904c7be9f37 100644 (file)
@@ -1472,11 +1472,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);