Ensure usage of __attribute__(cleanup) never frees un-initialized memory

Use macros that take an initializer parameter to, thereby ensuring the
variable to be cleaned up is always initialized.
This commit is contained in:
Kovid Goyal
2023-08-07 11:52:52 +05:30
parent 9af4d5e0fc
commit 6c7a8f8fa9
19 changed files with 75 additions and 77 deletions

View File

@@ -46,10 +46,10 @@
void log_error(const char *fmt, ...) __attribute__ ((format (printf, 1, 2)));
#define fatal(...) { log_error(__VA_ARGS__); exit(EXIT_FAILURE); }
static inline void cleanup_free(void *p) { free(*(void**)p); }
#define FREE_AFTER_FUNCTION __attribute__((cleanup(cleanup_free)))
#define RAII_ALLOC(type, name, initializer) __attribute__((cleanup(cleanup_free))) type *name = initializer
static inline void cleanup_decref(PyObject **p) { if(*p) { Py_DECREF(*p); *p = NULL;} }
#define DECREF_AFTER_FUNCTION __attribute__((cleanup(cleanup_decref)))
#define FREE_BUFFER_AFTER_FUNCTION __attribute__((cleanup(PyBuffer_Release)))
#define RAII_PyObject(name, initializer) __attribute__((cleanup(cleanup_decref))) PyObject *name = initializer
#define RAII_PY_BUFFER(name) __attribute__((cleanup(PyBuffer_Release))) Py_buffer name = {0}
typedef unsigned long long id_type;
typedef uint32_t char_type;