tempfile: avoid directory cleanup race
commitbabe2e05592f0e8025061ffc97e387e2aa70c99b
authorRené Scharfe <l.s.r@web.de>
Fri, 26 Aug 2022 22:46:29 +0000 (27 00:46 +0200)
committerJunio C Hamano <gitster@pobox.com>
Sat, 27 Aug 2022 17:17:46 +0000 (27 10:17 -0700)
tree8ae391e3f752bc233644839dcc365e048513c38f
parent0f5bd024f21b3700ce37148d75ad7918bf9977f8
tempfile: avoid directory cleanup race

The temporary directory created by mks_tempfile_dt() is deleted by first
deleting the file within, then truncating the filename strbuf and
passing the resulting string to rmdir(2).  When the cleanup routine is
invoked concurrently by a signal handler we can end up passing the now
truncated string to unlink(2), however, which could cause problems on
some systems.

Avoid that issue by remembering the directory name separately.  This way
the paths stay unchanged.  A signal handler can still race with normal
cleanup, but deleting the same files and directories twice is harmless.

Reported-by: Jeff King <peff@peff.net>
Signed-off-by: René Scharfe <l.s.r@web.de>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
tempfile.c
tempfile.h