From 7aa690860eceb74332c1edad1b8a7c4956bbdad5 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Thu, 3 Dec 2015 01:02:03 +0200 Subject: Issue #25709: Fixed problem with in-place string concatenation and utf-8 cache. --- Lib/test/test_unicode.py | 17 +++++++++++++++++ Misc/NEWS | 2 ++ Objects/unicodeobject.c | 5 +++++ 3 files changed, 24 insertions(+) diff --git a/Lib/test/test_unicode.py b/Lib/test/test_unicode.py index d54642f..f046938 100644 --- a/Lib/test/test_unicode.py +++ b/Lib/test/test_unicode.py @@ -2672,6 +2672,23 @@ class UnicodeTest(string_tests.CommonTest, self.assertTrue(astral >= bmp2) self.assertFalse(astral >= astral2) + @support.cpython_only + def test_pep393_utf8_caching_bug(self): + # Issue #25709: Problem with string concatenation and utf-8 cache + from _testcapi import getargs_s_hash + for k in 0x24, 0xa4, 0x20ac, 0x1f40d: + s = '' + for i in range(5): + # Due to CPython specific optimization the 's' string can be + # resized in-place. + s += chr(k) + # Parsing with the "s#" format code calls indirectly + # PyUnicode_AsUTF8AndSize() which creates the UTF-8 + # encoded string cached in the Unicode object. + self.assertEqual(getargs_s_hash(s), chr(k).encode() * (i + 1)) + # Check that the second call returns the same result + self.assertEqual(getargs_s_hash(s), chr(k).encode() * (i + 1)) + class StringModuleTest(unittest.TestCase): def test_formatter_parser(self): diff --git a/Misc/NEWS b/Misc/NEWS index 08efa17..076b0b5 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -10,6 +10,8 @@ Release date: tba Core and Builtins ----------------- +- Issue #25709: Fixed problem with in-place string concatenation and utf-8 cache. + - Issue #24097: Fixed crash in object.__reduce__() if slot name is freed inside __getattr__. diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 6fc3683..193d898 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -724,6 +724,11 @@ resize_compact(PyObject *unicode, Py_ssize_t length) } new_size = (struct_size + (length + 1) * char_size); + if (_PyUnicode_HAS_UTF8_MEMORY(unicode)) { + PyObject_DEL(_PyUnicode_UTF8(unicode)); + _PyUnicode_UTF8(unicode) = NULL; + _PyUnicode_UTF8_LENGTH(unicode) = 0; + } _Py_DEC_REFTOTAL; _Py_ForgetReference(unicode); -- cgit v0.12