diff --git a/frameworks/bridge/declarative_frontend/jsview/js_sceneview.cpp b/frameworks/bridge/declarative_frontend/jsview/js_sceneview.cpp index cbbe5dde9b9..d86482c3557 100644 --- a/frameworks/bridge/declarative_frontend/jsview/js_sceneview.cpp +++ b/frameworks/bridge/declarative_frontend/jsview/js_sceneview.cpp @@ -13,6 +13,7 @@ * limitations under the License. */ +#include "parse_resource_uint32.h" #include "frameworks/bridge/declarative_frontend/jsview/js_sceneview.h" #include @@ -74,13 +75,17 @@ bool GetResourceId(const std::string& uri, uint32_t& resId) { std::smatch matches; if (std::regex_match(uri, matches, MODEL_RES_ID_REGEX) && matches.size() == MODEL_RESOURCE_MATCH_SIZE) { - resId = static_cast(std::stoul(matches[1].str())); + if (!ParseResourceUint32(matches[1].str(), resId)) { + return false; + } return true; } std::smatch appMatches; if (std::regex_match(uri, appMatches, MODEL_APP_RES_ID_REGEX) && appMatches.size() == MODEL_RESOURCE_MATCH_SIZE) { - resId = static_cast(std::stoul(appMatches[1].str())); + if (!ParseResourceUint32(appMatches[1].str(), resId)) { + return false; + } return true; } return false; diff --git a/frameworks/bridge/declarative_frontend/jsview/parse_resource_uint32.h b/frameworks/bridge/declarative_frontend/jsview/parse_resource_uint32.h new file mode 100644 index 00000000000..f081b118691 --- /dev/null +++ b/frameworks/bridge/declarative_frontend/jsview/parse_resource_uint32.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2026 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef PARSE_RESOURCE_UINT32_H +#define PARSE_RESOURCE_UINT32_H + +#include +#include +#include +#include + +inline bool ParseResourceUint32(const std::string &s, uint32_t &out) +{ + if (s.empty()) { + return false; + } + uint32_t value = 0; + const char *first = s.data(); + const char *last = first + s.size(); + auto result = std::from_chars(first, last, value); + if (result.ec != std::errc() || result.ptr != last) { + return false; + } + out = value; + return true; +} + +#endif diff --git a/frameworks/core/interfaces/native/implementation/component3d_modifier.cpp b/frameworks/core/interfaces/native/implementation/component3d_modifier.cpp index c3dece07f60..75afef02c60 100644 --- a/frameworks/core/interfaces/native/implementation/component3d_modifier.cpp +++ b/frameworks/core/interfaces/native/implementation/component3d_modifier.cpp @@ -13,6 +13,7 @@ * limitations under the License. */ +#include "parse_resource_uint32.h" #include "core/components_ng/base/frame_node.h" #include "core/interfaces/native/utility/converter.h" #if defined(MODEL_COMPONENT_SUPPORTED) @@ -73,13 +74,17 @@ bool GetResourceId(const std::string& uri, uint32_t& resId) { std::smatch matches; if (std::regex_match(uri, matches, MODEL_RES_ID_REGEX) && matches.size() == MODEL_RESOURCE_MATCH_SIZE) { - resId = static_cast(std::stoul(matches[1].str())); + if (!ParseResourceUint32(matches[1].str(), resId)) { + return false; + } return true; } std::smatch appMatches; if (std::regex_match(uri, appMatches, MODEL_APP_RES_ID_REGEX) && appMatches.size() == MODEL_RESOURCE_MATCH_SIZE) { - resId = static_cast(std::stoul(appMatches[1].str())); + if (!ParseResourceUint32(appMatches[1].str(), resId)) { + return false; + } return true; } return false; diff --git a/frameworks/core/interfaces/native/implementation/parse_resource_uint32.h b/frameworks/core/interfaces/native/implementation/parse_resource_uint32.h new file mode 100644 index 00000000000..f081b118691 --- /dev/null +++ b/frameworks/core/interfaces/native/implementation/parse_resource_uint32.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2026 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef PARSE_RESOURCE_UINT32_H +#define PARSE_RESOURCE_UINT32_H + +#include +#include +#include +#include + +inline bool ParseResourceUint32(const std::string &s, uint32_t &out) +{ + if (s.empty()) { + return false; + } + uint32_t value = 0; + const char *first = s.data(); + const char *last = first + s.size(); + auto result = std::from_chars(first, last, value); + if (result.ec != std::errc() || result.ptr != last) { + return false; + } + out = value; + return true; +} + +#endif diff --git a/test/hosttest/parse_resource_uint32.h b/test/hosttest/parse_resource_uint32.h new file mode 100644 index 00000000000..f081b118691 --- /dev/null +++ b/test/hosttest/parse_resource_uint32.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2026 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef PARSE_RESOURCE_UINT32_H +#define PARSE_RESOURCE_UINT32_H + +#include +#include +#include +#include + +inline bool ParseResourceUint32(const std::string &s, uint32_t &out) +{ + if (s.empty()) { + return false; + } + uint32_t value = 0; + const char *first = s.data(); + const char *last = first + s.size(); + auto result = std::from_chars(first, last, value); + if (result.ec != std::errc() || result.ptr != last) { + return false; + } + out = value; + return true; +} + +#endif diff --git a/test/hosttest/parse_resource_uint32_host_test.cpp b/test/hosttest/parse_resource_uint32_host_test.cpp new file mode 100644 index 00000000000..2b43b420894 --- /dev/null +++ b/test/hosttest/parse_resource_uint32_host_test.cpp @@ -0,0 +1,45 @@ +#include "../parse_resource_uint32.h" +#include +#include +#include +#include + +static void Expect(bool cond, const char *msg) +{ + if (!cond) { + std::fprintf(stderr, "FAIL: %s\n", msg); + std::exit(1); + } +} + +int main() +{ + uint32_t v = 99; + Expect(!ParseResourceUint32("", v), "empty"); + Expect(v == 99, "empty-unchanged"); + + Expect(!ParseResourceUint32("abc", v), "abc"); + Expect(v == 99, "abc-unchanged"); + + Expect(!ParseResourceUint32("12a", v), "12a"); + Expect(v == 99, "12a-unchanged"); + + Expect(!ParseResourceUint32("4294967296", v), "overflow"); + Expect(v == 99, "overflow-unchanged"); + + Expect(!ParseResourceUint32("-1", v), "neg"); + Expect(v == 99, "neg-unchanged"); + + Expect(!ParseResourceUint32(" 1", v), "lead-space"); + Expect(v == 99, "lead-space-unchanged"); + + Expect(!ParseResourceUint32("99999999999999999999", v), "huge"); + Expect(v == 99, "huge-unchanged"); + + Expect(ParseResourceUint32("0", v) && v == 0, "zero"); + Expect(ParseResourceUint32("42", v) && v == 42, "res-id"); + Expect(ParseResourceUint32("4294967295", v) && v == std::numeric_limits::max(), "u32-max"); + + std::puts("ok"); + return 0; +}