[Python-checkins] bpo-33954: Rewrite FILL() macro of unicodeobject.c (GH-10738)

Victor Stinner webhook-mailer at python.org
Tue Nov 27 06:42:09 EST 2018


https://github.com/python/cpython/commit/7f9fb0f34555641722be5468b7fbd53dd3c0f1e2
commit: 7f9fb0f34555641722be5468b7fbd53dd3c0f1e2
branch: 3.7
author: Victor Stinner <vstinner at redhat.com>
committer: GitHub <noreply at github.com>
date: 2018-11-27T12:42:04+01:00
summary:

bpo-33954: Rewrite FILL() macro of unicodeobject.c (GH-10738)

Copy code from master: add assertions on start and value, replace 'i'
iterator with 'end' pointer for the loop stop condition.

_PyUnicode_FastFill(): fix type of 'data', it must not be constant,
since data is modified by FILL().

files:
M Objects/unicodeobject.c

diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index 8dd7c3b8258c..e6371d2337c3 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -220,22 +220,30 @@ static PyObject *unicode_empty = NULL;
 
 #define FILL(kind, data, value, start, length) \
     do { \
-        Py_ssize_t i_ = 0; \
+        assert(0 <= start); \
         assert(kind != PyUnicode_WCHAR_KIND); \
-        switch ((kind)) { \
+        switch (kind) { \
         case PyUnicode_1BYTE_KIND: { \
-            unsigned char * to_ = (unsigned char *)((data)) + (start); \
-            memset(to_, (unsigned char)value, (length)); \
+            assert(value <= 0xff); \
+            Py_UCS1 ch = (unsigned char)value; \
+            Py_UCS1 *to = (Py_UCS1 *)data + start; \
+            memset(to, ch, length); \
             break; \
         } \
         case PyUnicode_2BYTE_KIND: { \
-            Py_UCS2 * to_ = (Py_UCS2 *)((data)) + (start); \
-            for (; i_ < (length); ++i_, ++to_) *to_ = (value); \
+            assert(value <= 0xffff); \
+            Py_UCS2 ch = (Py_UCS2)value; \
+            Py_UCS2 *to = (Py_UCS2 *)data + start; \
+            const Py_UCS2 *end = to + length; \
+            for (; to < end; ++to) *to = ch; \
             break; \
         } \
         case PyUnicode_4BYTE_KIND: { \
-            Py_UCS4 * to_ = (Py_UCS4 *)((data)) + (start); \
-            for (; i_ < (length); ++i_, ++to_) *to_ = (value); \
+            assert(value <= MAX_UNICODE); \
+            Py_UCS4 ch = value; \
+            Py_UCS4 * to = (Py_UCS4 *)data + start; \
+            const Py_UCS4 *end = to + length; \
+            for (; to < end; ++to) *to = ch; \
             break; \
         } \
         default: Py_UNREACHABLE(); \
@@ -10079,7 +10087,7 @@ _PyUnicode_FastFill(PyObject *unicode, Py_ssize_t start, Py_ssize_t length,
                     Py_UCS4 fill_char)
 {
     const enum PyUnicode_Kind kind = PyUnicode_KIND(unicode);
-    const void *data = PyUnicode_DATA(unicode);
+    void *data = PyUnicode_DATA(unicode);
     assert(PyUnicode_IS_READY(unicode));
     assert(unicode_modifiable(unicode));
     assert(fill_char <= PyUnicode_MAX_CHAR_VALUE(unicode));



More information about the Python-checkins mailing list