From c123a6af9f5aee4baea1d9ab202e03df6de319e3 Mon Sep 17 00:00:00 2001 From: David Steele Date: Thu, 28 Apr 2022 11:19:01 -0400 Subject: [PATCH] Fix leaks in the storage/posix module. storagePosixPathCreate() leaked a String. storagePosixPathRemoveCallback() leaked a String. --- src/storage/posix/storage.c | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/storage/posix/storage.c b/src/storage/posix/storage.c index 57aa7755a..88fc722fe 100644 --- a/src/storage/posix/storage.c +++ b/src/storage/posix/storage.c @@ -353,7 +353,8 @@ storagePosixNewWrite(THIS_VOID, const String *file, StorageInterfaceNewWritePara /**********************************************************************************************************************************/ void storagePosixPathCreate( - THIS_VOID, const String *path, bool errorOnExists, bool noParentCreate, mode_t mode, StorageInterfacePathCreateParam param) + THIS_VOID, const String *const path, const bool errorOnExists, const bool noParentCreate, const mode_t mode, + const StorageInterfacePathCreateParam param) { THIS(StoragePosix); @@ -375,8 +376,12 @@ storagePosixPathCreate( // If the parent path does not exist then create it if allowed if (errno == ENOENT && !noParentCreate) { - storageInterfacePathCreateP(this, strPath(path), errorOnExists, noParentCreate, mode); + String *const pathParent = strPath(path); + + storageInterfacePathCreateP(this, pathParent, errorOnExists, noParentCreate, mode); storageInterfacePathCreateP(this, path, errorOnExists, noParentCreate, mode); + + strFree(pathParent); } // Ignore path exists if allowed else if (errno != EEXIST || errorOnExists) @@ -394,7 +399,7 @@ typedef struct StoragePosixPathRemoveData } StoragePosixPathRemoveData; static void -storagePosixPathRemoveCallback(void *callbackData, const StorageInfo *info) +storagePosixPathRemoveCallback(void *const callbackData, const StorageInfo *const info) { FUNCTION_TEST_BEGIN(); FUNCTION_TEST_PARAM_P(VOID, callbackData); @@ -406,8 +411,8 @@ storagePosixPathRemoveCallback(void *callbackData, const StorageInfo *info) if (!strEqZ(info->name, ".")) { - StoragePosixPathRemoveData *data = callbackData; - String *file = strNewFmt("%s/%s", strZ(data->path), strZ(info->name)); + StoragePosixPathRemoveData *const data = callbackData; + String *const file = strNewFmt("%s/%s", strZ(data->path), strZ(info->name)); // Rather than stat the file to discover what type it is, just try to unlink it and see what happens if (unlink(strZ(file)) == -1) // {vm_covered} @@ -421,6 +426,8 @@ storagePosixPathRemoveCallback(void *callbackData, const StorageInfo *info) else THROW_SYS_ERROR_FMT(PathRemoveError, STORAGE_ERROR_PATH_REMOVE_FILE, strZ(file)); // {vm_covered} } + + strFree(file); } FUNCTION_TEST_RETURN_VOID();