Bug 129 - failure to define MALLOC_0_RETURNS_NULL in libX11
Summary: failure to define MALLOC_0_RETURNS_NULL in libX11
Alias: None
Product: xorg
Classification: Unclassified
Component: Lib/Xlib (show other bugs)
Version: unspecified
Hardware: x86 (IA32) FreeBSD
: high normal
Assignee: Eric Anholt
QA Contact:
Depends on:
Reported: 2003-10-29 00:13 UTC by Eric Anholt
Modified: 2011-10-15 17:23 UTC (History)
0 users

See Also:
i915 platform:
i915 features:


Description Eric Anholt 2003-10-29 00:13:45 UTC
The XFree86 build defined MALLOC_0_RETURNS_NULL on platforms such as FreeBSD
where that was the case.  The libX11 build doesn't appear to be doing that.

If X11/Xlibint.h is used by more than libX11, getting that defined in every
piece of software that uses it may be difficult and we should instead fix
software to not do mallocs of 0.  A trivial inclusion of a printf in libX11 in
the case an Xmalloc/Xrealloc/Xcalloc with size 0 was tried resulted in no
printfs happening for the couple of trivial apps tested. 

Would it be accepted for there to be patches applied to remove the need for
Xmalloc(0) to be handled in a standard way?
Comment 1 Jim Gettys 2003-10-29 10:15:44 UTC
I suspect right now we should make sure to build with the same flags that
XFree86 does on a per platform basis.

Can you gen a patch so that configure does the right thing for us?

I'm chary about the chance of being able to fix code.

The fundamental problem came about as malloc had different behavior in the
presence of 0 size on different platforms.

Patches that fix our code to avoid this case may be ok, but it is hard
to say what out there may be using Xlibint.h, which is certainly public to
extension writers.

 * Note that some machines do not return a valid pointer for malloc(0), in
 * which case we provide an alternate under the control of the
 * define MALLOC_0_RETURNS_NULL.  This is necessary because some
 * Xlib code expects malloc(0) to return a valid pointer to storage.
Comment 2 Eric Anholt 2003-10-29 23:43:34 UTC
I'm in the process of going through libX11 to find calls of Xmalloc and friends
with the possibility of being called with size 0.  I'm also running with a
libX11 on my desktop system which should printf when a 0 allocation is attempted.
Comment 3 Jim Gettys 2003-10-30 07:28:23 UTC
You'll need to look through all the extensions as well; Xlibint.h is
public to extension writers.


A better fix, of course, would have been to do what you are now doing,
but 15 years ago, when the problem first came up.
                          - Jim
Comment 4 Eric Anholt 2003-11-08 21:23:57 UTC
I have been convinced to not work on fixing libX11 to not malloc(0).  Though the
impact on the code is small, it will take a long time to review the whole
library.  Using the autotools-supplied malloc(0) != NULL test doesn't work well
because it wouldn't get Xmalloc fixed for extensions.  For now I'm manually
definnig MALLOC_0_RETURNS_NULL in FreeBSD packages.  The proper short-term fix
would be to write an autoconf test to define MALLOC_0_RETURNS_NULL.
Comment 5 Daniel Stone 2007-03-26 08:31:06 UTC
reopening all RESOLVED/{LATER,REMIND} bugs.  now is later, and this is a reminder. ;)
Comment 6 Eric Anholt 2010-08-27 21:07:57 UTC
commit 222df94cf4d433b893c092ccd7c38d86d59ce034
Author: Kevin E Martin <kem@kem.org>
Date:   Sat Dec 3 04:41:21 2005 +0000

    Add macro to detect when malloc(0) returns NULL for use in libraries.

Use of freedesktop.org services, including Bugzilla, is subject to our Code of Conduct. How we collect and use information is described in our Privacy Policy.