char *strerror_r(int errnum, char *buf, size_t buflen);
What are these buf
/buflen
parameters for?
Empty buffer works like a charm:
char* buf = nullptr;
fprintf(stderr, strerror_r(errno, buf, 0));
Also this buffer looks like unused:
char buf[1024];
fprintf(stderr, "%s\n", strerror_r(errno, buf, sizeof buf)); // Correct message here
fprintf(stderr, "%s\n", buf); // Empty
As you noticed in comments, buf and buflen parameters is used only if you pass invalid errnum, negative or unknown errno value. This is confirmed by the source code of the function.
In relation capacity of the buffer, I think 1024 bytes will be enough. Moreover, it's the exact same size which strerror implementation uses (that's thread unsafe). See also the related answer and the comment to it.
Of course, it all is concern of GNU-version of the function. XSI-compliant version always uses this buffer for copy of a static string.