From a91d225301ce7b204bbd2bdf21b97b252dc27bf5 Mon Sep 17 00:00:00 2001 From: Sangyoon Jang Date: Mon, 10 Feb 2020 15:24:00 +0900 Subject: [PATCH] Fix double close issue fd passed to fdopendir is used internally by the implementation, and should not otherwise be used by the application. Change-Id: Ib8de3e9782f8e2455329acaecaeb43d2646c3a6d Signed-off-by: Sangyoon Jang --- src/pkg_getsize.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/pkg_getsize.c b/src/pkg_getsize.c index 94ebd66..b459c2c 100644 --- a/src/pkg_getsize.c +++ b/src/pkg_getsize.c @@ -157,6 +157,16 @@ static long long __calculate_directory_size(int dfd, bool include_itself) size += __stat_size(&st); } + /* fd passed to fdopendir is used internally by the implementation, + * and should not otherwise be used by the application. + * So we need to pass duplicated fd to fdopendir. + */ + dfd = dup(dfd); + if (dfd == -1) { + LOGE("failed to duplicate fd, errno: %d (%s)", errno, + strerror_r(errno, buf, sizeof(buf))); + return -1; + } dir = fdopendir(dfd); if (dir == NULL) { LOGE("fdopendir() failed, errno: %d (%s)", errno, -- 2.7.4