From c38ef605cc9abd1081cb062fe80f7dd7d0475bd1 Mon Sep 17 00:00:00 2001 From: Maschell Date: Fri, 23 Feb 2024 11:07:56 +0100 Subject: [PATCH] StorageAPI: Update GetItemSize to take an additional itemType parameter to avoid a accidential string to binary conversion (e.g. for hashes) --- Dockerfile | 2 +- source/utils/storage/StorageItem.cpp | 9 +++++++-- source/utils/storage/StorageItem.h | 4 +++- source/utils/storage/StorageUtils.cpp | 24 ++++++++++++++++++------ source/utils/storage/StorageUtils.h | 2 +- 5 files changed, 30 insertions(+), 11 deletions(-) diff --git a/Dockerfile b/Dockerfile index dea9e42..19572a1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,7 @@ FROM ghcr.io/wiiu-env/devkitppc:20230621 COPY --from=ghcr.io/wiiu-env/wiiumodulesystem:0.3.2-dev-20231203-2e5832b /artifacts $DEVKITPRO -COPY --from=ghcr.io/wiiu-env/wiiupluginsystem:0.8.0-dev-20231221-ca17105 /artifacts $DEVKITPRO +COPY --from=ghcr.io/wiiu-env/wiiupluginsystem:0.8.0-dev-20240223-46f4cf6 /artifacts $DEVKITPRO COPY --from=ghcr.io/wiiu-env/libfunctionpatcher:20230621 /artifacts $DEVKITPRO COPY --from=ghcr.io/wiiu-env/libmappedmemory:20230621 /artifacts $DEVKITPRO COPY --from=ghcr.io/wiiu-env/libwupsbackend:20230621 /artifacts $DEVKITPRO diff --git a/source/utils/storage/StorageItem.cpp b/source/utils/storage/StorageItem.cpp index 0a9d07e..82cdd85 100644 --- a/source/utils/storage/StorageItem.cpp +++ b/source/utils/storage/StorageItem.cpp @@ -148,11 +148,16 @@ bool StorageItem::getValue(int64_t &result) const { return false; } -bool StorageItem::getItemSize(uint32_t &outSize) const { +bool StorageItem::getItemSizeString(uint32_t &outSize) const { if (mType == StorageItemType::String) { outSize = (std::get(mData).length() + 1); return true; - } else if (mType == StorageItemType::Binary) { + } + return false; +} + +bool StorageItem::getItemSizeBinary(uint32_t &outSize) const { + if (mType == StorageItemType::Binary) { outSize = std::get>(mData).size(); return true; } diff --git a/source/utils/storage/StorageItem.h b/source/utils/storage/StorageItem.h index bfe55bf..0ceed32 100644 --- a/source/utils/storage/StorageItem.h +++ b/source/utils/storage/StorageItem.h @@ -73,7 +73,9 @@ public: return mKey; } - bool getItemSize(uint32_t &outSize) const; + bool getItemSizeString(uint32_t &outSize) const; + + bool getItemSizeBinary(uint32_t &outSize) const; bool attemptBinaryConversion(); diff --git a/source/utils/storage/StorageUtils.cpp b/source/utils/storage/StorageUtils.cpp index fd8797b..d5acc5e 100644 --- a/source/utils/storage/StorageUtils.cpp +++ b/source/utils/storage/StorageUtils.cpp @@ -400,7 +400,7 @@ namespace StorageUtils { } WUPSStorageError err = Helper::LoadFromFile(plugin_id, *root); - if(err == WUPS_STORAGE_ERROR_NOT_FOUND) { + if (err == WUPS_STORAGE_ERROR_NOT_FOUND) { // Create new clean StorageItemRoot if no existing storage was found root = make_unique_nothrow(plugin_id); } else if (err != WUPS_STORAGE_ERROR_SUCCESS) { @@ -512,10 +512,13 @@ namespace StorageUtils { return WUPS_STORAGE_ERROR_NOT_FOUND; } - WUPSStorageError GetItemSize(wups_storage_root_item root, wups_storage_item parent, const char *key, uint32_t *outSize) { + WUPSStorageError GetItemSize(wups_storage_root_item root, wups_storage_item parent, const char *key, WUPSStorageItemType itemType, uint32_t *outSize) { if (!outSize) { return WUPS_STORAGE_ERROR_INVALID_ARGS; } + if (itemType != WUPS_STORAGE_ITEM_STRING && itemType != WUPS_STORAGE_ITEM_BINARY) { + return WUPS_STORAGE_ERROR_UNEXPECTED_DATA_TYPE; + } std::lock_guard lock(gStorageMutex); auto subItem = StorageUtils::Helper::getSubItem(root, parent); if (!subItem) { @@ -523,15 +526,24 @@ namespace StorageUtils { } auto item = subItem->getItem(key); if (item) { - // Trigger potential string -> binary conversion. - if (!item->attemptBinaryConversion()) { - return WUPS_STORAGE_ERROR_MALLOC_FAILED; + if (itemType == WUPS_STORAGE_ITEM_BINARY) { + // Trigger potential string -> binary conversion. + if (!item->attemptBinaryConversion()) { + return WUPS_STORAGE_ERROR_MALLOC_FAILED; + } } uint32_t tmp = 0; - if (item->getItemSize(tmp)) { + bool res = false; + if (itemType == WUPS_STORAGE_ITEM_STRING) { + res = item->getItemSizeString(tmp); + } else if (itemType == WUPS_STORAGE_ITEM_BINARY) { + res = item->getItemSizeBinary(tmp); + } + if (res) { *outSize = tmp; return WUPS_STORAGE_ERROR_SUCCESS; } + DEBUG_FUNCTION_LINE_ERR("Failed to get size for item %s", key); return WUPS_STORAGE_ERROR_UNEXPECTED_DATA_TYPE; } return WUPS_STORAGE_ERROR_NOT_FOUND; diff --git a/source/utils/storage/StorageUtils.h b/source/utils/storage/StorageUtils.h index be9f8a2..8294997 100644 --- a/source/utils/storage/StorageUtils.h +++ b/source/utils/storage/StorageUtils.h @@ -17,5 +17,5 @@ namespace StorageUtils::API { WUPSStorageError GetSubItem(wups_storage_root_item root, wups_storage_item parent, const char *key, wups_storage_item *outItem); WUPSStorageError StoreItem(wups_storage_root_item root, wups_storage_item parent, const char *key, WUPSStorageItemType itemType, void *data, uint32_t length); WUPSStorageError GetItem(wups_storage_root_item root, wups_storage_item parent, const char *key, WUPSStorageItemType itemType, void *data, uint32_t maxSize, uint32_t *outSize); - WUPSStorageError GetItemSize(wups_storage_root_item root, wups_storage_item parent, const char *key, uint32_t *outSize); + WUPSStorageError GetItemSize(wups_storage_root_item root, wups_storage_item parent, const char *key, WUPSStorageItemType itemType, uint32_t *outSize); } // namespace StorageUtils::API