Submitted By: Robert Connolly (ashes) Date: 2007-05-07 Initial Package Version: 2.5 Upstream Status: Submitted http://sourceware.org/ml/libc-alpha/2004-05/msg00067.html http://sourceware.org/ml/libc-alpha/2004-06/msg00007.html Origin: Alt-Linux / Dmitry V. Levin Description: The asprintf(3) and vasprintf(3) functions are GNU extentions, not defined by C or Posix standards. In Glibc these functions leave (char **strp) undefined after an error. This patch resets (char **strp) to NULL after an error, for sanity. This patch, and the behavior it sets, was reviewed and discussed on the Glibc mailing list, and appeared to be accepted, and then it looks like it was forgotten about. 2004-06-03 Dmitry V. Levin * libio/vasprintf.c (_IO_vasprintf): Reset the result pointer to NULL on any error. * manual/stdio.texi: Reflect the change in asprintf API. --- glibc-2.5.orig/libio/vasprintf.c +++ glibc-2.5/libio/vasprintf.c @@ -50,7 +50,10 @@ _IO_vasprintf (result_ptr, format, args) we know we will never seek on the stream. */ string = (char *) malloc (init_string_size); if (string == NULL) - return -1; + { + *result_ptr = NULL; + return -1; + } #ifdef _IO_MTSAFE_IO sf._sbf._f._lock = NULL; #endif @@ -64,6 +67,7 @@ #endif if (ret < 0) { free (sf._sbf._f._IO_buf_base); + *result_ptr = NULL; return ret; } /* Only use realloc if the size we need is of the same (binary) --- glibc-2.5.orig/manual/stdio.texi +++ glibc-2.5/manual/stdio.texi @@ -2398,7 +2398,9 @@ to the newly allocated string at that lo The return value is the number of characters allocated for the buffer, or less than zero if an error occurred. Usually this means that the buffer -could not be allocated. +could not be allocated, and the value of @var{ptr} in this situation is +implementation-dependent (in glibc, @var{ptr} will be set to the null +pointer, but this behavior should not be relied upon). Here is how to use @code{asprintf} to get the same result as the @code{snprintf} example, but more easily: