StorageAPI: Update GetItemSize to take an additional itemType parameter to avoid a accidential string to binary conversion (e.g. for hashes)

This commit is contained in:
Maschell 2024-02-23 11:07:56 +01:00
parent 8cc78de58c
commit c38ef605cc
5 changed files with 30 additions and 11 deletions

View File

@ -1,7 +1,7 @@
FROM ghcr.io/wiiu-env/devkitppc:20230621 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/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/libfunctionpatcher:20230621 /artifacts $DEVKITPRO
COPY --from=ghcr.io/wiiu-env/libmappedmemory:20230621 /artifacts $DEVKITPRO COPY --from=ghcr.io/wiiu-env/libmappedmemory:20230621 /artifacts $DEVKITPRO
COPY --from=ghcr.io/wiiu-env/libwupsbackend:20230621 /artifacts $DEVKITPRO COPY --from=ghcr.io/wiiu-env/libwupsbackend:20230621 /artifacts $DEVKITPRO

View File

@ -148,11 +148,16 @@ bool StorageItem::getValue(int64_t &result) const {
return false; return false;
} }
bool StorageItem::getItemSize(uint32_t &outSize) const { bool StorageItem::getItemSizeString(uint32_t &outSize) const {
if (mType == StorageItemType::String) { if (mType == StorageItemType::String) {
outSize = (std::get<std::string>(mData).length() + 1); outSize = (std::get<std::string>(mData).length() + 1);
return true; return true;
} else if (mType == StorageItemType::Binary) { }
return false;
}
bool StorageItem::getItemSizeBinary(uint32_t &outSize) const {
if (mType == StorageItemType::Binary) {
outSize = std::get<std::vector<uint8_t>>(mData).size(); outSize = std::get<std::vector<uint8_t>>(mData).size();
return true; return true;
} }

View File

@ -73,7 +73,9 @@ public:
return mKey; return mKey;
} }
bool getItemSize(uint32_t &outSize) const; bool getItemSizeString(uint32_t &outSize) const;
bool getItemSizeBinary(uint32_t &outSize) const;
bool attemptBinaryConversion(); bool attemptBinaryConversion();

View File

@ -400,7 +400,7 @@ namespace StorageUtils {
} }
WUPSStorageError err = Helper::LoadFromFile(plugin_id, *root); 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 // Create new clean StorageItemRoot if no existing storage was found
root = make_unique_nothrow<StorageItemRoot>(plugin_id); root = make_unique_nothrow<StorageItemRoot>(plugin_id);
} else if (err != WUPS_STORAGE_ERROR_SUCCESS) { } else if (err != WUPS_STORAGE_ERROR_SUCCESS) {
@ -512,10 +512,13 @@ namespace StorageUtils {
return WUPS_STORAGE_ERROR_NOT_FOUND; 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) { if (!outSize) {
return WUPS_STORAGE_ERROR_INVALID_ARGS; 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); std::lock_guard lock(gStorageMutex);
auto subItem = StorageUtils::Helper::getSubItem(root, parent); auto subItem = StorageUtils::Helper::getSubItem(root, parent);
if (!subItem) { if (!subItem) {
@ -523,15 +526,24 @@ namespace StorageUtils {
} }
auto item = subItem->getItem(key); auto item = subItem->getItem(key);
if (item) { if (item) {
// Trigger potential string -> binary conversion. if (itemType == WUPS_STORAGE_ITEM_BINARY) {
if (!item->attemptBinaryConversion()) { // Trigger potential string -> binary conversion.
return WUPS_STORAGE_ERROR_MALLOC_FAILED; if (!item->attemptBinaryConversion()) {
return WUPS_STORAGE_ERROR_MALLOC_FAILED;
}
} }
uint32_t tmp = 0; 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; *outSize = tmp;
return WUPS_STORAGE_ERROR_SUCCESS; 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_UNEXPECTED_DATA_TYPE;
} }
return WUPS_STORAGE_ERROR_NOT_FOUND; return WUPS_STORAGE_ERROR_NOT_FOUND;

View File

@ -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 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 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 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 } // namespace StorageUtils::API