usage of asprintf
claudiamoroder at st-ulrich.suedtirol.net
Mon Jan 14 14:37:12 GMT 2002
the CodingSuggestions tells us all to use asprintf rather then pstrings and
fstring. What the CodingSuggestions don't tell us is how to check the results
of asprintf. Asprintf uses malloc to allocate the memory and can also fail (
I know I am boring ) . Even worse, the pointer asprintf gives back if it
fails, can point to a wrong place.
You can read the answer of Ulrich Drepper ( libc mantainer ) to a e-mail
asking to set the pointer to NULL in case of errors at
> - When either malloc or _IO_vfprintf call fails, _IO_vasprintf returns
> error code without setting result_ptr to zero.
That's nonsense. The code never did this and there is no reason to
ever do this.
( I wrote Ulrich about a strange behaviour in libc, I got the same type of
answer. Very nice guy ;-)
Searching in google you can see that this is not true for all unices. Certain
man pages tell you that the pointer is set to NULL.
It would be a smaller problem if all versions return NULL. In this case samba
crashes, but what in the case the pointer returned points to valid memory ?
Now, in samba there are NO checks after asprintf calls. Is this really ok ?
More information about the samba-technical