From a0d46cded9286299f265ac993bf109b6b417dc32 Mon Sep 17 00:00:00 2001
From: Rob Swindell <rob@synchro.net>
Date: Tue, 15 Sep 2020 22:19:24 -0700
Subject: [PATCH] Resolve race condition around temp directory creation It's
 possible that isdir(temp_dir) will be false and then a mkpath() call will
 fail because some other thread came in and crated the directory at that very
 moment - so save errno if mkdir fails and call isdir() again to double-check
 that the directory wasn't created by someone else already.

---
 src/sbbs3/load_cfg.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/src/sbbs3/load_cfg.c b/src/sbbs3/load_cfg.c
index 61c69d880b..ee0ecb55f8 100644
--- a/src/sbbs3/load_cfg.c
+++ b/src/sbbs3/load_cfg.c
@@ -347,8 +347,11 @@ int md(const char* inpath)
 		*p = '\0';
 
 	if(!isdir(path)) {
-		if(mkpath(path) != 0)
-			return errno;
+		if(mkpath(path) != 0) {
+			int result = errno;
+			if(!isdir(path)) // race condition: did another thread make the directory already?
+				return result;
+		}
 	}
 	
 	return 0;
-- 
GitLab