diff --git a/framework/tools/hc-gen/src/ast.h b/framework/tools/hc-gen/src/ast.h index 17a4b14118e22c34991377a71d2a6079854e20b2..5938a6427489435ddb501242ea62a2ff342d7ffe 100644 --- a/framework/tools/hc-gen/src/ast.h +++ b/framework/tools/hc-gen/src/ast.h @@ -383,7 +383,7 @@ public: return false; } } else { - uint32_t ret = forwardCallback(roundWalkObj, walkDepth); + uint32_t ret = static_cast(forwardCallback(roundWalkObj, walkDepth)); /* when callback return EASTWALKBREAK, not walk current's child */ if (ret && ret != EASTWALKBREAK) { return false; diff --git a/framework/tools/hc-gen/src/lexer.cpp b/framework/tools/hc-gen/src/lexer.cpp index e9e10566017b8899192bc1cc0d8faf9af688c234..3b30dc2f18f0cd758739fcea831ecdf731ad9351 100644 --- a/framework/tools/hc-gen/src/lexer.cpp +++ b/framework/tools/hc-gen/src/lexer.cpp @@ -12,7 +12,7 @@ #include #include "logger.h" - +#define PATH_MAX 128 using namespace OHOS::Hardware; static constexpr int BINARY_NUM = 2; @@ -40,7 +40,14 @@ bool Lexer::Initialize(const std::string &sourceName) bufferEnd_ = nullptr; lineno_ = 1; lineLoc_ = 1; - src_.open(srcName_->c_str(), std::ifstream::binary); + + char realpathStr[PATH_MAX] = {'\0'}; + if (realpath(srcName_->c_str(), realpathStr) == nullptr) { + HDF_LOGE("%{public}s:realpath failed.ret = %{public}s", __func__, strerror(errno)); + return false; + } + + src_.open(realpathStr, std::ifstream::binary); if (!src_.is_open()) { Logger().Error() << "Failed to open source file: " << srcName_->data(); return false; diff --git a/framework/tools/hc-gen/src/macro_gen.cpp b/framework/tools/hc-gen/src/macro_gen.cpp index 4ce4eeb4e94fa90c84f54be4da09d8d0ff5dabf0..7652ae0d51f0855ae5b00e05247902c19c16dbd1 100644 --- a/framework/tools/hc-gen/src/macro_gen.cpp +++ b/framework/tools/hc-gen/src/macro_gen.cpp @@ -12,7 +12,7 @@ #include "file.h" #include "logger.h" #include "macro_gen.h" - +#define PATH_MAX 128 using namespace OHOS::Hardware; constexpr static const char *FILE_HEAD_COMMENT = @@ -82,8 +82,14 @@ bool MacroGen::Initialize() if (outFileName.find(".h") == std::string::npos) { outFileName.append(".h"); } + + char realpathStr[PATH_MAX] = {'\0'}; + if (realpath(outFileName, realpathStr) == nullptr) { + HDF_LOGE("%{public}s:realpath failed.ret = %{public}s", __func__, strerror(errno)); + return false; + } - ofs_.open(outFileName, std::ofstream::out | std::ofstream::binary); + ofs_.open(realpathStr, std::ofstream::out | std::ofstream::binary); if (!ofs_.is_open()) { Logger().Error() << "failed to open output file: " << outFileName; return false; diff --git a/framework/tools/hc-gen/src/option.cpp b/framework/tools/hc-gen/src/option.cpp index c661d330176bb4fcf5d0d6754ba6010040bbc7c8..b7ebe1b9fce61434e4c10c666d0881e312658929 100644 --- a/framework/tools/hc-gen/src/option.cpp +++ b/framework/tools/hc-gen/src/option.cpp @@ -42,7 +42,7 @@ Option &Option::Parse(int argc, char **argv) break; } - if (optind >= argc) { + if (optind < 0 || optind >= argc) { Logger().Error() << "Miss input file name"; SetOptionError(); break; diff --git a/framework/tools/hc-gen/src/startup_cfg_gen.cpp b/framework/tools/hc-gen/src/startup_cfg_gen.cpp index bcb5d2f18e7893ba9981b06ca9c842ac304f2bd7..a15157a3399790726e83d3b3bf014021e4c1a0d2 100644 --- a/framework/tools/hc-gen/src/startup_cfg_gen.cpp +++ b/framework/tools/hc-gen/src/startup_cfg_gen.cpp @@ -13,7 +13,7 @@ #include "ast.h" #include "file.h" #include "logger.h" - +#define PATH_MAX 128 using namespace OHOS::Hardware; static constexpr const char *BOOT_CONFIG_TOP = @@ -87,7 +87,13 @@ bool StartupCfgGen::Initialize() outFileName = Util::File::StripSuffix(outFileName).append(".cfg"); outFileName_ = Util::File::FileNameBase(outFileName); - ofs_.open(outFileName, std::ofstream::out | std::ofstream::binary); + char realpathStr[PATH_MAX] = {'\0'}; + if (realpath(outFileName, realpathStr) == nullptr) { + HDF_LOGE("%{public}s:realpath failed.ret = %{public}s", __func__, strerror(errno)); + return false; + } + + ofs_.open(realpathStr, std::ofstream::out | std::ofstream::binary); if (!ofs_.is_open()) { Logger().Error() << "failed to open output file: " << outFileName; return false; @@ -191,8 +197,8 @@ void StartupCfgGen::HostInfoOutput(const std::string &name, bool end) if (!hostInfoMap_[name].initConfig.empty()) { for (auto &info : hostInfoMap_[name].initConfig) { - int firstQuotePos = info.find("\""); - int secondQuotePos = info.find("\"", firstQuotePos + 1); + unsigned int firstQuotePos = info.find("\""); + unsigned int secondQuotePos = info.find("\"", firstQuotePos + 1); configedKeywords.insert(info.substr(firstQuotePos + 1, secondQuotePos - (firstQuotePos + 1))); } } @@ -365,7 +371,7 @@ void StartupCfgGen::GetMallocOpt(const std::shared_ptr &hostInfo, std::vector mallocOptions = {}; GetConfigVector(term, mallocOptions); for (auto mallocOption : mallocOptions) { - int separatorPos = mallocOption.find(MALLOPT_SEPARATOR); + unsigned int separatorPos = mallocOption.find(MALLOPT_SEPARATOR); std::string malloptKey = mallocOption.substr(0, separatorPos); std::string malloptValue = mallocOption.substr(separatorPos + 1, mallocOption.length() - (separatorPos + 1)); diff --git a/framework/tools/hc-gen/src/text_gen.cpp b/framework/tools/hc-gen/src/text_gen.cpp index 6ac78346ab59110f6afbb3c60a91894c175178b2..2a55479d1049da87e474ac787cd542d5c27f3c60 100644 --- a/framework/tools/hc-gen/src/text_gen.cpp +++ b/framework/tools/hc-gen/src/text_gen.cpp @@ -369,6 +369,9 @@ uint32_t TextGen::ImplementCloseBraceGen(const std::shared_ptr &objec if (!object->IsNode() || ConfigNode::CastFrom(object)->GetNodeType() == NODE_INHERIT) { return NOERR; } + if (depth < 0 && depth != -1) { + return NOERR; + } if (object == ast_->GetAstRoot()) { ofs_ << "};\n"; } else {