[Python-checkins] bpo-39943: Fix MSVC warnings in sre extension (GH-20508)

miss-islington webhook-mailer at python.org
Sun Mar 14 08:17:40 EDT 2021


https://github.com/python/cpython/commit/cf8d6ef9629dc1bffadfcec251a2ffe30d5addaa
commit: cf8d6ef9629dc1bffadfcec251a2ffe30d5addaa
branch: 3.9
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: miss-islington <31488909+miss-islington at users.noreply.github.com>
date: 2021-03-14T05:17:26-07:00
summary:

bpo-39943: Fix MSVC warnings in sre extension (GH-20508)

(cherry picked from commit 06e3a27a3c863495390a07c695171a8e62a6e0d2)

Co-authored-by: Ammar Askar <ammar at ammaraskar.com>

files:
M Modules/_sre.c
M Modules/sre_lib.h

diff --git a/Modules/_sre.c b/Modules/_sre.c
index 244e4f1f84dff..bdc427822d7e1 100644
--- a/Modules/_sre.c
+++ b/Modules/_sre.c
@@ -454,7 +454,10 @@ state_init(SRE_STATE* state, PatternObject* pattern, PyObject* string,
 
     return string;
   err:
-    PyMem_Del(state->mark);
+    /* We add an explicit cast here because MSVC has a bug when
+       compiling C code where it believes that `const void**` cannot be
+       safely casted to `void*`, see bpo-39943 for details. */
+    PyMem_Del((void*) state->mark);
     state->mark = NULL;
     if (state->buffer.buf)
         PyBuffer_Release(&state->buffer);
@@ -468,7 +471,8 @@ state_fini(SRE_STATE* state)
         PyBuffer_Release(&state->buffer);
     Py_XDECREF(state->string);
     data_stack_dealloc(state);
-    PyMem_Del(state->mark);
+    /* See above PyMem_Del for why we explicitly cast here. */
+    PyMem_Del((void*) state->mark);
     state->mark = NULL;
 }
 
diff --git a/Modules/sre_lib.h b/Modules/sre_lib.h
index 9cc786321c560..2657d8d82c6f1 100644
--- a/Modules/sre_lib.h
+++ b/Modules/sre_lib.h
@@ -448,12 +448,15 @@ do { \
     state->data_stack_base += size; \
 } while (0)
 
+/* We add an explicit cast to memcpy here because MSVC has a bug when
+   compiling C code where it believes that `const void**` cannot be
+   safely casted to `void*`, see bpo-39943 for details. */
 #define DATA_STACK_POP(state, data, size, discard) \
 do { \
     TRACE(("copy data to %p from %" PY_FORMAT_SIZE_T "d " \
            "(%" PY_FORMAT_SIZE_T "d)\n", \
            data, state->data_stack_base-size, size)); \
-    memcpy(data, state->data_stack+state->data_stack_base-size, size); \
+    memcpy((void*) data, state->data_stack+state->data_stack_base-size, size); \
     if (discard) \
         state->data_stack_base -= size; \
 } while (0)



More information about the Python-checkins mailing list