Skip to content

in BUGFIX reject a stream without a file descriptor in ly_in_file() - #2642

Open
GeekQAQ wants to merge 1 commit into
CESNET:develfrom
GeekQAQ:in-file-nofd
Open

GeekQAQ wants to merge 1 commit into
CESNET:develfrom
GeekQAQ:in-file-nofd

Conversation

@GeekQAQ

@GeekQAQ GeekQAQ commented Nov 19, 2026

Copy link
Copy Markdown

ly_in_file() documents NULL in case of invalid argument or an error when setting up the new input file, original input handler @p in is untouched. Given a stream that has no file descriptor (fmemopen(), open_memstream(), fopencookie()), it instead returns the previous stream (success) and stores the new stream in the handler, while the handler keeps reading the previous file. ly_in_new_file() refuses the same stream with LY_EINVAL.

#include <stdio.h>
#include <string.h>

#include <libyang/libyang.h>

int
main(void)
{
    struct ly_in *in;
    FILE *f1, *f2, *prev;
    char mem[] = "new data", buf[4] = {0};

    f1 = fopen("old.txt", "w");
    fputs("old data", f1);
    fclose(f1);

    f1 = fopen("old.txt", "r");
    ly_in_new_file(f1, &in);

    /* a stream without a file descriptor, fileno() returns -1 */
    f2 = fmemopen(mem, strlen(mem), "r");
    prev = ly_in_file(in, f2);
    printf("ly_in_file() returned %s\n", !prev ? "NULL" : (prev == f1) ? "the previous stream" : "?");
    printf("handler stream is %s\n", (ly_in_file(in, NULL) == f2) ? "the new stream" : "the previous stream");
    ly_in_read(in, buf, 3);
    printf("read \"%s\"\n", buf);

    ly_in_free(in, 0);
    fclose(f1);
    fclose(f2);
    return 0;
}

On devel f45939c this prints:

ly_in_file() returned the previous stream
handler stream is the new stream
read "old"

A later ly_in_file() call with a regular file then returns NULL (failure), although the handler does read the new file, and the handler still reports the fmemopen() stream.

Cause: ly_in_file() passes fileno(f) to ly_in_fd(), and -1 there means "get the current descriptor", so the call succeeds without changing the input. The handler's stream is then replaced anyway.

Fix: check fileno(f) first and return NULL without touching the handler, with an error message. The new case in test_input_file (not on Windows) fails on the current tree and passes with the fix.

Tested on devel f45939c with clang 21, -fsanitize=address,undefined, ENABLE_TESTS=ON: ctest passes (70 tests).

For a stream without a file descriptor (fmemopen(), open_memstream())
fileno() returns -1, which ly_in_fd() takes as a query of the current
descriptor. ly_in_file() then returned the previous stream as if the
input was changed and stored the new stream in the handler, while the
handler kept reading the previous file. Return NULL and leave the
handler untouched, as documented.
Copilot AI balanced review requested due to automatic review settings November 19, 2026 21:50

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants