refactor: introduce XFREE_CLEAR()

Unfortunately we cannot indiscriminately replace xfree() with
XFREE_CLEAR(), because comparing pointers after freeing them is a common
pattern. Example in `tv_list_remove_items()`:

    xfree(li);
    if (li == item2) {
      break;
    }

Instead we can do it selectively/explicitly.

ref #1375
This commit is contained in:
Justin M. Keyes
2019-05-22 00:10:35 +02:00
parent 4769deb36a
commit a9d7ec4587
8 changed files with 33 additions and 20 deletions

View File

@@ -40,4 +40,15 @@ extern bool entered_free_all_mem;
#ifdef INCLUDE_GENERATED_DECLARATIONS
# include "memory.h.generated.h"
#endif
#define XFREE_CLEAR(ptr) \
do { \
/* Take the address to avoid double evaluation. #1375 */ \
void **ptr_ = (void **)&(ptr); \
xfree(*ptr_); \
/* coverity[dead-store] */ \
*ptr_ = NULL; \
(void)(*ptr_); \
} while (0)
#endif // NVIM_MEMORY_H