On Tue, 10 Apr 2012, Måns Rullgård wrote:
Martin Storsjö <[email protected]> writes:
Plain POSIX malloc(0) is allowed to return either NULL or a
non-NULL pointer. The calling code should be ready to handle
a NULL return as a correct return (instead of a failure) if the size
to allocate was 0 - this makes sure the condition is handled
in a consistent way across platforms.
This also avoids calling posix_memalign(&ptr, 32, 0) on OS X,
which returns an invalid pointer (a non-NULL pointer that causes
crashes when passed to av_free).
Abort in debug mode, to help track down issues related to
incorrect handling of this case.
---
libavutil/mem.c | 7 ++++++-
1 file changed, 6 insertions(+), 1 deletion(-)
diff --git a/libavutil/mem.c b/libavutil/mem.c
index b6230cf..22ec9a8 100644
--- a/libavutil/mem.c
+++ b/libavutil/mem.c
@@ -68,8 +68,13 @@ void *av_malloc(size_t size)
long diff;
#endif
+#ifdef DEBUG
+ if (!size)
+ abort();
+#endif
I really don't like this. Doing things differently in some
(ill-defined) "debug" configuration is a recipe for weirdness.
Returning NULL for zero-size allocations as originally proposed should
solve all problems in a simple, consistent way.
I'd be happy to remove the debug-specific behaviour, if others can agree
on it too.
// Martin
_______________________________________________
libav-devel mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-devel