include/linux/slab.h: new KFREE() macro.
PATCH 1/1 include/linux/slab.h: new KFREE() macro.
Description: new KFREE() macro to set the variable NULL after freeing it.
include/linux/slab.h +6 -0
@@ -75,6 +75,12 @@ void *__kzalloc(size_t, gfp_t);void kfree (const void *);unsigned int ksize (const void *); +#define KFREE (x) \ + do { \ + kfree(x); \ + x = NULL; \ + } while(0) +/** * kcalloc - allocate memory for an array. The memory is set to zero. * @n: number of elements.
include/linux/slab.h void *__kzalloc(size_t, gfp_t); · line 82
Quoted 5 lines
include/linux/slab.h +5
-0+#define KFREE (x) \ + do { \ + kfree(x); \ + x = NULL; \ + } while(0)
This doesn't work correctly if "x" has side effects -- double evaluation. Use a temporary variable instead, or better, an inline function. A better name wouldn't hurt either (kfree_and_zero()?)
Segher
include/linux/slab.h void *__kzalloc(size_t, gfp_t); · line 82
Quoted 5 lines · Amit Choudhary
include/linux/slab.h +5
-0+#define KFREE (x) \ + do { \ + kfree(x); \ + x = NULL; \ + } while(0)
NAK until you have actual callers for it. CONFIG_SLAB_DEBUG already catches use after free and double-free so I don't see the point of this.
Quoted 3 lines · Pekka Enberg
NAK until you have actual callers for it.
CONFIG_SLAB_DEBUGalready catches use after free and double-free so I don't see the point of this.
And CONFIG_SLAB_DEBUG actually finds them for real using poisoning, unlike setting the pointer to NULL which is too magic to catch most bugs but rather papers over them.