From dca15ebabfa257fc76bb409387ec5e33887ca9da Mon Sep 17 00:00:00 2001 From: Nicholas Hastings Date: Mon, 27 Oct 2014 17:25:01 -0400 Subject: [PATCH 1/2] Fix OpenDirectory with use_valve_fs requirement of trailing slash. --- core/logic/smn_filesystem.cpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/core/logic/smn_filesystem.cpp b/core/logic/smn_filesystem.cpp index 16f20e3c..a5d2d8b2 100644 --- a/core/logic/smn_filesystem.cpp +++ b/core/logic/smn_filesystem.cpp @@ -271,13 +271,19 @@ static cell_t sm_OpenDirectory(IPluginContext *pContext, const cell_t *params) return 0; } + if (!path[0]) + { + return pContext->ThrowNativeError("Invalid file path"); + } + Handle_t handle = 0; if (params[0] >= 2 && params[2]) { + size_t len = strlen(path); char wildcardedPath[PLATFORM_MAX_PATH]; - snprintf(wildcardedPath, sizeof(wildcardedPath), "%s*", path); ValveDirectory *valveDir = new ValveDirectory; + snprintf(wildcardedPath, sizeof(wildcardedPath), "%s%s*", path, (path[len-1] != '/' && path[len-1] != '\\') ? "/" : ""); char *pathID; if ((err=pContext->LocalToStringNULL(params[3], &pathID)) != SP_ERROR_NONE) From 9021b23bc2b725a61c63cf5974185a4f8aedfec8 Mon Sep 17 00:00:00 2001 From: Nicholas Hastings Date: Mon, 27 Oct 2014 17:27:53 -0400 Subject: [PATCH 2/2] Fix crash on OpenDirectory with use_valve_fs if path not found. Also fixes minor memory leak on bad path. --- core/logic/smn_filesystem.cpp | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/core/logic/smn_filesystem.cpp b/core/logic/smn_filesystem.cpp index a5d2d8b2..2a04b009 100644 --- a/core/logic/smn_filesystem.cpp +++ b/core/logic/smn_filesystem.cpp @@ -179,7 +179,7 @@ private: struct ValveDirectory { - FileFindHandle_t hndl; + FileFindHandle_t hndl = -1; char szFirstPath[PLATFORM_MAX_PATH]; bool bHandledFirstPath; }; @@ -282,7 +282,6 @@ static cell_t sm_OpenDirectory(IPluginContext *pContext, const cell_t *params) { size_t len = strlen(path); char wildcardedPath[PLATFORM_MAX_PATH]; - ValveDirectory *valveDir = new ValveDirectory; snprintf(wildcardedPath, sizeof(wildcardedPath), "%s%s*", path, (path[len-1] != '/' && path[len-1] != '\\') ? "/" : ""); char *pathID; @@ -292,15 +291,18 @@ static cell_t sm_OpenDirectory(IPluginContext *pContext, const cell_t *params) return 0; } + ValveDirectory *valveDir = new ValveDirectory; + const char *pFirst = smcore.filesystem->FindFirstEx(wildcardedPath, pathID, &valveDir->hndl); - if (pFirst) + if (!pFirst) { - valveDir->bHandledFirstPath = false; - strncpy(valveDir->szFirstPath, pFirst, sizeof(valveDir->szFirstPath)); + delete valveDir; + return 0; } else { - valveDir->bHandledFirstPath = true; + valveDir->bHandledFirstPath = false; + strncpy(valveDir->szFirstPath, pFirst, sizeof(valveDir->szFirstPath)); } handle = handlesys->CreateHandle(g_ValveDirType, valveDir, pContext->GetIdentity(), g_pCoreIdent, NULL);