[Python-checkins] cpython: PyUnicode_Substring() now accepts end bigger than string length

victor.stinner python-checkins at python.org
Sat Oct 1 04:02:21 CEST 2011


http://hg.python.org/cpython/rev/45f1de829d70
changeset:   72560:45f1de829d70
user:        Victor Stinner <victor.stinner at haypocalc.com>
date:        Sat Oct 01 03:55:54 2011 +0200
summary:
  PyUnicode_Substring() now accepts end bigger than string length

Fix also a bug: call PyUnicode_READY() before reading string length.

files:
  Objects/unicodeobject.c |  9 ++++++---
  1 files changed, 6 insertions(+), 3 deletions(-)


diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -10447,6 +10447,11 @@
     int kind;
     Py_ssize_t length;
 
+    if (PyUnicode_READY(self) == -1)
+        return NULL;
+
+    end = Py_MIN(end, PyUnicode_GET_LENGTH(self));
+
     if (start == 0 && end == PyUnicode_GET_LENGTH(self))
     {
         if (PyUnicode_CheckExact(self)) {
@@ -10461,13 +10466,11 @@
     if (length == 1)
         return unicode_getitem((PyUnicodeObject*)self, start);
 
-    if (start < 0 || end < 0 || end > PyUnicode_GET_LENGTH(self)) {
+    if (start < 0 || end < 0) {
         PyErr_SetString(PyExc_IndexError, "string index out of range");
         return NULL;
     }
 
-    if (PyUnicode_READY(self) == -1)
-        return NULL;
     kind = PyUnicode_KIND(self);
     data = PyUnicode_1BYTE_DATA(self);
     return PyUnicode_FromKindAndData(kind,

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list