Null pointers and error paths
Null pointers and error paths
Posted Jun 10, 2024 9:51 UTC (Mon) by paulj (subscriber, #341)In reply to: Null pointers and error paths by Wol
Parent article: Removing GFP_NOFS
What I do is:
1. At the lower levels that need to deal directly with *alloc and free(), I have a wrapper around free() (possibly a macro) which takes a double-pointer to the caller's pointer. It can then null out the caller's pointer directly.
foo *foo_free(foo_t **foo) {
__whatever_house_and_book_keeping ();
free (*foo);
*foo = NULL;
return NULL;
}
#define attr_cleanup(X) __attribute__ ((__cleanup__(X)))
....
{
foo_t *foo attr_cleanup (foo_free) = foo_new(ctxt);
...
}
2. At a higher level, you need to encapsulate the low-level memory allocations into some coherent strategy to manage the lifetime of objects. Often some combination of:
a) Allocating a pre-determined number of objects, suitable for the problem being tackled. (Good for deterministic behaviour).
b) Careful alignment of entity lifetime with the structure of the algorithm being run
c) Reference counting
d) Hierarchical allocation management, in combination with one of the previous
e) Liveness checking [either at a low level by scanning for pointers (v rare in C/C++), or some more abstract, problem-domain + type specific check] and GC
Probably forgetting some strategies.
This is one of the most important and hardest parts to get right. Some languages have features to make it harder, even impossible, to free used objects. But that leads to many programmers in such languages not understanding the importance of lifetime management - which remains important even in such languages for performance.
Posted Jun 10, 2024 16:00 UTC (Mon)
by Wol (subscriber, #4433)
[Link] (1 responses)
How come? It sounds like it typically doesn't work that way on Unix, but if MS define "realloc( ptr, 0)" to free the memory and return null, then "ptr = realloc( ptr, 0)" achieves exactly what I would like by that definition - it destroys the pointer at the same time as destroying the memory.
If you have something that then traps de-referencing a null pointer (or simply your code checks for a null pointer), then the likelihood of double-free, use-after-free, etc goes down.
Cheers,
Posted Jun 17, 2024 17:24 UTC (Mon)
by mrugiero (guest, #153040)
[Link]
Null pointers and error paths
Wol
Null pointers and error paths
#define xfree(ptr) do { \
free(ptr); \
ptr = NULL; \
} while (0)
