From a41f8b52e1e22b39a2d06a600e47dad96fae35c1 Mon Sep 17 00:00:00 2001 From: cjihrig Date: Tue, 19 Sep 2017 18:02:51 -0400 Subject: [PATCH] unix: support copying empty files Initialize the error variable in uv_fs_copyfile() to zero. If the source file being copied has no data in it, then it's possible to make it to the exit without ever setting error. In this situation, the function's behavior is unpredictable. Refs: https://github.com/libuv/libuv/pull/1551 PR-URL: https://github.com/libuv/libuv/pull/1552 Reviewed-By: Santiago Gimeno Reviewed-By: Ben Noordhuis --- src/unix/fs.c | 1 + test/test-fs-copyfile.c | 11 +++++++++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/unix/fs.c b/src/unix/fs.c index a6366255..00af4675 100644 --- a/src/unix/fs.c +++ b/src/unix/fs.c @@ -795,6 +795,7 @@ static ssize_t uv__fs_copyfile(uv_fs_t* req) { int64_t in_offset; dstfd = -1; + err = 0; /* Open the source file. */ srcfd = uv_fs_open(NULL, &fs_req, req->path, O_RDONLY, 0, NULL); diff --git a/test/test-fs-copyfile.c b/test/test-fs-copyfile.c index 2d1f9079..e91044e0 100644 --- a/test/test-fs-copyfile.c +++ b/test/test-fs-copyfile.c @@ -119,6 +119,13 @@ TEST_IMPL(fs_copyfile) { ASSERT(r == 0); handle_result(&req); + /* Copies a file of size zero. */ + unlink(dst); + touch_file(src, 0); + r = uv_fs_copyfile(NULL, &req, src, dst, 0, NULL); + ASSERT(r == 0); + handle_result(&req); + /* Copies file synchronously. Overwrites existing file. */ r = uv_fs_copyfile(NULL, &req, fixture, dst, 0, NULL); ASSERT(r == 0); @@ -141,9 +148,9 @@ TEST_IMPL(fs_copyfile) { unlink(dst); r = uv_fs_copyfile(loop, &req, fixture, dst, 0, handle_result); ASSERT(r == 0); - ASSERT(result_check_count == 3); - uv_run(loop, UV_RUN_DEFAULT); ASSERT(result_check_count == 4); + uv_run(loop, UV_RUN_DEFAULT); + ASSERT(result_check_count == 5); unlink(dst); /* Cleanup */ return 0;