fixDrawableStoul

Signed-off-by: hw_wyx <wuyinxiao@huawei.com>
Change-Id: I745d5d7e28bce1d752869539f87e2b2bdda38e7e
This commit is contained in:
hw_wyx 2024-10-16 14:59:34 +08:00
parent 910d550742
commit 091af71ca4

View File

@ -16,6 +16,10 @@
#if defined(ANDROID_PLATFORM) || defined(IOS_PLATFORM)
#include "application_context.h"
#endif
#include <cstdlib>
#include <cerrno>
#include <limits>
#include "drawable_descriptor.h"
#include "third_party/cJSON/cJSON.h"
#ifndef PREVIEW
@ -61,6 +65,7 @@ inline bool NearEqual(const double left, const double right)
#endif
const int DEFAULT_DURATION = 1000;
const std::string DEFAULT_MASK = "ohos_icon_mask";
constexpr int DECIMAL_BASE = 10;
// define for get resource path in preview scenes
const static char PREVIEW_LOAD_RESOURCE_ID[] = "ohos_drawable_descriptor_path";
@ -82,6 +87,23 @@ inline bool IsNumber(const std::string& value)
}
return std::all_of(value.begin(), value.end(), [](char i) { return isdigit(i); });
}
bool ConvertStringToUInt32(const std::string& idStr, uint32_t& result)
{
char* endPtr = nullptr;
errno = 0;
result = std::strtoul(idStr.c_str(), &endPtr, DECIMAL_BASE);
if (errno == ERANGE || result > std::numeric_limits<uint32_t>::max()) {
HILOGE("Out of range: string value is too large for uint32_t");
return false;
}
if (endPtr == idStr.c_str() || *endPtr != '\0') {
HILOGE("Invalid argument: unable to convert string to uint32_t");
return false;
}
return true;
}
} // namespace
DrawableItem LayeredDrawableDescriptor::PreGetDrawableItem(
@ -95,10 +117,11 @@ DrawableItem LayeredDrawableDescriptor::PreGetDrawableItem(
}
std::tuple<std::string, size_t, std::string> info;
auto state = resourceMgr->GetDrawableInfoById(
static_cast<uint32_t>(std::stoul(idStr)), info, resItem.data_, iconType_, density_);
uint32_t resourceId = 0;
if (ConvertStringToUInt32(idStr, resourceId)) {
resItem.state_ = resourceMgr->GetDrawableInfoById(resourceId, info, resItem.data_, iconType_, density_);
}
resItem.len_ = std::get<1>(info);
resItem.state_ = state;
return resItem;
}