From ccfd2f93716265968552012c83fcd6801a60446e Mon Sep 17 00:00:00 2001 From: Pu Cheng Date: Fri, 20 Nov 2026 05:49:41 +0800 Subject: [PATCH] out BUGFIX truncate in ly_out_reset() based on the seek result 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. --- src/out.c | 22 ++++++++++++---------- tests/utests/basic/test_inout.c | 6 ++++++ 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/out.c b/src/out.c index 110bb724e..f936c7948 100644 --- a/src/out.c +++ b/src/out.c @@ -305,11 +305,12 @@ ly_out_reset(struct ly_out *out) LOGINT(NULL); return LY_EINT; case LY_OUT_FD: - if ((lseek(out->method.fd, 0, SEEK_SET) == -1) && (errno != ESPIPE)) { - LOGERR(NULL, LY_ESYS, "Seeking output file descriptor failed (%s).", strerror(errno)); - return LY_ESYS; - } - if ((errno != ESPIPE) && (ftruncate(out->method.fd, 0) == -1)) { + if (lseek(out->method.fd, 0, SEEK_SET) == -1) { + if (errno != ESPIPE) { + LOGERR(NULL, LY_ESYS, "Seeking output file descriptor failed (%s).", strerror(errno)); + return LY_ESYS; + } + } else if (ftruncate(out->method.fd, 0) == -1) { LOGERR(NULL, LY_ESYS, "Truncating output file failed (%s).", strerror(errno)); return LY_ESYS; } @@ -317,11 +318,12 @@ ly_out_reset(struct ly_out *out) case LY_OUT_FDSTREAM: case LY_OUT_FILE: case LY_OUT_FILEPATH: - if ((fseek(out->method.f, 0, SEEK_SET) == -1) && (errno != ESPIPE)) { - LOGERR(NULL, LY_ESYS, "Seeking output file stream failed (%s).", strerror(errno)); - return LY_ESYS; - } - if ((errno != ESPIPE) && (ftruncate(fileno(out->method.f), 0) == -1)) { + if (fseek(out->method.f, 0, SEEK_SET) == -1) { + if (errno != ESPIPE) { + LOGERR(NULL, LY_ESYS, "Seeking output file stream failed (%s).", strerror(errno)); + return LY_ESYS; + } + } else if (ftruncate(fileno(out->method.f), 0) == -1) { LOGERR(NULL, LY_ESYS, "Truncating output file failed (%s).", strerror(errno)); return LY_ESYS; } diff --git a/tests/utests/basic/test_inout.c b/tests/utests/basic/test_inout.c index c5e1d5c9c..2657829a0 100644 --- a/tests/utests/basic/test_inout.c +++ b/tests/utests/basic/test_inout.c @@ -277,6 +277,8 @@ test_output_fd(void **UNUSED(state)) assert_int_equal(10, read(fd2, buf, 30)); assert_string_equal("test print", buf); assert_int_equal(0, lseek(fd2, 0, SEEK_SET)); + /* a value of errno left from before must not prevent truncating */ + errno = ESPIPE; assert_int_equal(LY_SUCCESS, ly_out_reset(out)); assert_int_equal(LY_SUCCESS, ly_write(out, "rewrite", 8)); @@ -321,6 +323,8 @@ test_output_file(void **UNUSED(state)) assert_non_null(fgets(buf, 31, f2)); assert_string_equal("test print", buf); assert_int_equal(0, fseek(f2, 0, SEEK_SET)); + /* a value of errno left from before must not prevent truncating */ + errno = ESPIPE; assert_int_equal(LY_SUCCESS, ly_out_reset(out)); assert_int_equal(LY_SUCCESS, ly_write(out, "rewrite", 8)); @@ -328,6 +332,8 @@ test_output_file(void **UNUSED(state)) ly_print_flush(out); assert_non_null(fgets(buf, 31, f2)); assert_string_equal("rewrite", buf); + assert_int_equal(0, fseek(f2, 0, SEEK_END)); + assert_int_equal(8, ftell(f2)); fclose(f2); ly_out_free(out, NULL, 1);