Bug 79839 - malloc(0) returns 0 on AIX even with _LINUX_SOURCE_COMPAT
Summary: malloc(0) returns 0 on AIX even with _LINUX_SOURCE_COMPAT
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: target (show other bugs)
Version: 4.8.4
: P3 normal
Target Milestone: 5.5
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2017-03-03 20:40 UTC by Jaideep Bajwa
Modified: 2017-05-09 12:14 UTC (History)
1 user (show)

See Also:
Host:
Target: aix
Build:
Known to work:
Known to fail:
Last reconfirmed: 2017-05-09 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jaideep Bajwa 2017-03-03 20:40:06 UTC
malloc(0) returning 0 is expected behaviour on AIX but compiling with -D_LINUX_SOURCE_COMPAT, malloc(0) should return a valid pointer. As per:
https://www.ibm.com/support/knowledgecenter/ssw_aix_71/com.ibm.aix.basetrf1/malloc.html 
It doesn't work for c++ program which includes <cstdlib>. In the header it states:
// Get rid of those macros defined in <stdlib.h> in lieu of real functions.
...
#undef malloc

The above seems to be causing the issue and resetting the behaviour of -D_LINUX_SOURCE_COMPAT.

Code to reproduce:
>cat malloc.cpp
#include <stdio.h>
//#include <stdlib.h>  //<-- Works this stdlib
#include <cstdlib>
int main() {
  printf("%p \n", malloc(0));
  return 0;
}

>g++ malloc.cpp -D_LINUX_SOURCE_COMPAT
>./a.out
0
Comment 1 Jonathan Wakely 2017-05-09 11:52:46 UTC
I can reproduce this on AIX 7.1.3.0 but it returns a valid pointer on AIX 7.2.0.0
Comment 2 Jonathan Wakely 2017-05-09 12:12:19 UTC
N.B. with GCC 6 and later you get the same behaviour for <stdlib.h> because that includes <cstdlib> now.

My tests on AIX 7.1.3.0 were not the same GCC version, it looks like this was fixed for GCC 6.2.0 by r233029 (and r237394 on trunk). The fixed header now has:

# 748 "/home/jwakely/gcc/6/lib/gcc/powerpc-ibm-aix7.2.0.0/6.3.1/include-fixed/stdlib.h" 3 4
extern void *__linux_malloc(size_t);
extern void *__linux_realloc(void *, size_t);
extern void *__linux_calloc(size_t, size_t);
extern void *__linux_valloc(size_t);

extern void *malloc(size_t) __asm__("__linux_malloc");
extern void *calloc(size_t, size_t) __asm__("__linux_calloc");
extern void *realloc(void *, size_t) __asm__("__linux_realloc");
extern void *valloc(size_t) __asm__("__linux_valloc");

This is necessary because the C++ standard explicitly forbids malloc and other functions from the C library from being defined as macros, they must be defined as functions.
Comment 3 Jonathan Wakely 2017-05-09 12:14:29 UTC
It was also fixed on the gcc-5-branch by r237479