From 01999559cfb7b8794da4c4796f5e44f4339d335e Mon Sep 17 00:00:00 2001 From: Jianghua Yang Date: Wed, 23 Sep 2015 16:37:40 +0800 Subject: [PATCH] fs: undo uv__req_init when uv__malloc failed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR-URL: https://github.com/libuv/libuv/pull/543 Reviewed-By: Saúl Ibarra Corretgé --- src/unix/fs.c | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/src/unix/fs.c b/src/unix/fs.c index d739c282..ff27e84a 100644 --- a/src/unix/fs.c +++ b/src/unix/fs.c @@ -80,8 +80,10 @@ req->path = path; \ } else { \ req->path = uv__strdup(path); \ - if (req->path == NULL) \ + if (req->path == NULL) { \ + uv__req_unregister(loop, req); \ return -ENOMEM; \ + } \ } \ } \ while (0) @@ -97,8 +99,10 @@ path_len = strlen(path) + 1; \ new_path_len = strlen(new_path) + 1; \ req->path = uv__malloc(path_len + new_path_len); \ - if (req->path == NULL) \ + if (req->path == NULL) { \ + uv__req_unregister(loop, req); \ return -ENOMEM; \ + } \ req->new_path = req->path + path_len; \ memcpy((void*) req->path, path, path_len); \ memcpy((void*) req->new_path, new_path, new_path_len); \ @@ -1062,8 +1066,10 @@ int uv_fs_mkdtemp(uv_loop_t* loop, uv_fs_cb cb) { INIT(MKDTEMP); req->path = uv__strdup(tpl); - if (req->path == NULL) + if (req->path == NULL) { + uv__req_unregister(loop, req); return -ENOMEM; + } POST; } @@ -1099,8 +1105,10 @@ int uv_fs_read(uv_loop_t* loop, uv_fs_t* req, if (nbufs > ARRAY_SIZE(req->bufsml)) req->bufs = uv__malloc(nbufs * sizeof(*bufs)); - if (req->bufs == NULL) + if (req->bufs == NULL) { + uv__req_unregister(loop, req); return -ENOMEM; + } memcpy(req->bufs, bufs, nbufs * sizeof(*bufs)); @@ -1224,8 +1232,10 @@ int uv_fs_write(uv_loop_t* loop, if (nbufs > ARRAY_SIZE(req->bufsml)) req->bufs = uv__malloc(nbufs * sizeof(*bufs)); - if (req->bufs == NULL) + if (req->bufs == NULL) { + uv__req_unregister(loop, req); return -ENOMEM; + } memcpy(req->bufs, bufs, nbufs * sizeof(*bufs));