Hi all:

uclibc seem will set argument *memptr to NULL when posix_memalign[1]
fail, I know it's not specify in most document[1-3],
but other libc (newlib, glibc, musl) implementations are not set it to
NULL if fail and gcc testsuite have 1 test case for that[7], so how
about make uclib consistent to other libc?

the patch attached.


newlib[4]:
int
__posix_memalign (void **memptr, size_t alignment, size_t size)
{
  void *mem;

  /* Test whether the ALIGNMENT argument is valid.  It must be a power
     of two multiple of sizeof (void *).  */
  if (alignment % sizeof (void *) != 0 || (alignment & (alignment - 1)) != 0)
    return EINVAL;

  mem = __libc_memalign (alignment, size);

  if (mem != NULL) // only set when success
    {
      *memptr = mem;
      return 0;
    }

  return ENOMEM;
}


glibc [5]:
int
__posix_memalign (void **memptr, size_t alignment, size_t size)
{
  void *mem;

  /* Test whether the SIZE argument is valid.  It must be a power of
     two multiple of sizeof (void *).  */
  if (alignment % sizeof (void *) != 0
      || !powerof2 (alignment / sizeof (void *)) != 0
      || alignment == 0)
    return EINVAL;

  /* Call the hook here, so that caller is posix_memalign's caller
     and not posix_memalign itself.  */
  void *(*hook) (size_t, size_t, const void *) =
    force_reg (__memalign_hook);
  if (__builtin_expect (hook != NULL, 0))
    mem = (*hook)(alignment, size, RETURN_ADDRESS (0));
  else
    mem = __libc_memalign (alignment, size);

  if (mem != NULL) { // only set when success
    *memptr = mem;
    return 0;
  }

  return ENOMEM;
}


musl [6]:
int posix_memalign(void **res, size_t align, size_t len)
{
        if (align < sizeof(void *)) return EINVAL;
        void *mem = __memalign(align, len);
        if (!mem) return errno;
        *res = mem;  // only set when success
        return 0;
}



[1] http://man7.org/linux/man-pages/man3/posix_memalign.3.html
[2] http://pubs.opengroup.org/onlinepubs/009695399/functions/posix_memalign.html
[3] https://linux.die.net/man/3/posix_memalign
[4] https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;a=blob;f=newlib/libc/sys/linux/malloc.c;h=25007e8896a1292d6ae821aa35d7b8973ea99bad;hb=HEAD#l4938
[5] https://sourceware.org/git/?p=glibc.git;a=blob;f=malloc/malloc.c;h=488579390578e09a1a232ac5161daee086dbbd6b;hb=HEAD#l5086
[6] http://git.musl-libc.org/cgit/musl/tree/src/malloc/posix_memalign.c
[7] https://github.com/gcc-mirror/gcc/blob/master/gcc/testsuite/gcc.dg/torture/pr60092.c