From 0b80e979adc5a5ad870e1acc234c53ba9f6080ec Mon Sep 17 00:00:00 2001 From: Jun Li Date: Tue, 3 Aug 2021 20:00:43 -0700 Subject: [PATCH] Fix dirname bug on MacOS --- src/tfs/src/tfs.c | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/tfs/src/tfs.c b/src/tfs/src/tfs.c index 9dc68dcdfd..61fbc61448 100644 --- a/src/tfs/src/tfs.c +++ b/src/tfs/src/tfs.c @@ -261,11 +261,20 @@ int tfsMkdirRecurAt(const char *rname, int level, int id) { // Try to create upper char *s = strdup(rname); - if (tfsMkdirRecurAt(dirname(s), level, id) < 0) { - tfree(s); + // Make a copy of dirname(s) because the implementation of 'dirname' differs on different platforms. + // Some platform may modify the contents of the string passed into dirname(). Others may return a pointer to + // internal static storage space that will be overwritten by next call. For case like that, we should not use + // the pointer directly in this recursion. + // See https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/dirname.3.html + char *dir = strdup(dirname(s)); + + if (tfsMkdirRecurAt(dir, level, id) < 0) { + free(s); + free(dir); return -1; } - tfree(s); + free(s); + free(dir); if (tfsMkdirAt(rname, level, id) < 0) { return -1; -- GitLab