> According to printf(3):
> 
>       snprintf() and vsnprintf() will write at most size-1 of the characters
>       printed into the output string (the size'th character then gets the ter-
>       minating `\0'); if the return value is greater than or equal to the size
>       argument, the string was too short and some of the printed characters
>       were discarded.  If size is zero, str may be a null pointer and no char-
>       acters will be written; the number of bytes that would have been written
>       excluding the terminating `\0' byte, or -1 on error, will be returned.
> 
> and:
> 
>       The snprintf() and vsnprintf() functions return the number of characters
>       that would have been output if the size were unlimited (again, not
>       including the final `\0').  If an output or encoding error occurs, a 
> val-
>       ue of -1 is returned instead.
> 
> I'm having trouble making snprintf return -1.  I've tried stuff like:
> 
>       len = snprintf(str, 0, "%.-Z\n", 9);
>       printf("%d", len);
> 
> but that just prints `2'.  Does snprintf ever return -1?

On some other systems, yes.  On OpenBSD -- today -- it cannot return -1.

However, that is absolutely no excuse to go writing unportable code.

You must check for either ret > buflen or ret == -1 being a failure
condition.

Reply via email to