Skip to content

fix(stdio): `perror` should be allowed to take NULL prefixes as per spec

According to the spec:

[perror] writes a sequence of characters to the standard error stream thus: first (if s is not a null pointer and the character pointed to by s is not the null character), the string pointed to s followed by a colon (:) and a space; then the appropriate error message string followed by a new-line character. [...]

In other words, s is allowed to be NULL or empty. Other libc implementations follow through with this as well.

To test, build and run this snippet on Redox, before and after this patch; and a different OS:

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

int main(void) {
    // I'm not sure how else to set errno
    // For example, errno = EACCES didn't work
    FILE* empty= fopen("", "");
    assert(errno > 0);
    assert(!empty);

    // All of these should succeed
    perror(NULL);
    perror("");
    perror("Prefix");

    return 0;
}

Updates

  • Instead of assuming UTF8, perror checks the caller's string because C strings don't assume an encoding
  • perror should treat an empty string ("") like NULL as well
Edited by Josh Megnauth

Merge request reports