From c56b5256ff30f59955362ab536c6f089dd86f9fd Mon Sep 17 00:00:00 2001 From: Alexander Shadchin Date: Thu, 12 Dec 2024 19:39:27 +0300 Subject: [PATCH 1/7] Fix segmentation fault --- Objects/unicodeobject.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 33c4747bbef488..e043015b49d510 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -1429,12 +1429,12 @@ _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)); - if (how_many == 0) return 0; + assert(PyUnicode_Check(to)); + assert(to_start + how_many <= PyUnicode_GET_LENGTH(to)); + from_kind = PyUnicode_KIND(from); from_data = PyUnicode_DATA(from); to_kind = PyUnicode_KIND(to); From af5cedb25c4eeb7c1917707a4d6bc1892a6d921a Mon Sep 17 00:00:00 2001 From: Alexander Shadchin Date: Fri, 13 Dec 2024 14:18:27 +0300 Subject: [PATCH 2/7] add news --- .../2024-12-13-14-17-24.gh-issue-127903.vemHSl.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2024-12-13-14-17-24.gh-issue-127903.vemHSl.rst 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..287b333bdc1c16 --- /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 @@ +Fix segmentation fault in debug mode in ``_copy_characters`` when there is +nothing to copy. From 8a325480aac43305f2003f2a58bd75cd9deb8c85 Mon Sep 17 00:00:00 2001 From: Alexander Shadchin Date: Sat, 14 Dec 2024 09:41:17 +0300 Subject: [PATCH 3/7] add test --- Lib/test/test_str.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Lib/test/test_str.py b/Lib/test/test_str.py index 4de6c1cba152bd..640db121839fef 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,11 @@ def test_utf8_decode_invalid_sequences(self): self.assertRaises(UnicodeDecodeError, (b'\xF4'+cb+b'\xBF\xBF').decode, 'utf-8') + def test_issue127903(self): + # Issue #127903: segmentation fault in debug mode in ``_copy_characters`` + # when there is nothing to copy. + self.assertEqual(datetime.datetime(2013, 11, 10, 14, 20, 59).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 From 02043a8734e302cda10630902a654511391426e0 Mon Sep 17 00:00:00 2001 From: Alexander Shadchin Date: Tue, 17 Dec 2024 12:58:06 +0300 Subject: [PATCH 4/7] fix comments --- Lib/test/test_str.py | 7 ++++--- .../2024-12-13-14-17-24.gh-issue-127903.vemHSl.rst | 4 ++-- Objects/unicodeobject.c | 6 +++--- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/Lib/test/test_str.py b/Lib/test/test_str.py index 640db121839fef..670849a28113d0 100644 --- a/Lib/test/test_str.py +++ b/Lib/test/test_str.py @@ -1910,9 +1910,10 @@ def test_utf8_decode_invalid_sequences(self): (b'\xF4'+cb+b'\xBF\xBF').decode, 'utf-8') def test_issue127903(self): - # Issue #127903: segmentation fault in debug mode in ``_copy_characters`` - # when there is nothing to copy. - self.assertEqual(datetime.datetime(2013, 11, 10, 14, 20, 59).strftime('%z'), '') + # Issue #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, 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 index 287b333bdc1c16..ad479b52d1675c 100644 --- 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 @@ -1,2 +1,2 @@ -Fix segmentation fault in debug mode in ``_copy_characters`` when there is -nothing to copy. +``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 e043015b49d510..a2590f70693935 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -1429,12 +1429,12 @@ _copy_characters(PyObject *to, Py_ssize_t to_start, assert(PyUnicode_Check(from)); assert(from_start + how_many <= PyUnicode_GET_LENGTH(from)); + assert((to == NULL && how_many == 0) || PyUnicode_Check(to)); + assert((to == NULL && how_many == 0) || (to_start + how_many <= PyUnicode_GET_LENGTH(to))); + if (how_many == 0) return 0; - assert(PyUnicode_Check(to)); - assert(to_start + how_many <= PyUnicode_GET_LENGTH(to)); - from_kind = PyUnicode_KIND(from); from_data = PyUnicode_DATA(from); to_kind = PyUnicode_KIND(to); From ad692fb9500e7f6ee5792c2bbbf372bc47579bd4 Mon Sep 17 00:00:00 2001 From: Alexander Shadchin Date: Tue, 17 Dec 2024 13:35:24 +0300 Subject: [PATCH 5/7] fix assert --- Objects/unicodeobject.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index a2590f70693935..c9cbe6d25c7a91 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -1429,8 +1429,8 @@ _copy_characters(PyObject *to, Py_ssize_t to_start, assert(PyUnicode_Check(from)); assert(from_start + how_many <= PyUnicode_GET_LENGTH(from)); - assert((to == NULL && how_many == 0) || PyUnicode_Check(to)); - assert((to == NULL && how_many == 0) || (to_start + how_many <= PyUnicode_GET_LENGTH(to))); + assert(to == NULL || PyUnicode_Check(to)); + assert(how_many == 0 || (to_start + how_many <= PyUnicode_GET_LENGTH(to))); if (how_many == 0) return 0; From 70ca11ab60ffe0639ffddb8023a5a14dbda86594 Mon Sep 17 00:00:00 2001 From: Alexander Shadchin Date: Fri, 3 Jan 2025 20:26:37 +0300 Subject: [PATCH 6/7] fix comment --- Objects/unicodeobject.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index c9cbe6d25c7a91..c96a15ea5d7bf8 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -1430,10 +1430,13 @@ _copy_characters(PyObject *to, Py_ssize_t to_start, assert(from_start + how_many <= PyUnicode_GET_LENGTH(from)); assert(to == NULL || PyUnicode_Check(to)); - assert(how_many == 0 || (to_start + how_many <= PyUnicode_GET_LENGTH(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); From 6372a200af5522d5950adc7259b9f471c3eadcd3 Mon Sep 17 00:00:00 2001 From: Kirill Podoprigora Date: Fri, 3 Jan 2025 20:05:42 +0200 Subject: [PATCH 7/7] Update Lib/test/test_str.py --- Lib/test/test_str.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/test/test_str.py b/Lib/test/test_str.py index 670849a28113d0..d1c9542c7d1317 100644 --- a/Lib/test/test_str.py +++ b/Lib/test/test_str.py @@ -1910,7 +1910,7 @@ def test_utf8_decode_invalid_sequences(self): (b'\xF4'+cb+b'\xBF\xBF').decode, 'utf-8') def test_issue127903(self): - # Issue #127903: ``_copy_characters`` crashes on DEBUG builds when + # 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'), '')