From 25a5d69ee6478dd72302b280e11f9ac0ceb9553a Mon Sep 17 00:00:00 2001 From: Amal Raj T Date: Wed, 29 Jul 2020 16:57:29 +0530 Subject: [PATCH] fs: clobber req->path on uv_fs_mkstemp() error Since clobbering expects the string to be in writable area, uv_fs_mkstemp(NULL, req, "test_file", NULL) will result in segmentation fault. so the string was moved to stack to make it writable. Previous commit also ignored to preserve return code when initial check in uv_fs_mkstemp fails. See previous commit for more details --- src/unix/fs.c | 1 + test/test-fs.c | 6 +++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/unix/fs.c b/src/unix/fs.c index 9a8da0fa3..bcb8e2c5b 100644 --- a/src/unix/fs.c +++ b/src/unix/fs.c @@ -306,6 +306,7 @@ static int uv__fs_mkstemp(uv_fs_t* req) { if (path_length < pattern_size || strcmp(path + path_length - pattern_size, pattern)) { errno = EINVAL; + r = -1; goto clobber; } diff --git a/test/test-fs.c b/test/test-fs.c index ae9923a9a..26a6afcf0 100644 --- a/test/test-fs.c +++ b/test/test-fs.c @@ -1271,6 +1271,7 @@ TEST_IMPL(fs_mkstemp) { int r; int fd; const char path_template[] = "test_file_XXXXXX"; + const char test_file[] = "test_file"; uv_fs_t req; loop = uv_default_loop(); @@ -1290,7 +1291,10 @@ TEST_IMPL(fs_mkstemp) { ASSERT(strcmp(mkstemp_req1.path, mkstemp_req2.path) != 0); /* invalid template returns EINVAL */ - ASSERT(uv_fs_mkstemp(NULL, &mkstemp_req3, "test_file", NULL) == UV_EINVAL); + ASSERT(uv_fs_mkstemp(NULL, &mkstemp_req3, test_file, NULL) == UV_EINVAL); + + /* Make sure that path is empty string */ + ASSERT(strlen(mkstemp_req3.path) == 0); /* We can write to the opened file */ iov = uv_buf_init(test_buf, sizeof(test_buf));