From 34ee25734f752609707de339796c31b49fcfb683 Mon Sep 17 00:00:00 2001 From: Ben Noordhuis Date: Wed, 22 Jun 2016 16:06:26 +0200 Subject: [PATCH] unix: don't convert stat buffer when syscall fails MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Don't call uv__to_stat() when the stat/fstat/lstat system call fails; the stack-allocated buffer contains only garbage in that case. Not a very serious bug it's technically undefined behavior and it made valgrind squawk. Introduced in commit 499c7976 ("unix, windows: nanosecond resolution for uv_fs_[fl]stat"). PR-URL: https://github.com/libuv/libuv/pull/921 Reviewed-By: Colin Ihrig Reviewed-By: Saúl Ibarra Corretgé --- src/unix/fs.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/unix/fs.c b/src/unix/fs.c index 508b748a..0b2505e4 100644 --- a/src/unix/fs.c +++ b/src/unix/fs.c @@ -810,8 +810,11 @@ static void uv__to_stat(struct stat* src, uv_stat_t* dst) { static int uv__fs_stat(const char *path, uv_stat_t *buf) { struct stat pbuf; int ret; + ret = stat(path, &pbuf); - uv__to_stat(&pbuf, buf); + if (ret == 0) + uv__to_stat(&pbuf, buf); + return ret; } @@ -819,8 +822,11 @@ static int uv__fs_stat(const char *path, uv_stat_t *buf) { static int uv__fs_lstat(const char *path, uv_stat_t *buf) { struct stat pbuf; int ret; + ret = lstat(path, &pbuf); - uv__to_stat(&pbuf, buf); + if (ret == 0) + uv__to_stat(&pbuf, buf); + return ret; } @@ -828,8 +834,11 @@ static int uv__fs_lstat(const char *path, uv_stat_t *buf) { static int uv__fs_fstat(int fd, uv_stat_t *buf) { struct stat pbuf; int ret; + ret = fstat(fd, &pbuf); - uv__to_stat(&pbuf, buf); + if (ret == 0) + uv__to_stat(&pbuf, buf); + return ret; }