[PATCH] avoid double-close in unusual dotlock situations
Daniel Kahn Gillmor
dkg at fifthhorseman.net
Fri Dec 19 23:12:37 CET 2014
* common/dotlock.c: (dotlock_create_unix) avoid double-close()
in unusual situations.
close() should not be retried after an EINTR since this may
cause a reused descriptor from another thread to be closed.
Before this patch was applied, if close(fd) failed with EINTR, it
would be closed again in the write_failed: block.
It could also have been closed a second time in the case that
(use_hardlinks_p (h->tname)) evaluated to something other than 0 or 1.
This patch avoids both of those scenarios.
Note that close() could still be called twice on the same file
descriptor if the first close(fd) fails but errno is not EINTR. I'm
not sure the right thing to do in that scenario. An alternate
resolution could be to unequivocally set fd to -1 after the first
failed close(fd), avoiding the errno == EINTR test.
common/dotlock.c | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)
diff --git a/common/dotlock.c b/common/dotlock.c
index c5520db..a9963d1 100644
@@ -680,7 +680,12 @@ dotlock_create_unix (dotlock_t h, const char *file_to_lock)
if ( write (fd, "\n", 1 ) != 1 )
if ( close (fd) )
- goto write_failed;
+ if ( errno == EINTR )
+ fd = -1;
+ goto write_failed;
+ fd = -1;
/* Check whether we support hard links. */
switch (use_hardlinks_p (h->tname))
@@ -718,7 +723,8 @@ dotlock_create_unix (dotlock_t h, const char *file_to_lock)
all_lockfiles = h->next;
my_error_2 (_("error writing to '%s': %s\n"), h->tname, strerror (errno));
- close (fd);
+ if ( fd != -1 )
+ close (fd);
More information about the Gnupg-devel