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
This commit is contained in:
Amal Raj T 2020-07-29 16:57:29 +05:30
parent ac0e4f1f39
commit 25a5d69ee6
2 changed files with 6 additions and 1 deletions

View File

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

View File

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