Skip to content

out BUGFIX truncate in ly_out_reset() based on the seek result - #2644

Open
GeekQAQ wants to merge 1 commit into
CESNET:develfrom
GeekQAQ:out-reset-seek-result
Open

GeekQAQ wants to merge 1 commit into
CESNET:develfrom
GeekQAQ:out-reset-seek-result

Conversation

@GeekQAQ

@GeekQAQ GeekQAQ commented Nov 19, 2026

Copy link
Copy Markdown

ly_out_reset() is documented to make the following printer functions rewrite the output instead of appending, and for files to seek to the beginning and truncate them. For file descriptor, file stream and file path outputs it decides whether to truncate by testing errno != ESPIPE even when the seek succeeded. A successful lseek()/fseek() does not change errno, so if errno happens to be ESPIPE before the call, the file is rewound but not truncated, and the tail of the previous output stays after the new one.

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

#include <libyang/libyang.h>

int
main(void)
{
    struct ly_out *out;
    char buf[64] = {0};
    int fd = open("out.txt", O_RDWR | O_CREAT | O_TRUNC, 0600);

    ly_out_new_fd(fd, &out);
    ly_print(out, "first output, longer\n");
    ly_print_flush(out);

    /* errno left by an earlier call, e.g. lseek() on a pipe or ly_out_reset() of a handler writing to a pipe */
    errno = ESPIPE;
    ly_out_reset(out);
    ly_print(out, "second\n");
    ly_print_flush(out);

    pread(fd, buf, sizeof buf - 1, 0);
    printf("file: \"%s\"\n", buf);

    ly_out_free(out, NULL, 1);
    return 0;
}

On devel f45939c the file is "second\nutput, longer\n" instead of "second\n". errno can be ESPIPE here without the caller doing anything unusual: ly_out_reset() on an output writing to a pipe sets it and returns success, so a later reset of a file output in the same thread does not truncate.

Fix: truncate only when the seek succeeded; a failed seek with ESPIPE is still accepted as before. The test_output_fd and test_output_file cases now set errno to ESPIPE before the reset and check the length of the file; they fail on the current tree and pass with the fix.

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

Whether to truncate the file was decided by errno != ESPIPE also after
a successful lseek()/fseek(), which does not change errno. With errno
left at ESPIPE from before (for example by ly_out_reset() of an output
writing to a pipe), the file was rewound but not truncated and the
tail of the previous output remained after the new one.
Copilot AI balanced review requested due to automatic review settings November 19, 2026 21:51

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