root: Fix string handling of paths from PHYSFS_setRoot.

Fixes #4.
This commit is contained in:
Ryan C. Gordon 2022-05-20 22:06:33 -04:00
parent 497934818b
commit 48e7c2e3af
No known key found for this signature in database
GPG Key ID: FA148B892AB48044
1 changed files with 13 additions and 13 deletions

View File

@ -1780,10 +1780,10 @@ int PHYSFS_setRoot(const char *archive, const char *subdir)
if (i->root) if (i->root)
allocator.Free(i->root); allocator.Free(i->root);
i->root = ptr; i->root = ptr;
i->rootlen = len; i->rootlen = strlen(i->root); /* in case sanitizePlatformIndependentPath changed subdir */
if (longest_root < len) if (longest_root < i->rootlen)
longest_root = len; longest_root = i->rootlen;
} /* else */ } /* else */
break; break;
@ -2154,10 +2154,10 @@ static int verifyPath(DirHandle *h, char **_fname, int allowMissing)
if (h->root) if (h->root)
{ {
const int isempty = (*fname == '\0'); const int isempty = (*fname == '\0');
fname -= h->rootlen - 1; fname -= h->rootlen + (isempty ? 0 : 1);
strcpy(fname, h->root); strcpy(fname, h->root);
if (!isempty) if (!isempty)
fname[h->rootlen - 2] = '/'; fname[h->rootlen] = '/';
*_fname = fname; *_fname = fname;
} /* if */ } /* if */
@ -2312,10 +2312,10 @@ static DirHandle *getRealDirHandle(const char *_fname)
BAIL_IF(!_fname, PHYSFS_ERR_INVALID_ARGUMENT, NULL); BAIL_IF(!_fname, PHYSFS_ERR_INVALID_ARGUMENT, NULL);
__PHYSFS_platformGrabMutex(stateLock); __PHYSFS_platformGrabMutex(stateLock);
len = strlen(_fname) + longest_root + 1; len = strlen(_fname) + longest_root + 2;
allocated_fname = __PHYSFS_smallAlloc(len); allocated_fname = __PHYSFS_smallAlloc(len);
BAIL_IF_MUTEX(!allocated_fname, PHYSFS_ERR_OUT_OF_MEMORY, stateLock, NULL); 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)) if (sanitizePlatformIndependentPath(_fname, fname))
{ {
DirHandle *i; DirHandle *i;
@ -2543,10 +2543,10 @@ int PHYSFS_enumerate(const char *_fn, PHYSFS_EnumerateCallback cb, void *data)
__PHYSFS_platformGrabMutex(stateLock); __PHYSFS_platformGrabMutex(stateLock);
len = strlen(_fn) + longest_root + 1; len = strlen(_fn) + longest_root + 2;
allocated_fname = (char *) __PHYSFS_smallAlloc(len); allocated_fname = (char *) __PHYSFS_smallAlloc(len);
BAIL_IF_MUTEX(!allocated_fname, PHYSFS_ERR_OUT_OF_MEMORY, stateLock, 0); 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)) if (!sanitizePlatformIndependentPath(_fn, fname))
retval = PHYSFS_ENUM_STOP; retval = PHYSFS_ENUM_STOP;
else else
@ -2749,10 +2749,10 @@ PHYSFS_File *PHYSFS_openRead(const char *_fname)
BAIL_IF_MUTEX(!searchPath, PHYSFS_ERR_NOT_FOUND, stateLock, 0); 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); allocated_fname = (char *) __PHYSFS_smallAlloc(len);
BAIL_IF_MUTEX(!allocated_fname, PHYSFS_ERR_OUT_OF_MEMORY, stateLock, 0); 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)) if (sanitizePlatformIndependentPath(_fname, fname))
{ {
@ -3140,10 +3140,10 @@ int PHYSFS_stat(const char *_fname, PHYSFS_Stat *stat)
stat->readonly = 1; stat->readonly = 1;
__PHYSFS_platformGrabMutex(stateLock); __PHYSFS_platformGrabMutex(stateLock);
len = strlen(_fname) + longest_root + 1; len = strlen(_fname) + longest_root + 2;
allocated_fname = (char *) __PHYSFS_smallAlloc(len); allocated_fname = (char *) __PHYSFS_smallAlloc(len);
BAIL_IF_MUTEX(!allocated_fname, PHYSFS_ERR_OUT_OF_MEMORY, stateLock, 0); 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)) if (sanitizePlatformIndependentPath(_fname, fname))
{ {