!18991 merge fixjsontocpp into master

change json to cpp

Created-by: zhongshield1
Commit-by: zhangzezhong
Merged-by: openharmony_ci
Description: **IssueNo**:https://gitcode.com/openharmony/ability_ability_base/issues/770

**Description**:

**稳定性自检:**
| 自检项                                                       | 自检结果  |
| ------------------------------------------------------------ | -------- |
| 涉及跨进程调用的相关操作需要抛至主线程或加锁防止并发              |          |
| 成员变量进行赋值或创建需要排查并发                               |          |
| 谨慎在lambda表达式中使用引用捕获                                |          |
| 谨慎在未经拷贝的情况下使用外部传入的string、C字符串               |          |
| map\vector\list\set等stl模板类使用时需要排查并发                |          |
| 谨慎考虑加锁范围                                               |          |
| 在IPC通信中谨慎使用同步通信方式                                 |          |
| 禁止传递this指针至其他模块或线程(特别是eventhandler任务)        |          |
| 禁止将外部传入的裸指针在内部直接构造智能指针                      |          |
| 禁止多个独立创建的智能指针管理同一地址                           |          |
| 禁止在析构函数中抛异步任务                                      |          |
| 禁止js对象在非js线程(例如在IPC线程)创建、使用或销毁             |          |
| 禁止在对外接口中未经判空直接使用外部传入的指针                    |          |
| 禁止接口返回局部变量引用                                        |          |
| 禁止在信号函数中加锁                                            |          |
| 禁止在关键流程(SA启动、应用启动等主流程)执行耗时的操作           |          |
| 禁止将同一个cpp编译在不同的so中                                 |          |

**安全编码自检:**
| 自检项                                                          | 自检结果 |
| -------------------------------------------------------------- | -------- |
| 裸指针避免通过隐式转换构造为sptr                                 |          |
| json对象在取值之前必须先判断类型,避免类型不匹配                   |          |
| 序列化时必须对传入的数组大小进行校验,避免出现超大数组              |          |
| 避免使用未明确位宽的整型,选择使用int8_t、uint8_t等类型            |          |
| 外部传入的路径要做规范化校验,对路径中的.、..、../等特殊字符严格校验 |          |
| 指针变量、表示资源描述符的变量、bool变量必须赋初值                  |          |
| readParcelable获取的对象使用前需要判空                            |          |
| 分配和释放内存的函数需要成对出现                                   |          |
| 申请内存后异常退出前需要及时进行内存释放                            |          |
| 内存申请前必须对内存大小进行合法性校验                              |          |
| 内存分配后必须判断是否成功                                         |          |
| 禁止使用realloc、alloca函数                                       |          |
| 禁止打印文件路径、口令等敏感信息,如有需要,使用private修饰          |          |
| 禁止打印内存地址                                                  |          |
| 整数之间运算时必须严格检查,确保不会出现溢出、反转、除0               |          |
| 禁止对有符号整数进行位操作符运算                                    |          |
| 禁止对指针进行逻辑或位运算                                         |          |
| 循环次数如果收外部数据控制,需要检验其合法性                         |          |
| 禁止使用内存操作类危险函数,需要使用安全函数                         |          |
| 谨慎使用不可重入函数                                               |          |
| 必须检查安全函数的返回值,并进行正确处理                             |          |
| 禁止仅通过TokenType类型判断绕过权限校验                             |          |

**TDD Result**:

**XTS Result**:

### 是否已执行L0用例
- [ ] 已验证
- [ ] 不涉及。如不涉及,请写明理由


See merge request: openharmony/ability_ability_runtime!18991
This commit is contained in:
openharmony_ci
2026-04-24 17:31:43 +08:00
12 changed files with 234 additions and 172 deletions
+1
View File
@@ -61,6 +61,7 @@ ohos_shared_library("ani_common") {
"src/ani_common_execute_param.cpp",
"src/ani_common_execute_result.cpp",
"src/ani_common_intent_info_filter.cpp",
"src/ani_common_json_util.cpp",
"src/ani_common_start_options.cpp",
"src/ani_common_query_entity_param.cpp",
"src/ani_common_util.cpp",
@@ -0,0 +1,30 @@
/*
* 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 OHOS_ABILITY_RUNTIME_ANI_COMMON_JSON_UTIL_H
#define OHOS_ABILITY_RUNTIME_ANI_COMMON_JSON_UTIL_H
#include "ani.h"
#include "nlohmann/json.hpp"
namespace OHOS {
namespace AppExecFwk {
bool CreateEmptyAniRecord(ani_env *env, ani_object &recordObject);
bool CreateAniArrayFromJson(ani_env *env, const nlohmann::json &jsonArray, ani_object &arrayObject);
bool CreateAniRecordFromJson(ani_env *env, const nlohmann::json &jsonObject, ani_object &recordObject);
} // namespace AppExecFwk
} // namespace OHOS
#endif // OHOS_ABILITY_RUNTIME_ANI_COMMON_JSON_UTIL_H
@@ -41,9 +41,6 @@ ani_object WrapElementNameInner(ani_env *env, ani_class elementNameObj, ani_obje
const AppExecFwk::ElementName &elementNameParam);
ani_object CreateAniWant(ani_env *env, const AAFwk::Want &want);
bool CreateArrayFromJson(ani_env *env, const nlohmann::json &jsonArray, ani_object &arrayObject);
bool CreateRecordObjectFromJson(ani_env *env,
const nlohmann::json &jsonObject, ani_object &recordObject);
} // namespace AppExecFwk
} // namespace OHOS
#endif // OHOS_ABILITY_RUNTIME_ANI_COMMON_WANT_H
@@ -0,0 +1,182 @@
/*
* 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.
*/
#include "ani_common_json_util.h"
#include "ani_common_cache_mgr.h"
#include "ani_common_util.h"
#include "hilog_tag_wrapper.h"
namespace OHOS {
namespace AppExecFwk {
namespace {
constexpr const char *CLASSNAME_ARRAY = "std.core.Array";
constexpr const char *RECORD_SET_NAME =
"X{C{std.core.Numeric}C{std.core.String}C{std.core.BaseEnum}}Y:";
bool SetRecordValue(ani_env *env, ani_object recordObject, ani_string key, ani_object value)
{
ani_class recordCls = nullptr;
ani_method recordSetMethod = nullptr;
AniCommonMethodCacheKey recordSet = std::make_pair("$_set", RECORD_SET_NAME);
if (!AniCommonCacheMgr::GetCachedClassAndMethod(env, CLASSNAME_RECORD, recordSet,
recordCls, recordSetMethod)) {
TAG_LOGE(AAFwkTag::ANI, "failed to get cached class and method for record $_set");
return false;
}
ani_status status = env->Object_CallMethod_Void(recordObject, recordSetMethod, key, value);
if (status != ANI_OK) {
TAG_LOGE(AAFwkTag::ANI, "Object_CallMethod_Void failed status: %{public}d", status);
return false;
}
return true;
}
bool CreateAniValueFromJson(ani_env *env, const nlohmann::json &jsonValue, ani_object &aniValue)
{
if (jsonValue.is_object()) {
return CreateAniRecordFromJson(env, jsonValue, aniValue);
}
if (jsonValue.is_array()) {
return CreateAniArrayFromJson(env, jsonValue, aniValue);
}
if (jsonValue.is_string()) {
aniValue = GetAniString(env, jsonValue.get<std::string>());
return aniValue != nullptr;
}
if (jsonValue.is_boolean()) {
aniValue = CreateBoolean(env, jsonValue.get<bool>());
return aniValue != nullptr;
}
if (jsonValue.is_number()) {
aniValue = CreateDouble(env, jsonValue.get<double>());
return aniValue != nullptr;
}
if (jsonValue.is_null()) {
return true;
}
TAG_LOGW(AAFwkTag::ANI, "unsupported json value type");
return false;
}
} // namespace
bool CreateEmptyAniRecord(ani_env *env, ani_object &recordObject)
{
ani_class recordCls = nullptr;
ani_method recordCtorMethod = nullptr;
AniCommonMethodCacheKey recordCtor = std::make_pair("<ctor>", ":");
if (!AniCommonCacheMgr::GetCachedClassAndMethod(env, CLASSNAME_RECORD, recordCtor,
recordCls, recordCtorMethod)) {
TAG_LOGE(AAFwkTag::ANI, "failed to get cached class and method for record ctor");
return false;
}
ani_status status = env->Object_New(recordCls, recordCtorMethod, &recordObject);
if (status != ANI_OK) {
TAG_LOGE(AAFwkTag::ANI, "Object_New failed: %{public}d", status);
return false;
}
return true;
}
bool CreateAniArrayFromJson(ani_env *env, const nlohmann::json &jsonArray, ani_object &arrayObject)
{
if (!jsonArray.is_array()) {
TAG_LOGE(AAFwkTag::ANI, "json is not array");
return false;
}
ani_class arrayCls = nullptr;
ani_status status = env->FindClass(CLASSNAME_ARRAY, &arrayCls);
if (status != ANI_OK || arrayCls == nullptr) {
TAG_LOGE(AAFwkTag::ANI, "failed to find array class, status: %{public}d", status);
return false;
}
ani_method arrayCtor = nullptr;
status = env->Class_FindMethod(arrayCls, "<ctor>", "i:", &arrayCtor);
if (status != ANI_OK || arrayCtor == nullptr) {
TAG_LOGE(AAFwkTag::ANI, "failed to find array constructor, status: %{public}d", status);
return false;
}
status = env->Object_New(arrayCls, arrayCtor, &arrayObject, jsonArray.size());
if (status != ANI_OK || arrayObject == nullptr) {
TAG_LOGE(AAFwkTag::ANI, "failed to create array object, status: %{public}d", status);
return false;
}
ani_size index = 0;
for (const auto &item : jsonArray) {
ani_object elementValue = nullptr;
if (!CreateAniValueFromJson(env, item, elementValue)) {
TAG_LOGE(AAFwkTag::ANI, "failed to create array element");
index++;
continue;
}
if (elementValue != nullptr) {
status = env->Object_CallMethodByName_Void(arrayObject, "$_set", "iY:", index, elementValue);
if (status != ANI_OK) {
TAG_LOGE(AAFwkTag::ANI, "failed to set array element, status: %{public}d", status);
return false;
}
}
index++;
}
return true;
}
bool CreateAniRecordFromJson(ani_env *env, const nlohmann::json &jsonObject, ani_object &recordObject)
{
if (env == nullptr) {
TAG_LOGE(AAFwkTag::ANI, "null env");
return false;
}
if (!jsonObject.is_object()) {
TAG_LOGE(AAFwkTag::ANI, "json is not object");
return false;
}
if (!CreateEmptyAniRecord(env, recordObject)) {
TAG_LOGE(AAFwkTag::ANI, "failed to create record object");
return false;
}
for (const auto &item : jsonObject.items()) {
const std::string &key = item.key();
const nlohmann::json &value = item.value();
ani_string aniKey = GetAniString(env, key);
if (aniKey == nullptr) {
TAG_LOGE(AAFwkTag::ANI, "failed to create key string: %{public}s", key.c_str());
continue;
}
ani_object aniValue = nullptr;
if (!CreateAniValueFromJson(env, value, aniValue)) {
TAG_LOGE(AAFwkTag::ANI, "failed to create value for key: %{public}s", key.c_str());
continue;
}
if (aniValue == nullptr) {
continue;
}
if (!SetRecordValue(env, recordObject, aniKey, aniValue)) {
TAG_LOGE(AAFwkTag::ANI, "failed to set record for key: %{public}s", key.c_str());
return false;
}
}
return true;
}
} // namespace AppExecFwk
} // namespace OHOS
@@ -15,6 +15,7 @@
#include "ani_common_want.h"
#include "ani_common_cache_mgr.h"
#include "ani_common_json_util.h"
#include "ani_common_util.h"
#include "remote_object_taihe_ani.h"
#include "array_wrapper.h"
@@ -1078,102 +1079,6 @@ bool InnerWrapWantParamsArray(
}
}
bool InnerCreateArrayRecordFromJson(ani_env *env, const nlohmann::json &jsonArray, ani_object &arrayObject)
{
ani_size index = 0;
for (const auto &item : jsonArray) {
ani_object elementValue = nullptr;
if (item.is_object()) {
if (!CreateRecordObjectFromJson(env, item, elementValue)) {
TAG_LOGE(AAFwkTag::ANI, "failed to create object element");
continue;
}
} else if (item.is_array()) {
if (!CreateArrayFromJson(env, item, elementValue)) {
TAG_LOGE(AAFwkTag::ANI, "failed to create array element");
continue;
}
} else if (item.is_string()) {
elementValue = GetAniString(env, item.get<std::string>());
} else if (item.is_boolean()) {
if (!InnerCreateBooleanObject(env, item.get<bool>(), elementValue)) {
TAG_LOGE(AAFwkTag::ANI, "failed to create boolean element");
continue;
}
} else if (item.is_number()) {
if (!InnerCreateDoubleObject(env, item.get<double>(), elementValue)) {
TAG_LOGE(AAFwkTag::ANI, "failed to create double element");
continue;
}
}
if (elementValue != nullptr) {
ani_status status = env->Object_CallMethodByName_Void(arrayObject, "$_set",
"iY:", index, elementValue);
if (status != ANI_OK) {
TAG_LOGE(AAFwkTag::ANI, "failed to set array element, status: %{public}d", status);
return false;
}
}
index++;
}
return true;
}
bool InnerFillRecordFromJson(ani_env *env, const nlohmann::json &jsonObject, ani_object &recordObject)
{
for (const auto &item : jsonObject.items()) {
const std::string &key = item.key();
const nlohmann::json &value = item.value();
ani_string aniKey = GetAniString(env, key);
if (aniKey == nullptr) {
TAG_LOGE(AAFwkTag::ANI, "failed to create key string: %{public}s", key.c_str());
continue;
}
ani_object aniValue = nullptr;
if (value.is_object()) {
if (!CreateRecordObjectFromJson(env, value, aniValue)) {
TAG_LOGE(AAFwkTag::ANI,
"failed to create nested record object for key: %{public}s", key.c_str());
continue;
}
} else if (value.is_string()) {
aniValue = GetAniString(env, value.get<std::string>());
} else if (value.is_boolean()) {
if (!InnerCreateBooleanObject(env, value.get<bool>(), aniValue)) {
TAG_LOGE(AAFwkTag::ANI, "failed to create boolean object for key: %{public}s", key.c_str());
continue;
}
} else if (value.is_number()) {
if (!InnerCreateDoubleObject(env, value.get<double>(), aniValue)) {
TAG_LOGE(AAFwkTag::ANI, "failed to create double object for key: %{public}s", key.c_str());
continue;
}
} else if (value.is_array()) {
if (!CreateArrayFromJson(env, value, aniValue)) {
TAG_LOGE(AAFwkTag::ANI,
"failed to create array object for key: %{public}s", key.c_str());
continue;
}
} else {
TAG_LOGW(AAFwkTag::ANI, "unsupported json type for key: %{public}s", key.c_str());
continue;
}
if (aniValue == nullptr) {
TAG_LOGE(AAFwkTag::ANI, "null value object for key: %{public}s", key.c_str());
continue;
}
if (!InnerSetRecord(env, recordObject, aniKey, aniValue)) {
TAG_LOGE(AAFwkTag::ANI, "failed to set record for key: %{public}s", key.c_str());
return false;
}
}
return true;
}
}
ani_object WrapWant(ani_env *env, const AAFwk::Want &want)
@@ -1270,55 +1175,6 @@ ani_ref WrapWantParamsFD(ani_env *env, const AAFwk::WantParams &wantParams)
return WrapWantParams(env, fds);
}
bool CreateArrayFromJson(ani_env *env, const nlohmann::json &jsonArray, ani_object &arrayObject)
{
if (!jsonArray.is_array()) {
TAG_LOGE(AAFwkTag::ANI, "json is not array");
return false;
}
ani_class arrayCls = nullptr;
ani_status status = env->FindClass("std.core.Array", &arrayCls);
if (status != ANI_OK || arrayCls == nullptr) {
TAG_LOGE(AAFwkTag::ANI, "failed to find array class, status: %{public}d", status);
return false;
}
ani_method arrayCtor = nullptr;
status = env->Class_FindMethod(arrayCls, "<ctor>", "i:", &arrayCtor);
if (status != ANI_OK || arrayCtor == nullptr) {
TAG_LOGE(AAFwkTag::ANI, "failed to find array constructor, status: %{public}d", status);
return false;
}
status = env->Object_New(arrayCls, arrayCtor, &arrayObject, jsonArray.size());
if (status != ANI_OK || arrayObject == nullptr) {
TAG_LOGE(AAFwkTag::ANI, "failed to create array object, status: %{public}d", status);
return false;
}
if (!InnerCreateArrayRecordFromJson(env, jsonArray, arrayObject)) {
TAG_LOGE(AAFwkTag::ANI, "failed to create array record object from json");
return false;
}
return true;
}
bool CreateRecordObjectFromJson(ani_env *env, const nlohmann::json &jsonObject, ani_object &recordObject)
{
if (env == nullptr) {
TAG_LOGE(AAFwkTag::INTENT, "null env");
return false;
}
if (!InnerCreateRecordObject(env, recordObject)) {
TAG_LOGE(AAFwkTag::ANI, "failed to create record object");
return false;
}
return InnerFillRecordFromJson(env, jsonObject, recordObject);
}
ani_ref WrapWantParams(ani_env *env, const AAFwk::WantParams &wantParams)
{
if (env == nullptr) {
@@ -21,6 +21,7 @@
#include "ets_runtime.h"
#include "insight_intent_execute_result.h"
#include "insight_intent_info_for_query.h"
#include "nlohmann/json.hpp"
#include "want_params.h"
#include "want_params_wrapper.h"
@@ -45,7 +46,6 @@ ani_object CreateEtsConfigPutParams(ani_env *env, const std::vector<std::string>
ani_object CreateEtsConfigIntentInfo(ani_env *env, const InsightIntentInfoForQuery &info);
ani_object CreateInsightIntentInfoParam(ani_env *env, const std::string &paramStr);
ani_object CreateInsightIntentInfoWithJson(ani_env *env, const nlohmann::json &jsonObject);
bool CreateEmptyRecordObject(ani_env *env, ani_object &recordObject);
void SetInsightIntentInfo(ani_env *env, ani_object objValue, const InsightIntentInfoForQuery &info);
} // namespace AbilityRuntime
@@ -18,9 +18,8 @@
#include <cstdint>
#include "ability_state.h"
#include "ani_common_cache_mgr.h"
#include "ani_common_json_util.h"
#include "ani_common_util.h"
#include "ani_common_want.h"
#include "ani_enum_convert.h"
#include "hilog_tag_wrapper.h"
@@ -455,45 +454,38 @@ ani_object CreateEtsFormInfoForQuery(ani_env *env, const FormInfoForQuery &info)
return objValue;
}
bool CreateEmptyRecordObject(ani_env *env, ani_object &recordObject)
{
ani_class recordCls = nullptr;
ani_method recordCtorMethod = nullptr;
AppExecFwk::AniCommonMethodCacheKey recordCtor = std::make_pair("<ctor>", ":");
if (!AppExecFwk::AniCommonCacheMgr::GetCachedClassAndMethod(env, "std.core.Record", recordCtor,
recordCls, recordCtorMethod)) {
return false;
}
ani_status status = env->Object_New(recordCls, recordCtorMethod, &recordObject);
if (status != ANI_OK) {
TAG_LOGE(AAFwkTag::ANI, "Object_New failed: %{public}d", status);
return false;
}
return true;
}
ani_object CreateInsightIntentInfoParam(ani_env *env, const std::string &paramStr)
{
ani_object recordObject;
if (paramStr.empty()) {
TAG_LOGE(AAFwkTag::INTENT, "paramStr empty");
CreateEmptyRecordObject(env, recordObject);
AppExecFwk::CreateEmptyAniRecord(env, recordObject);
return recordObject;
}
nlohmann::json jsonObject = nlohmann::json::parse(paramStr, nullptr, false);
if (jsonObject.is_discarded()) {
TAG_LOGE(AAFwkTag::INTENT, "Parse param str fail");
CreateEmptyRecordObject(env, recordObject);
AppExecFwk::CreateEmptyAniRecord(env, recordObject);
return recordObject;
}
if (!AppExecFwk::CreateRecordObjectFromJson(env, jsonObject, recordObject)) {
if (!AppExecFwk::CreateAniRecordFromJson(env, jsonObject, recordObject)) {
TAG_LOGE(AAFwkTag::INTENT, "failed to create record object from json");
return nullptr;
}
return recordObject;
}
ani_object CreateInsightIntentInfoWithJson(ani_env *env, const nlohmann::json &jsonObject)
{
ani_object recordObject = nullptr;
if (!AppExecFwk::CreateAniRecordFromJson(env, jsonObject, recordObject)) {
TAG_LOGE(AAFwkTag::INTENT, "failed to create InsightIntent record object from JSON");
return nullptr;
}
return recordObject;
}
ani_object CreateEtsInsightIntentInfoForQueryArray(ani_env *env, const std::vector<InsightIntentInfoForQuery> &infos)
{
ani_class arrayCls = nullptr;
@@ -45,6 +45,7 @@ ohos_shared_library("insightintentdriver_napi") {
"hilog:libhilog",
"ipc:ipc_core",
"ipc:ipc_napi",
"json:nlohmann_json_static",
"napi:ace_napi",
]
cflags_cc = []
@@ -19,6 +19,7 @@
#include "insight_intent_execute_result.h"
#include "insight_intent_info_for_query.h"
#include "native_engine/native_engine.h"
#include "nlohmann/json.hpp"
namespace OHOS {
namespace AbilityRuntime {
@@ -19,6 +19,7 @@
#include <string>
#include "mission_info.h"
#include "nlohmann/json.hpp"
namespace OHOS {
namespace AAFwk {
@@ -20,7 +20,7 @@
#include <vector>
#include "insight_intent_execute_param.h"
#include "nlohmann/json.hpp"
namespace OHOS {
namespace AbilityRuntime {
using ExecuteMode = AppExecFwk::ExecuteMode;
@@ -20,6 +20,7 @@
#include <vector>
#include "insight_intent_execute_param.h"
#include "nlohmann/json.hpp"
namespace OHOS {
namespace AbilityRuntime {