From 48e7c2e3af50e74ea40fca00730b910cd2720bc3 Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Fri, 20 May 2022 22:06:33 -0400 Subject: [PATCH] root: Fix string handling of paths from PHYSFS_setRoot. Fixes #4. --- src/physfs.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/physfs.c b/src/physfs.c index a5da945..e7ceddd 100644 --- a/src/physfs.c +++ b/src/physfs.c @@ -1780,10 +1780,10 @@ int PHYSFS_setRoot(const char *archive, const char *subdir) if (i->root) allocator.Free(i->root); i->root = ptr; - i->rootlen = len; + i->rootlen = strlen(i->root); /* in case sanitizePlatformIndependentPath changed subdir */ - if (longest_root < len) - longest_root = len; + if (longest_root < i->rootlen) + longest_root = i->rootlen; } /* else */ break; @@ -2154,10 +2154,10 @@ static int verifyPath(DirHandle *h, char **_fname, int allowMissing) if (h->root) { const int isempty = (*fname == '\0'); - fname -= h->rootlen - 1; + fname -= h->rootlen + (isempty ? 0 : 1); strcpy(fname, h->root); if (!isempty) - fname[h->rootlen - 2] = '/'; + fname[h->rootlen] = '/'; *_fname = fname; } /* if */ @@ -2312,10 +2312,10 @@ static DirHandle *getRealDirHandle(const char *_fname) BAIL_IF(!_fname, PHYSFS_ERR_INVALID_ARGUMENT, NULL); __PHYSFS_platformGrabMutex(stateLock); - len = strlen(_fname) + longest_root + 1; + len = strlen(_fname) + longest_root + 2; allocated_fname = __PHYSFS_smallAlloc(len); BAIL_IF_MUTEX(!allocated_fname, PHYSFS_ERR_OUT_OF_MEMORY, stateLock, NULL); - fname = allocated_fname + longest_root; + fname = allocated_fname + longest_root + 1; if (sanitizePlatformIndependentPath(_fname, fname)) { DirHandle *i; @@ -2543,10 +2543,10 @@ int PHYSFS_enumerate(const char *_fn, PHYSFS_EnumerateCallback cb, void *data) __PHYSFS_platformGrabMutex(stateLock); - len = strlen(_fn) + longest_root + 1; + len = strlen(_fn) + longest_root + 2; allocated_fname = (char *) __PHYSFS_smallAlloc(len); BAIL_IF_MUTEX(!allocated_fname, PHYSFS_ERR_OUT_OF_MEMORY, stateLock, 0); - fname = allocated_fname + longest_root; + fname = allocated_fname + longest_root + 1; if (!sanitizePlatformIndependentPath(_fn, fname)) retval = PHYSFS_ENUM_STOP; else @@ -2749,10 +2749,10 @@ PHYSFS_File *PHYSFS_openRead(const char *_fname) BAIL_IF_MUTEX(!searchPath, PHYSFS_ERR_NOT_FOUND, stateLock, 0); - len = strlen(_fname) + longest_root + 1; + len = strlen(_fname) + longest_root + 2; allocated_fname = (char *) __PHYSFS_smallAlloc(len); BAIL_IF_MUTEX(!allocated_fname, PHYSFS_ERR_OUT_OF_MEMORY, stateLock, 0); - fname = allocated_fname + longest_root; + fname = allocated_fname + longest_root + 1; if (sanitizePlatformIndependentPath(_fname, fname)) { @@ -3140,10 +3140,10 @@ int PHYSFS_stat(const char *_fname, PHYSFS_Stat *stat) stat->readonly = 1; __PHYSFS_platformGrabMutex(stateLock); - len = strlen(_fname) + longest_root + 1; + len = strlen(_fname) + longest_root + 2; allocated_fname = (char *) __PHYSFS_smallAlloc(len); BAIL_IF_MUTEX(!allocated_fname, PHYSFS_ERR_OUT_OF_MEMORY, stateLock, 0); - fname = allocated_fname + longest_root; + fname = allocated_fname + longest_root + 1; if (sanitizePlatformIndependentPath(_fname, fname)) {