Patchwork [3,of,4,STABLE] chg: do not close dir fd while iterating

login
register
mail settings
Submitter Yuya Nishihara
Date Nov. 3, 2020, 2:54 a.m.
Message ID <9534de20358f656be05e.1604372087@mimosa>
Download mbox | patch
Permalink /patch/47541/
State Accepted
Headers show

Comments

Yuya Nishihara - Nov. 3, 2020, 2:54 a.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1604369750 -32400
#      Tue Nov 03 11:15:50 2020 +0900
# Branch stable
# Node ID 9534de20358f656be05e0633a3a2ba59932326f3
# Parent  731ea8fa1f110b2856826db5ed78fc1178dc1306
chg: do not close dir fd while iterating

It works so long as the dp is the last entry, but readdir(dp) would fail
with EBADF. Let's not do that and close the dir fd explicitly.

Patch

diff --git a/contrib/chg/chg.c b/contrib/chg/chg.c
--- a/contrib/chg/chg.c
+++ b/contrib/chg/chg.c
@@ -288,7 +288,7 @@  static void execcmdserver(const struct c
 				         de->d_name);
 				continue;
 			}
-			if (fd_value > STDERR_FILENO) {
+			if (fd_value > STDERR_FILENO && fd_value != dirfd(dp)) {
 				debugmsg("closing fd %ld", fd_value);
 				int res = close(fd_value);
 				if (res) {
@@ -298,6 +298,7 @@  static void execcmdserver(const struct c
 				}
 			}
 		}
+		closedir(dp);
 	}
 
 	if (putenv("CHGINTERNALMARK=") != 0)