Skip to contentSkip to conversation

malloc: warn about realloc portability pitfall

PATCH New Eric Blake 1 participant 1 message Mar 24, 2011 Man-pages: manual pages for Linux -- Sections 2, 3, 4, 5, and 7

lore
Share & tools
lore

Eric Blake

author
#0 lore raw

PATCH 1/1 malloc: warn about realloc portability pitfall

---

For more on the portability pitfall, and on the fact that glibc intentionally does not comply with C99 realloc:

http://austingroupbugs.net/view.php?id=400
http://sourceware.org/bugzilla/show_bug.cgi?id=12547
http://lists.gnu.org/archive/html/bug-gnulib/2011-03/msg00247.html

 man3/malloc.3 |   22 ++++++++++++++++++----
 1 files changed, 18 insertions(+), 4 deletions(-)
man3/malloc.3 +18 -4
index 4067aea..32815a1 100644
@@ -113,8 +113,16 @@ is equal to zero,
 and
 .I ptr
 is not NULL, then the call is equivalent to
-.IR free(ptr) .
-Unless
+.IR free(ptr)
+with a return of NULL, which is compliant to C89 but in violation of
+C99.  C99 semantics result in leaking either the original
+.I ptr
+(if NULL is returned) or an unexpected new zero-size allocation, if
+you are assuming GNU semantics; while GNU semantics result in a double
+free when NULL is returned if you are expecting C99 semantics.  It is
+better to avoid a
+.I size
+of 0.  Unless
 .I ptr
 is NULL, it must have been returned by an earlier call to
 .BR malloc (),
@@ -162,9 +170,15 @@ was equal to 0, either NULL or a pointer suitable to be passed to
 is returned.
 If
 .BR realloc ()
-fails the original block is left untouched; it is not freed or moved.
+fails and
+.I size
+was not 0, the original block is left untouched; it is not freed or
+moved.
 .SH "CONFORMING TO"
-C89, C99.
+C89 (all functions), C99 (only
+.BR malloc ,
+.BR calloc ,
+.BR free ).
 .SH NOTES
 By default, Linux follows an optimistic memory allocation strategy.
 This means that when
Signature · 6 lines
1.7.4

--
To unsubscribe from this list: send the line "unsubscribe linux-man" in
the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html