From 58563f46c53d4677f8db57bb0689614834fdc43b Mon Sep 17 00:00:00 2001 From: Vito Caputo Date: Wed, 18 Nov 2020 21:40:58 -0800 Subject: thunk: clear thunk instance payloads Since thunk instances are utilized via initializer functions which always initialize all the environment members, it made sense to use plain malloc(). But with the addition of payloads, there's a component of the allocation which gets split out for the caller to make use of in a much more ad-hoc fashion. In my personal programming style, I tend to take advantage of the assumption that allocations are zeroed. The returned payload is essentially used as a heap allocation, just coupled to the thunk instance in terms of life cycle. So I prefer the payload space be zeroed on allocation. Since it still seems sensible to leave the thunk's environment part of the instance exclusively initialized by it's init function, this commit leaves the plain malloc and just memset's the payload when present. --- thunk.h | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/thunk.h b/thunk.h index 7fbdfbd..262137f 100644 --- a/thunk.h +++ b/thunk.h @@ -21,6 +21,7 @@ #include #include +#include typedef struct thunk_t thunk_t; @@ -488,8 +489,10 @@ struct thunk_t { env = malloc(sizeof(*env) + payload_size); \ assert(env); \ \ - if (payload_ptr) \ + if (payload_ptr) { \ + memset(env->__payload, 0, payload_size); \ *payload_ptr = env->__payload; \ + } \ \ env->__thunk.dispatch = __thunk_dispatch_##_name; \ \ @@ -566,8 +569,10 @@ struct thunk_t { env = malloc(sizeof(*env) + payload_size); \ assert(env); \ \ - if (payload_ptr) \ + if (payload_ptr) { \ + memset(env->__payload, 0, payload_size); \ *payload_ptr = env->__payload; \ + } \ \ env->__thunk.dispatch = __thunk_dispatch_##_name; \ \ -- cgit v1.2.3