perror implementation

It looks like “perror” doesn’t handle an empty string the way I’d expect it
to, when compared to other “perror” implementations. The documentation
technically says that “perror” will always prefix the error string with ":
", but this is not the case. Other perror implementations will only leave
off the ": " when the prefix string is NULL.

Example code:

#include <stdio.h>
#include <errno.h>

int main() {
errno=EPERM;
perror(“fake”);
perror("");
perror(NULL);
return 0;
}

under QNX, this produces:

fake: Operation not permitted
Operation not permitted
Operation not permitted

under Linux (or Solaris), this code would produce:

fake: Operation not permitted
: Operation not permitted
Operation not permitted

Using “perror” with an empty prefix is useful for building complex error
messages without
having to #include <string.h> and call strerror(errno) yourself:

fprintf(stderr,“open(’%s’)”,filename);
perror("");

so you could get a pretty message like so:

open(‘some file’): some error


Is the QNX ": "-less behavior of perror with an empty string an accident or
intentional?

On redhat 5.1 I get

fake: Operation not permitted
Operation not permitted
Operation not permitted

On Solaris 8 I get

fake: Not owner
Not owner
Not owner

and from the solaris man page…

The argument string s is printed, followed by a colon and a blank,
followed by the message and a NEWLINE character. If s is a null
pointer or points to a null string, the colon is not printed.

and from the POSIX spec…

The perror() function maps the error number accessed through the
symbol errno to a language dependant error message, which shall be
written to the standard error stream as follows:

  • First (if s is not a null pointer and the character pointed to
    by s is not the null byte), the string pointed to by s followed by
    a colon and a character

  • Then an error message string followed by a character.

Kees Cook <cook@cpoint.net> wrote:

It looks like “perror” doesn’t handle an empty string the way I’d expect it
to, when compared to other “perror” implementations. The documentation
technically says that “perror” will always prefix the error string with ":
", but this is not the case. Other perror implementations will only leave
off the ": " when the prefix string is NULL.

Example code:

#include <stdio.h
#include <errno.h

int main() {
errno=EPERM;
perror(“fake”);
perror("");
perror(NULL);
return 0;
}

under QNX, this produces:

fake: Operation not permitted
Operation not permitted
Operation not permitted

under Linux (or Solaris), this code would produce:

fake: Operation not permitted
: Operation not permitted
Operation not permitted

Using “perror” with an empty prefix is useful for building complex error
messages without
having to #include <string.h> and call strerror(errno) yourself:

fprintf(stderr,“open(’%s’)”,filename);
perror("");

so you could get a pretty message like so:

open(‘some file’): some error



Is the QNX ": "-less behavior of perror with an empty string an accident or
intentional?


cburgess@qnx.com

You’re totally right. Sorry about the report. I’d swear this wasn’t true
under Linux, but it clearly is…

Colin Burgess wrote in message <98reob$o5c$1@nntp.qnx.com>…

On redhat 5.1 I get

fake: Operation not permitted
Operation not permitted
Operation not permitted