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 <santiago.gimeno@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
This commit is contained in:
cjihrig 2017-09-19 18:02:51 -04:00
parent eaf25ae3eb
commit a41f8b52e1
No known key found for this signature in database
GPG Key ID: 7434390BDBE9B9C5
2 changed files with 10 additions and 2 deletions

View File

@ -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);

View File

@ -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;