diff Objects/unicodeobject.c @ 25005:12bb6f0c60f8 legacy-trunk

Fix SF bug 599128, submitted by Inyeol Lee: .replace() would do the wrong thing for a unicode subclass when there were zero string replacements. The example given in the SF bug report was only one way to trigger this; replacing a string of length >= 2 that's not found is another. The code would actually write outside allocated memory if replacement string was longer than the search string. (I wonder how many more of these are lurking? The unicode code base is full of wonders.) Bugfix candidate; this same bug is present in 2.2.1.
author Guido van Rossum <guido@python.org>
date Fri, 23 Aug 2002 18:50:21 +0000
parents 762dd09edb83
children 0337dad8403e
line wrap: on
line diff
--- a/Objects/unicodeobject.c	Fri Aug 23 18:21:28 2002 +0000
+++ b/Objects/unicodeobject.c	Fri Aug 23 18:50:21 2002 +0000
@@ -3534,10 +3534,16 @@
         n = count(self, 0, self->length, str1);
         if (n > maxcount)
             n = maxcount;
-        if (n == 0 && PyUnicode_CheckExact(self)) {
+        if (n == 0) {
             /* nothing to replace, return original string */
-            Py_INCREF(self);
-            u = self;
+            if (PyUnicode_CheckExact(self)) {
+                Py_INCREF(self);
+                u = self;
+            }
+            else {
+                u = (PyUnicodeObject *)
+                    PyUnicode_FromUnicode(self->str, self->length);
+	    }
         } else {
             u = _PyUnicode_New(
                 self->length + n * (str2->length - str1->length));