standards/138307: posix_memalign has incorrect behaviour if size == 0

View: New views
2 Messages — Rating Filter:   Alert me  

standards/138307: posix_memalign has incorrect behaviour if size == 0

by Rafaël Carré-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message


>Number:         138307
>Category:       standards
>Synopsis:       posix_memalign has incorrect behaviour if size == 0
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-standards
>State:          open
>Quarter:        
>Keywords:      
>Date-Required:
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Sat Aug 29 11:30:02 UTC 2009
>Closed-Date:
>Last-Modified:
>Originator:     Rafaël Carré
>Release:        8-BETA2
>Organization:
>Environment:
FreeBSD kru 8.0-BETA2 FreeBSD 8.0-BETA2 #1: Thu Aug 13 15:49:26 CEST 2009     fun@kru:/usr/obj/usr/src/sys/GENERIC  amd64

>Description:
http://www.opengroup.org/onlinepubs/9699919799/functions/posix_memalign.html quotes:
"If the size of the space requested is 0, the behavior is implementation-defined; the value returned in memptr shall be either a null pointer or a unique pointer."

The behaviour is different using differents alignements:

posix_memalign either returns EINVAL, either asserts.

Sample test program is attached, output is:
--8<--
% ./a.out
malloc 0

align 4
Invalid argument

align 8
Assertion failed: (size != 0), function arena_malloc, file /usr/src/lib/libc/stdlib/malloc.c, line 3349.
zsh: abort (core dumped)  ./a.out
--8<--

call stack:

#2  0x000000080071c225 in __assert () from /lib/libc.so.7
#3  0x00000008006be011 in malloc_usable_size () from /lib/libc.so.7
#4  0x00000008006c1228 in posix_memalign () from /lib/libc.so.7


I got a report that it functions correctly on "FreeBSD turbine 7.1-RELEASE-p5 FreeBSD 7.1-RELEASE-p5 #7 r191765: Thu Aug 20 12:26:09 CEST 2009" but I can't test myself
>How-To-Repeat:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

int main(void)
{
    void *p;
    int i;

    errno = 0;

    printf("malloc 0\n");
    p = malloc(0);
    if(!p)
        printf("%s\n", strerror(errno));
    printf("\n");

    printf("align 4\n");
    i = posix_memalign(&p, 4, 0);
    printf("%s\n\n", strerror(i));

    printf("align 8\n");
    i = posix_memalign(&p, 8, 0);
    printf("%s\n\n", strerror(i));

    return 0;
}

>Fix:


>Release-Note:
>Audit-Trail:
>Unformatted:
_______________________________________________
freebsd-standards@... mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-standards
To unsubscribe, send any mail to "freebsd-standards-unsubscribe@..."

Parent Message unknown Re: standards/138307: posix_memalign has incorrect behaviour if size == 0

by Kostik Belousov :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

The following reply was made to PR standards/138307; it has been noted by GNATS.

From: Kostik Belousov <kostikbel@...>
To: =?koi8-r?Q?Rafa=EBl_Carr=E9?= <rafael.carre@...>
Cc: bug-followup@...
Subject: Re: standards/138307: posix_memalign has incorrect behaviour if size == 0
Date: Thu, 3 Sep 2009 16:44:38 +0300

 The following patch should help.
 
 diff --git a/lib/libc/stdlib/malloc.c b/lib/libc/stdlib/malloc.c
 index 270d641..b56b003 100644
 --- a/lib/libc/stdlib/malloc.c
 +++ b/lib/libc/stdlib/malloc.c
 @@ -5320,6 +5320,15 @@ posix_memalign(void **memptr, size_t alignment, size_t size)
  goto RETURN;
  }
 
 + if (size == 0) {
 + if (opt_sysv == false)
 + size = 1;
 + else {
 + result = NULL;
 + ret = 0;
 + goto RETURN;
 + }
 + }
  result = ipalloc(alignment, size);
  }
 
_______________________________________________
freebsd-standards@... mailing list
http://lists.freebsd.org/mailman/listinfo/freebsd-standards
To unsubscribe, send any mail to "freebsd-standards-unsubscribe@..."