Problem of using malloc() without including stdlib.h
Jonathan Lu
luchsh at linux.vnet.ibm.com
Wed Jan 18 08:19:16 UTC 2012
Hi core-libs-dev,
I found that for some native code of OpenJDK code base, malloc() is used
without including header file stdlib.h, such as following files,
./src/solaris/native/sun/java2d/opengl/GLXSurfaceData.c
./src/solaris/native/sun/java2d/x11/XRBackendNative.c
....
I assume that there's no hacking tricks involved here, right? because
this may cause problem for some C compilers, which assumes 'int' as the
default return type of a function if it cannot find the function's
declaration during compiling. Under such a condition, actual return
result of type 'void*' from malloc() will be converted to 'int', which
may result in truncated pointers in 64bit platforms. If the application
tries to dereference such a broken pointer, error will occur.
Indeed I found some indirect includes of stdlib.h, but there're still
some I do not see a stdlib.h get included from any of the
direct/indirect included headers. I think in order to fix this problem,
two approaches may be considered here,
a) add "#include <stdlib.h>" to every missing .c file.
b) add "#include <stdlib.h>" to a commonly referenced header file, such
as jni_util.h. but it would not be easy to find such a file for all and
creating one is the same as approach a).
But both methods need to change many files, any other ideas about how to
fix it more elegantly?
Thanks and best regards!
- Jonathan
More information about the core-libs-dev
mailing list