From 9d4f51e1ff84af82157a3f2b07741ef1cb0a813e Mon Sep 17 00:00:00 2001 From: antirez Date: Tue, 2 Oct 2018 19:36:33 +0200 Subject: [PATCH] Listpack: optionally force reallocation on inserts. This is useful in order to spot bugs where we fail at updating the pointer returned by the insertion function. Normally often the same pointer is returned, making it harder than needed to spot bugs. Related to #5210. --- src/listpack.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/listpack.c b/src/listpack.c index c3070db6..e1f4d9a0 100644 --- a/src/listpack.c +++ b/src/listpack.c @@ -707,6 +707,26 @@ unsigned char *lpInsert(unsigned char *lp, unsigned char *ele, uint32_t size, un } } lpSetTotalBytes(lp,new_listpack_bytes); + +#if 0 + /* This code path is normally disabled: what it does is to force listpack + * to return *always* a new pointer after performing some modification to + * the listpack, even if the previous allocation was enough. This is useful + * in order to spot bugs in code using listpacks: by doing so we can find + * if the caller forgets to set the new pointer where the listpack reference + * is stored, after an update. */ + unsigned char *oldlp = lp; + lp = lp_malloc(new_listpack_bytes); + memcpy(lp,oldlp,new_listpack_bytes); + if (newp) { + unsigned long offset = (*newp)-oldlp; + *newp = lp + offset; + } + /* Make sure the old allocation contains garbage. */ + memset(oldlp,'A',new_listpack_bytes); + lp_free(oldlp); +#endif + return lp; }