Problem of using malloc() without including stdlib.h

Jonathan Lu luchsh at linux.vnet.ibm.com
Wed Jan 18 08:53:04 UTC 2012


On 01/18/2012 04:31 PM, Alan Bateman wrote:
> On 18/01/2012 08:19, Jonathan Lu wrote:
>> 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
>>
> I would suggest bringing this up on the 2d-dev list so that the folks 
> that maintain this area can help.
>
> -Alan.

Actually some core files are also involved in this problem, such as
./src/windows/native/java/io/io_util_md.c
./src/share/back/debugInit.c

And this problem seems very generic, so I guess core-libs-dev list might 
be the place to raise it.

-Jonathan




More information about the core-libs-dev mailing list