[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Bug-gnulib] GNUlib realloc POSIX comformance
From: |
Yoann Vandoorselaere |
Subject: |
[Bug-gnulib] GNUlib realloc POSIX comformance |
Date: |
Wed, 17 Nov 2004 18:29:21 +0100 |
Hi,
I'm in the process of using GNUlib realloc(), but have some question
relating to it's code:
The GNUlib realloc implementation will:
1) use malloc if the provided pointer is NULL.
2) set size to 1 if provided len is zero.
#1 is perfectly okay with me since some implementation of realloc() will
crash if the provided pointer is NULL, not comforming to the POSIX
standard.
#2, however doesn't sound okay:
- Quoting POSIX malloc definition:
"If the size of the space requested is 0, the behavior is
implementation-defined: the value returned shall be either a null
pointer or a unique pointer."
- Quoting POSIX realloc definition:
1. "If size is 0 and ptr is not a null pointer, the object pointed to
is freed."
2. "If size is 0, either a null pointer or a unique pointer that can
be successfully passed to free() shall be returned." Which mean
this is implementation defined.
First, the way GNUlib realloc() is implemented contradict with POSIX
definition #1.
GNUlib realloc() will set size to 1 if the provided size is 0,
regardless of whether the provided pointer is NULL, which mean we will
allocate a 1 byte block instead of freeing the block when the pointer is
NULL as per #1 POSIX recommendation.
Second, and IMHO GNUlib should not make a choise regarding the
'implementation defined' part of the #2 POSIX definition (setting size
to 1 if the provided size is zero). I personnally would assume a call to
malloc/realloc with size being 0 to be a bug in my code.
So my proposed realloc implementation would look like:
void *realloc(void *ptr, size_t size)
{
if ( ! ptr )
return malloc(size);
if ( size == 0 ) {
free(ptr);
return NULL;
}
return realloc(ptr, size);
}
Note that I'm handling the case where ptr is set and size is not zero
directly because I am not confident that every realloc implementation
comform to this POSIX definition.
Regards,
Ps: also, would it be possible to move the GNUlib realloc license from
GPL to LGPL ?
--
Yoann Vandoorselaere <address@hidden>
- [Bug-gnulib] GNUlib realloc POSIX comformance,
Yoann Vandoorselaere <=