Skip to content

fix: Invalid space in `/etc/hostname`

Josh Megnauth requested to merge josh/installer:fix-hostname-space into master

Spaces and newlines are invalid in hostnames. However, the base config inadvertently creates an /etc/hostname with a newline after "redox".

This can cause programs to fail.

To see this in action:

#include <assert.h>
#include <limits.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

__attribute__((nonnull))
static void error(const char* function) {
    assert(function);
    const char* net_err = hstrerror(h_errno);
    assert(net_err);
    fprintf(stderr, "[ERROR] %s failed: [%d] %s\n", function, h_errno, net_err);
}

int main(void) {
    printf("[INFO] HOST_NAME_MAX is: %d\n", HOST_NAME_MAX);

    char host[HOST_NAME_MAX];
    if (gethostname(host, HOST_NAME_MAX) != 0) {
        error("gethostname()");
        return -1;
    }
    printf("[INFO] Host name is: %s\n", host);
    printf("[INFO] Length: %lu\n", strlen(host));

    struct hostent* local = gethostbyname(host);
    if (!local) {
        error("gethostbyname()");
        return -1;
    }

    printf("[INFO] Host name from gethostbyname(): %s\n", local->h_name);
    // assert(!strcmp(host, local->h_name));

    return 0;
}

Host will be "redox" with a newline after it. strlen(host) will yield six instead of five. gethostbyname(host) will also fail even though it shouldn't.

Merge request reports