From dce47d5020064fb1fc0c58db165d72c7ffd10835 Mon Sep 17 00:00:00 2001 From: Mark Wooding Date: Sun, 20 Oct 2019 19:24:37 +0100 Subject: [PATCH] pyke/pyke.c: Check conversions hidden inside `KWLIST' and `KWMETH'. Introduce a new macro `CONVERT_CAREFULLY' which checks (via a rather sleazy hack) that its operand is of the expected type before converting it to some other type. This is protected by an `#ifdef' guard because I'm thinking about adding it to a version of mLib, but I want to keep the Pyke core independent of mLib. Use this new macro to build better versions of `KWLIST' and `KWMETH', which have casts hidden inside them, so that we can now be certain that the method table entries match up with the functions. (Spoiler: they didn't, quite, and commit 19bff42f99d41cd9b8953ff61edfe35380b54d88, backported onto the 1.1.x branch, fixes the bug that was found by this change.) --- pyke.h | 43 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/pyke.h b/pyke.h index 4e67464..b91b48d 100644 --- a/pyke.h +++ b/pyke.h @@ -160,7 +160,24 @@ extern PyObject *getulong(unsigned long); /* any kind of unsigned integer */ extern PyObject *abstract_pynew(PyTypeObject *, PyObject *, PyObject *); /* A `tp_new' function which refuses to make the object. */ -#define KWLIST (/*unconst*/ char **)kwlist +#ifndef CONVERT_CAREFULLY +# define CONVERT_CAREFULLY(newty, expty, obj) \ + (!sizeof(*(expty *)0 = (obj)) + (/*unconst*/ newty)(obj)) + /* Convert OBJ to the type NEWTY, having previously checked that it is + * convertible to the expected type EXPTY. + * + * Because of the way we set up types, we can make many kinds of tables be + * `const' which can't usually be so (because Python will want to fiddle + * with their reference counts); and, besides, Python's internals are + * generally quite bad at being `const'-correct about tables. One frequent + * application of this macro, then, is in removing `const' from a type + * without sacrificing all type safety. The other common use is in + * checking that method function types match up with the signatures + * expected in their method definitions. + */ +#endif + +#define KWLIST CONVERT_CAREFULLY(char **, const char *const *, kwlist) /* Strip `const' qualifiers from the keyword list `kwlist'. Useful when * calling `PyArg_ParseTupleAndKeywords', which isn't `const'-correct. */ @@ -209,6 +226,17 @@ extern PyTypeObject *inittype(PyTypeObject */*skel*/, #define INITTYPE(ty, base) INITTYPE_META(ty, base, type) /* Macros to initialize a type from its skeleton. */ +/* Macros for filling in `PyMethodDef' tables, ensuring that functions have + * the expected signatures. + */ +#define STD_METHOD(decor, func, doc) \ + { #func, decor(func), METH_VARARGS, doc }, +#define KEYWORD_METHOD(decor, func, doc) \ + { #func, \ + CONVERT_CAREFULLY(PyCFunction, PyCFunctionWithKeywords, decor(func)), \ + METH_VARARGS | METH_KEYWORDS, \ + doc }, + /* Convenience wrappers for filling in `PyMethodDef' tables, following * Pyke naming convention. Define `METHNAME' locally as * @@ -216,11 +244,8 @@ extern PyTypeObject *inittype(PyTypeObject */*skel*/, * * around the method table. */ -#define METH(func, doc) \ - { #func, METHNAME(func), METH_VARARGS, doc }, -#define KWMETH(func, doc) \ - { #func, (PyCFunction)METHNAME(func), \ - METH_VARARGS | METH_KEYWORDS, doc }, +#define METH(func, doc) STD_METHOD(METHNAME, func, doc) +#define KWMETH(func, doc) KEYWORD_METHOD(METHNAME, func, doc) /* Convenience wrappers for filling in `PyGetSetDef' tables, following Pyke * naming convention. Define `GETSETNAME' locally as @@ -319,9 +344,9 @@ extern PyMethodDef *donemethods(void); /*----- Generic mapping support -------------------------------------------*/ /* Mapping methods. */ -#define GMAP_METH(func, doc) { #func, gmapmeth_##func, METH_VARARGS, doc }, -#define GMAP_KWMETH(func, doc) \ - { #func, (PyCFunction)gmapmeth_##func, METH_VARARGS|METH_KEYWORDS, doc }, +#define GMAP_METMNAME(func) gmapmeth_##func +#define GMAP_METH(func, doc) STD_METHOD(GMAP_METMNAME, func, doc) +#define GMAP_KWMETH(func, doc) KEYWORD_METHOD(GMAP_METMNAME, func, doc) #define GMAP_METHDECL(func, doc) \ extern PyObject *gmapmeth_##func(PyObject *, PyObject *); #define GMAP_KWMETHDECL(func, doc) \ -- 2.11.0