bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [bug-gnulib] valloc()?


From: Bruno Haible
Subject: Re: [bug-gnulib] valloc()?
Date: Fri, 4 Mar 2005 13:20:29 +0100
User-agent: KMail/1.5

Derek Price wrote:
> >But it doesn't do so if malloc() fails on non-POSIX systems (like mingw
> >or so). I think one should set errno = ENOMEM if malloc() fails.
>
> How about the attached patch?

+  errno = 0;
+  unaligned_ptr = malloc (size + pagesize - 1);
+  if (unaligned_ptr == NULL)
+    {
+      /* Failed malloc on some non-posix systems (e.g. mingw) fail to set
+         errno.  */
+      if (!errno) errno = ENOMEM;
+      return NULL;
+    }

malloc() can (and usually does) cause system calls that can modify errno.
I.e. on an ISO C compliant implementation, errno can be anything after
malloc() returns. I.e. it is not meaningful.

Also, looking at the value of _POSIX_C_SOURCE to determine whether malloc()
will set errno or not is doomed, because even in mingw, people can
#define _POSIX_C_SOURCE 2
or similar.

So the only thing I consider possible is the appended patch.

Bruno

diff -c -3 -r1.4 pagealign_alloc.c
*** pagealign_alloc.c   3 Mar 2005 20:38:38 -0000       1.4
--- pagealign_alloc.c   4 Mar 2005 12:23:25 -0000
***************
*** 149,155 ****
    size_t pagesize = getpagesize ();
    void *unaligned_ptr = malloc (size + pagesize - 1);
    if (unaligned_ptr == NULL)
!     return NULL;
    ret = (char *) unaligned_ptr
          + ((- (unsigned long) unaligned_ptr) & (pagesize - 1));
    new_memnode (ret, unaligned_ptr);
--- 149,160 ----
    size_t pagesize = getpagesize ();
    void *unaligned_ptr = malloc (size + pagesize - 1);
    if (unaligned_ptr == NULL)
!     {
!       /* Set errno.  We don't know whether malloc already set errno: some
!        implementations of malloc do, some don't.  */
!       errno = ENOMEM;
!       return NULL;
!     }
    ret = (char *) unaligned_ptr
          + ((- (unsigned long) unaligned_ptr) & (pagesize - 1));
    new_memnode (ret, unaligned_ptr);





reply via email to

[Prev in Thread] Current Thread [Next in Thread]