Skip to content

Commit

Permalink
unix,win: return EINVAL on nullptr args in uv_fs_{read,write}
Browse files Browse the repository at this point in the history
PR-URL: libuv#470
Reviewed-By: Saúl Ibarra Corretgé <saghul@gmail.com>
  • Loading branch information
skomski authored and saghul committed Aug 11, 2015
1 parent c2e6f3b commit 939ea06
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 2 deletions.
6 changes: 6 additions & 0 deletions src/unix/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1071,6 +1071,9 @@ int uv_fs_read(uv_loop_t* loop, uv_fs_t* req,
unsigned int nbufs,
int64_t off,
uv_fs_cb cb) {
if (bufs == NULL || nbufs == 0)
return -EINVAL;

INIT(READ);
req->file = file;

Expand Down Expand Up @@ -1193,6 +1196,9 @@ int uv_fs_write(uv_loop_t* loop,
unsigned int nbufs,
int64_t off,
uv_fs_cb cb) {
if (bufs == NULL || nbufs == 0)
return -EINVAL;

INIT(WRITE);
req->file = file;

Expand Down
6 changes: 6 additions & 0 deletions src/win/fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1831,6 +1831,9 @@ int uv_fs_read(uv_loop_t* loop,
unsigned int nbufs,
int64_t offset,
uv_fs_cb cb) {
if (bufs == NULL || nbufs == 0)
return UV_EINVAL;

uv_fs_req_init(loop, req, UV_FS_READ, cb);

req->file.fd = fd;
Expand Down Expand Up @@ -1864,6 +1867,9 @@ int uv_fs_write(uv_loop_t* loop,
unsigned int nbufs,
int64_t offset,
uv_fs_cb cb) {
if (bufs == NULL || nbufs == 0)
return UV_EINVAL;

uv_fs_req_init(loop, req, UV_FS_WRITE, cb);

req->file.fd = fd;
Expand Down
21 changes: 21 additions & 0 deletions test/test-fs.c
Original file line number Diff line number Diff line change
Expand Up @@ -2514,3 +2514,24 @@ TEST_IMPL(fs_write_alotof_bufs_with_offset) {
MAKE_VALGRIND_HAPPY();
return 0;
}


TEST_IMPL(fs_read_write_null_arguments) {
int r;

r = uv_fs_read(NULL, NULL, 0, NULL, 0, -1, NULL);
ASSERT(r == UV_EINVAL);

r = uv_fs_write(NULL, NULL, 0, NULL, 0, -1, NULL);
ASSERT(r == UV_EINVAL);

iov = uv_buf_init(NULL, 0);
r = uv_fs_read(NULL, NULL, 0, &iov, 0, -1, NULL);
ASSERT(r == UV_EINVAL);

iov = uv_buf_init(NULL, 0);
r = uv_fs_write(NULL, NULL, 0, &iov, 0, -1, NULL);
ASSERT(r == UV_EINVAL);

return 0;
}
2 changes: 2 additions & 0 deletions test/test-list.h
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ TEST_DECLARE (fs_scandir_file)
TEST_DECLARE (fs_open_dir)
TEST_DECLARE (fs_rename_to_existing_file)
TEST_DECLARE (fs_write_multiple_bufs)
TEST_DECLARE (fs_read_write_null_arguments)
TEST_DECLARE (fs_write_alotof_bufs)
TEST_DECLARE (fs_write_alotof_bufs_with_offset)
TEST_DECLARE (threadpool_queue_work_simple)
Expand Down Expand Up @@ -690,6 +691,7 @@ TASK_LIST_START
TEST_ENTRY (fs_write_multiple_bufs)
TEST_ENTRY (fs_write_alotof_bufs)
TEST_ENTRY (fs_write_alotof_bufs_with_offset)
TEST_ENTRY (fs_read_write_null_arguments)
TEST_ENTRY (threadpool_queue_work_simple)
TEST_ENTRY (threadpool_queue_work_einval)
TEST_ENTRY (threadpool_multiple_event_loops)
Expand Down
6 changes: 4 additions & 2 deletions test/test-threadpool-cancel.c
Original file line number Diff line number Diff line change
Expand Up @@ -279,10 +279,12 @@ TEST_IMPL(threadpool_cancel_fs) {
uv_fs_t reqs[25];
uv_loop_t* loop;
unsigned n;
uv_buf_t iov;

INIT_CANCEL_INFO(&ci, reqs);
loop = uv_default_loop();
saturate_threadpool();
iov = uv_buf_init(NULL, 0);

/* Needs to match ARRAY_SIZE(fs_reqs). */
n = 0;
Expand All @@ -300,7 +302,7 @@ TEST_IMPL(threadpool_cancel_fs) {
ASSERT(0 == uv_fs_lstat(loop, reqs + n++, "/", fs_cb));
ASSERT(0 == uv_fs_mkdir(loop, reqs + n++, "/", 0, fs_cb));
ASSERT(0 == uv_fs_open(loop, reqs + n++, "/", 0, 0, fs_cb));
ASSERT(0 == uv_fs_read(loop, reqs + n++, 0, NULL, 0, 0, fs_cb));
ASSERT(0 == uv_fs_read(loop, reqs + n++, 0, &iov, 1, 0, fs_cb));
ASSERT(0 == uv_fs_scandir(loop, reqs + n++, "/", 0, fs_cb));
ASSERT(0 == uv_fs_readlink(loop, reqs + n++, "/", fs_cb));
ASSERT(0 == uv_fs_rename(loop, reqs + n++, "/", "/", fs_cb));
Expand All @@ -310,7 +312,7 @@ TEST_IMPL(threadpool_cancel_fs) {
ASSERT(0 == uv_fs_symlink(loop, reqs + n++, "/", "/", 0, fs_cb));
ASSERT(0 == uv_fs_unlink(loop, reqs + n++, "/", fs_cb));
ASSERT(0 == uv_fs_utime(loop, reqs + n++, "/", 0, 0, fs_cb));
ASSERT(0 == uv_fs_write(loop, reqs + n++, 0, NULL, 0, 0, fs_cb));
ASSERT(0 == uv_fs_write(loop, reqs + n++, 0, &iov, 1, 0, fs_cb));
ASSERT(n == ARRAY_SIZE(reqs));

ASSERT(0 == uv_timer_init(loop, &ci.timer_handle));
Expand Down

0 comments on commit 939ea06

Please sign in to comment.