Skip to content

Commit

Permalink
Make FREE() clear pointer after memory released
Browse files Browse the repository at this point in the history
Since a pointer to allocated memory mustn't be used after the memory is
freed, it is safer to clear the pointer. It also means that if the pointer
is subsequently used, it shoud segfault immediately rather than potentially
trampling over random memory, which might be very difficult to debug.

Signed-off-by: Quentin Armitage <quentin@armitage.org.uk>
  • Loading branch information
pqarmitage committed May 23, 2016
1 parent 8499917 commit 75f3e9e
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ extern void *zalloc(unsigned long size);
#define MALLOC(n) ( keepalived_malloc((n), \
(__FILE__), (char *)(__FUNCTION__), (__LINE__)) )
#define FREE(b) ( keepalived_free((b), \
(__FILE__), (char *)(__FUNCTION__), (__LINE__)) )
(__FILE__), (char *)(__FUNCTION__), (__LINE__)), \
(b) = NULL )
#define REALLOC(b,n) ( keepalived_realloc((b), (n), \
(__FILE__), (char *)(__FUNCTION__), (__LINE__)) )

Expand All @@ -60,12 +61,12 @@ extern void keepalived_free_final(char *);
#else

#define MALLOC(n) (zalloc(n))
#define FREE(p) (free(p))
#define FREE(p) (free(p), (p) = NULL)
#define REALLOC(p,n) (realloc((p),(n)))

#endif

/* Common defines */
#define FREE_PTR(P) if((P)) FREE((P)), (P) = NULL;
#define FREE_PTR(P) if((P)) FREE((P));

#endif

0 comments on commit 75f3e9e

Please sign in to comment.