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

Rafaël Carré rafael.carre at gmail.com
Sat Aug 29 11:30:02 UTC 2009


>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 at 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:


More information about the freebsd-standards mailing list