LCOV - code coverage report
Current view: top level - acl/common - json_parser.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 84.3 % 312 263
Test Date: 2026-08-06 15:29:52 Functions: 100.0 % 13 13

            Line data    Source code
       1              : /**
       2              :  * Copyright (c) 2025 Huawei Technologies Co., Ltd.
       3              :  * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
       4              :  * CANN Open Software License Agreement Version 2.0 (the "License").
       5              :  * Please refer to the License for details. You may not use this file except in compliance with the License.
       6              :  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
       7              :  * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
       8              :  * See LICENSE in the root of the software repository for the full text of the License.
       9              :  */
      10              : 
      11              : #include "json_parser.h"
      12              : 
      13              : #include <fstream>
      14              : #include <sstream>
      15              : #include <regex>
      16              : #include <sys/stat.h>
      17              : #include "mmpa/mmpa_api.h"
      18              : 
      19              : namespace {
      20              : const std::string ACL_JSON_DEFAULT_DEVICE = "defaultDevice";
      21              : const std::string ACL_JSON_DEFAULT_DEVICE_ID = "default_device";
      22              : constexpr int32_t DECIMAL = 10;
      23              : 
      24        23404 : void CountDepth(const char_t ch, size_t &objDepth, size_t &maxObjDepth, size_t &arrayDepth, size_t &maxArrayDepth)
      25              : {
      26        23404 :     switch (ch) {
      27          478 :         case '{': {
      28          478 :             ++objDepth;
      29          478 :             if (objDepth > maxObjDepth) {
      30          452 :                 maxObjDepth = objDepth;
      31              :             }
      32          478 :             break;
      33              :         }
      34          476 :         case '}': {
      35          476 :             if (objDepth > 0) {
      36          476 :                 --objDepth;
      37              :             }
      38          476 :             break;
      39              :         }
      40           73 :         case '[': {
      41           73 :             ++arrayDepth;
      42           73 :             if (arrayDepth > maxArrayDepth) {
      43           61 :                 maxArrayDepth = arrayDepth;
      44              :             }
      45           73 :             break;
      46              :         }
      47           73 :         case ']': {
      48           73 :             if (arrayDepth > 0) {
      49           73 :                 --arrayDepth;
      50              :             }
      51           73 :             break;
      52              :         }
      53        22304 :         default: {
      54        22304 :             return;
      55              :         }
      56              :     }
      57              : }
      58              : } // namespace
      59              : namespace acl {
      60              :     // 配置文件最大字节数目10MBytes
      61              :     constexpr int64_t MAX_CONFIG_FILE_BYTE = 10 * 1024 * 1024;
      62              :     // 配置文件最大递归深度
      63              :     constexpr size_t MAX_CONFIG_OBJ_DEPTH = 10U;
      64              :     // 配置文件最大数组个数
      65              :     constexpr size_t MAX_CONFIG_ARRAY_DEPTH = 10U;
      66              : 
      67          257 :     bool JsonParser::IsValidFileName(const char_t *const fileName)
      68              :     {
      69          257 :         char_t trustedPath[MMPA_MAX_PATH] = {};
      70          257 :         int32_t ret = mmRealPath(fileName, trustedPath, MMPA_MAX_PATH);
      71          257 :         if (ret != EN_OK) {
      72            4 :             const auto formatErrMsg = acl::AclGetErrorFormatMessage(mmGetErrorCode());
      73            4 :             acl::AclErrorLogManager::ReportInputError(acl::INVALID_PATH_MSG,
      74            8 :                 std::vector<const char *>({"path", "reason"}),
      75            8 :                 std::vector<const char *>({fileName, formatErrMsg.c_str()}));
      76            4 :             ACL_LOG_ERROR("[Trans][RealPath]the file path %s is not like a real path, mmRealPath return %d, "
      77              :                 "errMessage is %s", fileName, ret, formatErrMsg.c_str());
      78            4 :             return false;
      79            4 :         }
      80              : 
      81              :         mmStat_t pathStat;
      82          253 :         ret = mmStatGet(trustedPath, &pathStat);
      83          253 :         if (ret != EN_OK) {
      84            0 :             const auto formatErrMsg = acl::AclGetErrorFormatMessage(mmGetErrorCode());
      85            0 :             acl::AclErrorLogManager::ReportInputError(acl::INVALID_PATH_MSG,
      86            0 :                 std::vector<const char *>({"path", "reason"}),
      87            0 :                 std::vector<const char *>({trustedPath, formatErrMsg.c_str()}));
      88            0 :             ACL_LOG_ERROR("[Get][FileStatus]cannot get config file status, which path is %s, "
      89              :                 "maybe does not exist, return %d, errcode %d", trustedPath, ret, mmGetErrorCode());
      90            0 :             return false;
      91            0 :         }
      92          253 :         if ((pathStat.st_mode & S_IFMT) != S_IFREG) {
      93            0 :             acl::AclErrorLogManager::ReportInputError(acl::INVALID_FILE_MSG,
      94            0 :                 std::vector<const char *>({"path", "reason"}),
      95            0 :                 std::vector<const char *>({trustedPath, "config file is not a regular file"}));
      96            0 :             ACL_LOG_ERROR("[Config][ConfigFile]config file is not a regular file, which path is %s, "
      97              :                 "mode is %u", trustedPath, pathStat.st_mode);
      98            0 :             return false;
      99              :         }
     100          253 :         if (pathStat.st_size > MAX_CONFIG_FILE_BYTE) {
     101              :             std::string reason = acl::AclErrorLogManager::FormatStr(
     102            0 :                 "file size %ld exceeds maximum allowed size %ld", pathStat.st_size, MAX_CONFIG_FILE_BYTE);
     103            0 :             acl::AclErrorLogManager::ReportInputError(acl::INVALID_FILE_MSG,
     104            0 :                 std::vector<const char *>({"path", "reason"}),
     105            0 :                 std::vector<const char *>({trustedPath, reason.c_str()}));
     106            0 :             ACL_LOG_ERROR("[Check][FileSize]config file %s size[%ld] is larger than "
     107              :                 "max config file Bytes[%ld]", trustedPath, pathStat.st_size, MAX_CONFIG_FILE_BYTE);
     108            0 :             return false;
     109            0 :         }
     110          253 :         return true;
     111              :     }
     112              : 
     113          245 :     void JsonParser::GetMaxNestedLayers(const char_t *const fileName, const size_t length,
     114              :         size_t &maxObjDepth, size_t &maxArrayDepth)
     115              :     {
     116          245 :         if (length <= 0) {
     117            1 :             ACL_LOG_ERROR("[Check][Length]the length of file %s must be larger than 0.", fileName);
     118            3 :             return;
     119              :         }
     120              : 
     121          244 :         char_t *pBuffer = new(std::nothrow) char_t[length];
     122          244 :         if (pBuffer == nullptr) {
     123            0 :             ACL_LOG_ERROR("[Check][Malloc]Allocate memory failed, bufferSize=%zu.", length);
     124            0 :             const std::string lengthVal = std::to_string(length);
     125            0 :             acl::AclErrorLogManager::ReportInputError(acl::ALLOC_MEMORY_FAILED_MSG,
     126            0 :                 std::vector<const char *>({"buf_size", "alloc_interface"}),
     127            0 :                 std::vector<const char *>({lengthVal.c_str(), "new"}));
     128            0 :             return;
     129            0 :         }
     130          488 :         const std::shared_ptr<char_t> buffer(pBuffer, [](char_t *const deletePtr) { delete[] deletePtr; });
     131              : 
     132          244 :         std::ifstream fin(fileName);
     133          244 :         if (!fin.is_open()) {
     134            1 :             ACL_LOG_INNER_ERROR("[Open][File]Read file %s failed.", fileName);
     135            1 :             return;
     136              :         }
     137          243 :         (void)fin.seekg(0, fin.beg);
     138          243 :         (void)fin.read(buffer.get(), static_cast<int64_t>(length));
     139              : 
     140          243 :         size_t arrayDepth = 0U;
     141          243 :         size_t objDepth = 0U;
     142        23647 :         for (size_t i = 0U; i < length; ++i) {
     143        23405 :             const char_t v = buffer.get()[i];
     144        23405 :             if (v == '\0') {
     145            1 :                 fin.close();
     146            1 :                 return;
     147              :             }
     148        23404 :             CountDepth(v, objDepth, maxObjDepth, arrayDepth, maxArrayDepth);
     149              :         }
     150          242 :         fin.close();
     151          246 :     }
     152              : 
     153          216 :     aclError JsonParser::ParseJson(const char_t* const fileName, const char_t *const configStr, nlohmann::json &js)
     154              :     {
     155          216 :         if (strlen(configStr) == 0UL) {
     156           11 :             ACL_LOG_DEBUG("buffer is empty, no need parse json.");
     157           11 :             return ACL_SUCCESS;
     158              :         }
     159              :         try {
     160          620 :             js = nlohmann::json::parse(std::string(configStr));
     161            5 :         } catch (const nlohmann::json::exception &e) {
     162            5 :             ACL_LOG_ERROR("[Check][JsonFile]invalid json buffer, exception:%s.", e.what());
     163            5 :             std::string reason = acl::AclErrorLogManager::FormatStr("Parse exception: %s", e.what());
     164            5 :             acl::AclErrorLogManager::ReportInputError(
     165           10 :                 acl::INVALID_FILE_MSG, std::vector<const char*>({"path", "reason"}),
     166           10 :                 std::vector<const char*>({fileName, reason.c_str()}));
     167            5 :             return ACL_ERROR_PARSE_FILE;
     168            5 :         }
     169          200 :         ACL_LOG_DEBUG("parse json from buffer successfully.");
     170          200 :         return ACL_SUCCESS;
     171              :     }
     172              : 
     173          258 :     aclError JsonParser::GetConfigStrFromFile(const char_t *const fileName, std::string &configStr)
     174              :     {
     175          258 :         if (fileName == nullptr) {
     176            1 :             ACL_LOG_DEBUG("filename is nullptr, no need to parse json");
     177            1 :             return ACL_SUCCESS;
     178              :         }
     179          257 :         ACL_LOG_DEBUG("before GetConfigStrFromFile in ParseJsonFromFile");
     180          257 :         if (!IsValidFileName(fileName)) {
     181            4 :             ACL_LOG_ERROR("[Check][File]invalid config file[%s]", fileName);
     182            4 :             return ACL_ERROR_INVALID_FILE;
     183              :         }
     184          253 :         std::ifstream fin(fileName, std::ios::binary);
     185          253 :         ACL_CHECK_INVALID_FILE_MSG_RET(!fin.is_open(), fileName, "File cannot be opened for reading", ACL_ERROR_INVALID_FILE);
     186          253 :         (void)fin.seekg(0, std::ios::end);
     187          253 :         const std::streampos fp = fin.tellg();
     188          253 :         if (static_cast<int32_t>(fp) == 0) {
     189           12 :             ACL_LOG_DEBUG("parse file is null");
     190           12 :             fin.close();
     191           12 :             return ACL_SUCCESS;
     192              :         }
     193              :         // checking the depth of file
     194          241 :         size_t maxObjDepth = 0U;
     195          241 :         size_t maxArrayDepth = 0U;
     196          241 :         GetMaxNestedLayers(fileName, static_cast<size_t>(fp), maxObjDepth, maxArrayDepth);
     197          241 :         if ((maxObjDepth > MAX_CONFIG_OBJ_DEPTH) || (maxArrayDepth > MAX_CONFIG_ARRAY_DEPTH)) {
     198              :             std::string reason = acl::AclErrorLogManager::FormatStr(
     199              :                 "object depth %zu exceeds max %zu or array depth %zu exceeds max %zu",
     200            0 :                 maxObjDepth, MAX_CONFIG_OBJ_DEPTH, maxArrayDepth, MAX_CONFIG_ARRAY_DEPTH);
     201            0 :             acl::AclErrorLogManager::ReportInputError(acl::INVALID_FILE_MSG,
     202            0 :                 std::vector<const char *>({"path", "reason"}),
     203            0 :                 std::vector<const char *>({fileName, reason.c_str()}));
     204            0 :             ACL_LOG_ERROR("[Check][MaxArrayDepth]invalid json file, the object's depth[%zu] is larger than %zu, "
     205              :                                 "or the array's depth[%zu] is larger than %zu.",
     206              :                                 maxObjDepth, MAX_CONFIG_OBJ_DEPTH, maxArrayDepth, MAX_CONFIG_ARRAY_DEPTH);
     207            0 :             fin.close();
     208            0 :             return ACL_ERROR_PARSE_FILE;
     209            0 :         }
     210          241 :         ACL_LOG_DEBUG("json file's obj's depth is %zu, array's depth is %zu", maxObjDepth, maxArrayDepth);
     211          241 :         std::stringstream buffer;
     212          241 :         fin.seekg(0, std::ios::beg);
     213          241 :         buffer << fin.rdbuf();
     214          241 :         configStr = buffer.str();
     215          241 :         fin.close();
     216          241 :         return ACL_SUCCESS;
     217          253 :     }
     218              : 
     219          219 :     aclError JsonParser::ParseJsonFromFile(const char_t *const fileName, nlohmann::json &js)
     220              :     {
     221          219 :         std::string configStr;
     222          219 :         auto ret = GetConfigStrFromFile(fileName, configStr);
     223          219 :         if (ret != ACL_SUCCESS) {
     224            3 :             ACL_LOG_ERROR("[Parse][File]Get Buffer from file[%s] failed.", fileName);
     225            3 :             return ret;
     226              :         }
     227              : 
     228          216 :         ret= ParseJson(fileName, configStr.c_str(), js);
     229          216 :         if (ret != ACL_SUCCESS) {
     230            5 :             ACL_LOG_ERROR("[Parse][File]parse config file[%s] to json failed.", fileName);
     231            5 :             return ACL_ERROR_PARSE_FILE;
     232              :         }
     233              : 
     234          211 :         ACL_LOG_DEBUG("parse json from file[%s] successfully.", fileName);
     235          211 :         return ACL_SUCCESS;
     236          219 :     }
     237              : 
     238           73 :     aclError JsonParser::GetJsonCtxByKey(const char_t *const fileName,
     239              :         std::string &strJsonCtx, const std::string &subStrKey, bool &found) {
     240           73 :         found = false;
     241           73 :         nlohmann::json js;
     242           73 :         aclError ret = acl::JsonParser::ParseJsonFromFile(fileName, js);
     243           73 :         if (ret != ACL_SUCCESS) {
     244            2 :             ACL_LOG_ERROR("parse json from file failed, errorCode = %d", ret);
     245            2 :             return ret;
     246              :         }
     247           71 :         const auto configIter = js.find(subStrKey);
     248           71 :         if (configIter != js.end()) {
     249           10 :             strJsonCtx = configIter->dump();
     250           10 :             found = true;
     251              :         }
     252           71 :         return ACL_SUCCESS;
     253           73 :     }
     254              : 
     255            7 :     aclError JsonParser::GetAttrConfigFromFile(
     256              :         const char_t *const fileName, std::map<aclCannAttr, CannInfo> &cannInfoMap)
     257              :     {
     258            7 :         nlohmann::json js;
     259            7 :         aclError ret = JsonParser::ParseJsonFromFile(fileName, js);
     260            7 :         if (ret != ACL_SUCCESS) {
     261            1 :             ACL_LOG_ERROR("parse swFeatureList.json from file[%s] failed, ret = %d", fileName, ret);
     262            1 :             return ret;
     263              :         }
     264              :         try {
     265           24 :             for (auto &item : cannInfoMap) {
     266           18 :                 auto &cannInfo = item.second;
     267           18 :                 const auto config = js.find(cannInfo.readableAttrName);
     268           18 :                 if (config != js.end()) {
     269           14 :                     const auto runtimeIter = config->find(SW_CONFIG_RUNTIME);
     270           14 :                     if (runtimeIter != config->end()) {
     271           14 :                         ACL_REQUIRES_OK(CannInfoUtils::ParseVersionValue(
     272              :                             runtimeIter->get<std::string>(), &cannInfo.minimumRuntimeVersion));
     273              :                     }
     274              :                 }
     275              :             }
     276            0 :         } catch (const nlohmann::json::exception &e) {
     277            0 :             std::string reason = acl::AclErrorLogManager::FormatStr("JSON parse exception: %s", e.what());
     278            0 :             acl::AclErrorLogManager::ReportInputError(acl::INVALID_FILE_MSG,
     279            0 :                 std::vector<const char *>({"path", "reason"}),
     280            0 :                 std::vector<const char *>({fileName, reason.c_str()}));
     281            0 :             ACL_LOG_ERROR("invalid config file [%s], exception: %s", fileName, e.what());
     282            0 :             return ACL_ERROR_INTERNAL_ERROR;
     283            0 :         }
     284            6 :         ACL_LOG_INFO("Finish parsing swFeatureList.json");
     285            6 :         return ACL_SUCCESS;
     286            7 :     }
     287              : 
     288           30 :     aclError JsonParser::GetDefaultDeviceIdFromFile(const char_t *const fileName, int32_t& devId)
     289              :     {
     290           30 :         ACL_LOG_DEBUG("start to execute GetDefaultDeviceIdFromFile.");
     291           30 :         nlohmann::json js;
     292           30 :         std::string enableFlagStr, defaultDeviceIdStr;
     293           30 :         aclError ret = acl::JsonParser::ParseJsonFromFile(fileName, js);
     294           30 :         if (ret != ACL_SUCCESS) {
     295            0 :             ACL_LOG_ERROR("[Parse][JsonFromFile]parse default config from file[%s] failed, errorCode = %d", fileName, ret);
     296            0 :             return ret;
     297              :         }
     298              : 
     299              :         try {
     300           30 :             if (!JsonParser::ContainKey(js, ACL_JSON_DEFAULT_DEVICE)) {
     301           21 :                 ACL_LOG_WARN("no defaultDevice item!");
     302           21 :                 return ACL_SUCCESS;
     303              :             }
     304            9 :             const nlohmann::json &jsDefaultDeviceConfig = JsonParser::GetCfgJsonByKey(js, ACL_JSON_DEFAULT_DEVICE);
     305            9 :             if (!JsonParser::ContainKey(jsDefaultDeviceConfig, ACL_JSON_DEFAULT_DEVICE_ID)) {
     306            1 :                 ACL_LOG_WARN("no default_device in acl.json!");
     307            1 :                 return ACL_SUCCESS;
     308              :             }
     309              : 
     310            8 :             defaultDeviceIdStr = JsonParser::GetCfgStrByKey(jsDefaultDeviceConfig, ACL_JSON_DEFAULT_DEVICE_ID);
     311            1 :         } catch (const nlohmann::json::exception &e) {
     312            1 :             std::string reason = acl::AclErrorLogManager::FormatStr("JSON parse exception: %s", e.what());
     313            1 :             acl::AclErrorLogManager::ReportInputError(acl::INVALID_FILE_MSG,
     314            2 :                 std::vector<const char *>({"path", "reason"}),
     315            2 :                 std::vector<const char *>({fileName, reason.c_str()}));
     316            1 :             ACL_LOG_ERROR("parse config file [%s], exception: %s", fileName, e.what());
     317            1 :             return ACL_ERROR_INTERNAL_ERROR;
     318            1 :         }
     319              : 
     320            7 :         std::regex reg("0|[1-9]\\d*");
     321            7 :         if (!std::regex_match(defaultDeviceIdStr, reg)) {
     322            3 :             acl::AclErrorLogManager::ReportInputError(acl::INVALID_PARAM_REASON_MSG,
     323            6 :                 std::vector<const char *>({"func", "value", "param", "reason"}),
     324            3 :                 std::vector<const char *>({"Parsing the default device ID from the configuration file", defaultDeviceIdStr.c_str(), "default_device",
     325            6 :                     "value must be zero or a positive integer"}));
     326            3 :             ACL_LOG_ERROR("default_device %s in acl.json is neither zero nor positive integer.",
     327              :                            defaultDeviceIdStr.c_str());
     328            3 :             return ACL_ERROR_INVALID_PARAM;
     329              :         }
     330            4 :         devId = static_cast<int32_t>(std::strtol(defaultDeviceIdStr.c_str(), nullptr, DECIMAL));
     331            4 :         ACL_LOG_DEBUG("successfully parse defaultDevice, devId:%d", devId);
     332            4 :         return ACL_SUCCESS;
     333           30 :     }
     334              : 
     335           15 :     aclError JsonParser::GetEventModeFromFile(const char_t *const fileName, uint8_t &event_mode, bool &found)
     336              :     {
     337           15 :         nlohmann::json js;
     338           15 :         std::string eventModeStr;
     339           15 :         aclError ret = acl::JsonParser::ParseJsonFromFile(fileName, js);
     340           15 :         if (ret != ACL_SUCCESS) {
     341            1 :             ACL_LOG_ERROR("[Parse][JsonFromFile]parse json from file[%s] failed, errorCode = %d", fileName, ret);
     342            1 :             return ret;
     343              :         }
     344           28 :         const std::string ACL_GRAPH_CONFIG_NAME = "acl_graph";
     345           14 :         const std::string ACL_EVENT_MODE_CONFIG_NAME = "event_mode";
     346              : 
     347           14 :         if (!JsonParser::ContainKey(js, ACL_GRAPH_CONFIG_NAME)) {
     348           10 :             ACL_LOG_INFO("No acl_graph in json file!");
     349           10 :             return ACL_SUCCESS;
     350              :         }
     351            4 :         const nlohmann::json &jsAclGraphConfig = JsonParser::GetCfgJsonByKey(js, ACL_GRAPH_CONFIG_NAME);
     352            4 :         if (!JsonParser::ContainKey(jsAclGraphConfig, ACL_EVENT_MODE_CONFIG_NAME)) {
     353            1 :             ACL_LOG_INFO("No event_mode under acl_graph in json file!");
     354            1 :             return ACL_SUCCESS;
     355              :         }
     356            3 :         eventModeStr = JsonParser::GetCfgStrByKey(jsAclGraphConfig, ACL_EVENT_MODE_CONFIG_NAME);
     357              : 
     358              :         // 校验 event_mode 是否为合法整数,只允许 0 或 1
     359            3 :         std::regex reg("0|1");
     360            3 :         if (!std::regex_match(eventModeStr, reg)) {
     361            1 :             ACL_LOG_ERROR("event_mode value [%s] in json is not a valid integer.", eventModeStr.c_str());
     362            1 :             acl::AclErrorLogManager::ReportInputError(acl::INVALID_PARAM_REASON_MSG,
     363            2 :                 std::vector<const char *>({"func", "value", "param", "reason"}),
     364            1 :                 std::vector<const char *>({__func__, eventModeStr.c_str(), "event_mode",
     365            2 :                     "value must be 0 or 1"}));
     366            1 :             return ACL_ERROR_INVALID_PARAM;
     367              :         }
     368              : 
     369            2 :         event_mode = static_cast<uint8_t>(std::strtol(eventModeStr.c_str(), nullptr, DECIMAL));
     370            2 :         found = true;
     371            2 :         ACL_LOG_INFO("Successfully parse event_mode: %d, event_mode_str: %s", event_mode, eventModeStr.c_str());
     372            2 :         return ACL_SUCCESS;
     373           15 :     }
     374              : 
     375           90 :     aclError JsonParser::GetStackSizeByType(
     376              :         const char_t* const fileName, const std::string& typeName, size_t& outSize, bool& outExist)
     377              :     {
     378           90 :         ACL_LOG_DEBUG("start to execute GetStackSizeByType, typeName = %s.", typeName.c_str());
     379           90 :         outExist = false;
     380           90 :         outSize = 0U;
     381           90 :         nlohmann::json js;
     382           90 :         aclError ret = acl::JsonParser::ParseJsonFromFile(fileName, js);
     383           90 :         if (ret != ACL_SUCCESS) {
     384            2 :             ACL_LOG_ERROR(
     385              :                 "[Parse][JsonFromFile]parse default config from file[%s] failed, errorCode = %d", fileName, ret);
     386            2 :             acl::AclErrorLogManager::ReportInputError(
     387            4 :                 acl::INVALID_FILE_MSG, std::vector<const char*>({"path", "reason"}),
     388            4 :                 std::vector<const char*>({fileName, "Parse config file failed"}));
     389            2 :             return ret;
     390              :         }
     391              : 
     392              :         try {
     393              :             // 检查 "StackSize" 键是否存在
     394           88 :             if (js.find("StackSize") == js.end()) {
     395           66 :                 ACL_LOG_DEBUG("StackSize key not found in config file [%s]", fileName);
     396           66 :                 outExist = false; // 明确设置 outExist 为 false
     397           66 :                 return ACL_SUCCESS;
     398              :             }
     399              : 
     400           22 :             const nlohmann::json& stackSizeJs = js["StackSize"];
     401           22 :             if (stackSizeJs.find(typeName.c_str()) != stackSizeJs.end()) {
     402           19 :                 size_t rawSize = stackSizeJs.at(typeName.c_str()).get<size_t>();
     403           18 :                 outSize = rawSize;
     404           18 :                 outExist = true;
     405           18 :                 ACL_LOG_INFO("successfully parse %s, size is %zu", typeName.c_str(), outSize);
     406              :             }
     407            1 :         } catch (const nlohmann::json::exception& e) {
     408            1 :             ACL_LOG_ERROR("parse config file [%s], exception: %s", fileName, e.what());
     409            1 :             std::string reason = acl::AclErrorLogManager::FormatStr("Parse exception: %s", e.what());
     410            1 :             acl::AclErrorLogManager::ReportInputError(
     411            2 :                 acl::INVALID_FILE_MSG, std::vector<const char*>({"path", "reason"}),
     412            2 :                 std::vector<const char*>({fileName, reason.c_str()}));
     413            1 :             return ACL_ERROR_INTERNAL_ERROR;
     414            1 :         }
     415              : 
     416           21 :         ACL_LOG_DEBUG("successfully parse StackSize by type");
     417           21 :         return ACL_SUCCESS;
     418           90 :     }
     419              : 
     420           31 :     aclError JsonParser::GetPrintFifoSizeByType(
     421              :         const char_t* const fileName, const std::string& typeName, size_t& fifoSize, bool& found)
     422              :     {
     423           31 :         ACL_LOG_DEBUG("start to execute GetPrintFifoSizeByType, typeName = %s.", typeName.c_str());
     424           31 :         std::string fifoSizeStr;
     425           31 :         found = false;
     426              : 
     427           31 :         auto ret = acl::JsonParser::GetJsonCtxByKey(fileName, fifoSizeStr, typeName, found);
     428           31 :         if (ret != ACL_SUCCESS) {
     429            0 :             acl::AclErrorLogManager::ReportInputError(acl::INVALID_FILE_MSG,
     430            0 :                 std::vector<const char *>({"path", "reason"}),
     431            0 :                 std::vector<const char *>({fileName, ("cannot parse config for " + typeName).c_str()}));
     432            0 :             ACL_LOG_ERROR("can not parse config from file[%s], config[%s], errorCode = %d", fileName, typeName.c_str(), ret);
     433            0 :             return ret;
     434              :         }
     435           31 :         if (!found) {
     436           26 :             return ACL_SUCCESS;
     437              :         }
     438              : 
     439            5 :         std::regex reg("[1-9]\\d*");
     440            5 :         if (!std::regex_match(fifoSizeStr, reg)) {
     441            2 :             acl::AclErrorLogManager::ReportInputError(acl::INVALID_PARAM_REASON_MSG,
     442            4 :                 std::vector<const char *>({"func", "value", "param", "reason"}),
     443            2 :                 std::vector<const char *>({__func__, fifoSizeStr.c_str(), "fifoSize",
     444            4 :                     "value must be a positive integer in acl.json"}));
     445            2 :             ACL_LOG_ERROR("fifoSize %s in acl.json is not a positive integer.", fifoSizeStr.c_str());
     446            2 :             return ACL_ERROR_INVALID_PARAM;
     447              :         }
     448              : 
     449            3 :         fifoSize = static_cast<size_t>(std::strtol(fifoSizeStr.c_str(), nullptr, DECIMAL));
     450            3 :         ACL_LOG_DEBUG("successfully parse print fifo size by type");
     451            3 :         return ACL_SUCCESS;
     452           31 :     }
     453              : } // namespace acl
        

Generated by: LCOV version 2.0-1