Skip to content

g_malloc() NULL path is dead code — saved_errno there is never reached #348

@spe-ciellt

Description

@spe-ciellt

g_malloc() NULL path is dead code — saved_errno there is never reached

GLib's g_malloc() is documented to never return NULL: on allocation failure it calls g_error() (which terminates the process). The if (buf == NULL) block at line 116 is therefore dead code, and the saved_errno additions there:

char *buf = (char *)g_malloc(fd->datalen + 1);
if (buf == NULL) {          /* ← can never be true */
    int saved_errno = errno;
    munmap(fd->data, fd->datalen);
    fclose(fd->fd);
    g_free(fd);
    errno = saved_errno;
    return NULL;
}

are unreachable. The PR did not introduce the dead if (buf == NULL) guard (it's pre-existing), and applying the idiom uniformly is defensively correct, so this is not a blocker. However, it's worth a comment that g_malloc is abort-on-OOM in GLib.

Similarly, g_new() at the top of the function (fd = g_new(gerb_file_t, 1)) never returns NULL, making the if (fd == NULL) return NULL check at line 64 also unreachable. Same pre-existing issue, not introduced here.

Considered minor and save for future updates.

Originally posted by @spe-ciellt in #319 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions