From 3e90b3e1df6a60d9ac4eb158fd75be0febf25b71 Mon Sep 17 00:00:00 2001 From: yue Date: Tue, 31 May 2022 20:27:21 +0800 Subject: [PATCH] fix:modify security method and fix file path parsing Signed-off-by: yue --- tools/hdi-gen/MakefileLinux | 14 +---- tools/hdi-gen/MakefileWin | 14 +---- .../codegen/c_client_proxy_code_emitter.cpp | 2 +- .../codegen/c_custom_types_code_emitter.cpp | 2 +- .../codegen/c_interface_code_emitter.cpp | 2 +- .../codegen/c_service_driver_code_emitter.cpp | 2 +- .../codegen/c_service_impl_code_emitter.cpp | 2 +- .../codegen/c_service_stub_code_emitter.cpp | 2 +- tools/hdi-gen/codegen/code_emitter.cpp | 14 ++++- tools/hdi-gen/codegen/code_emitter.h | 2 +- .../codegen/cpp_client_proxy_code_emitter.cpp | 2 +- .../codegen/cpp_custom_types_code_emitter.cpp | 2 +- .../codegen/cpp_interface_code_emitter.cpp | 2 +- .../cpp_service_driver_code_emitter.cpp | 2 +- .../codegen/cpp_service_impl_code_emitter.cpp | 2 +- .../codegen/cpp_service_stub_code_emitter.cpp | 2 +- .../java_client_interface_code_emitter.cpp | 2 +- .../java_client_proxy_code_emitter.cpp | 2 +- tools/hdi-gen/preprocessor/preprocessor.cpp | 10 +++- tools/hdi-gen/util/file.cpp | 3 +- tools/hdi-gen/util/options.cpp | 30 ++-------- tools/hdi-gen/util/options.h | 4 +- tools/hdi-gen/util/string.cpp | 55 ++++++------------- tools/hdi-gen/util/string.h | 5 +- tools/hdi-gen/util/string_builder.cpp | 14 +++-- utils/src/hdf_map.c | 8 ++- 26 files changed, 81 insertions(+), 120 deletions(-) diff --git a/tools/hdi-gen/MakefileLinux b/tools/hdi-gen/MakefileLinux index 0302817a..d7649e09 100755 --- a/tools/hdi-gen/MakefileLinux +++ b/tools/hdi-gen/MakefileLinux @@ -4,8 +4,7 @@ # the GPL, or the BSD license, at your option. # See the LICENSE file in the root of this repository for complete details. -BOUNDS_CHECK_DIR := $(abspath ../../../../third_party/bounds_checking_function) -INCLUDES = -I$(PWD) -I$(BOUNDS_CHECK_DIR)/include +INCLUDES = -I$(PWD) CODEGEN_SOURCE_DIR := $(PWD)/codegen CODEGEN_SOURCE := $(wildcard $(CODEGEN_SOURCE_DIR)/*.cpp) @@ -37,16 +36,11 @@ UTIL_SOURCE := $(wildcard $(UTIL_SOURCE_DIR)/*.cpp) UTIL_OBJS_DIR := $(BUILD_DIR)/util UTIL_OBJS := $(addprefix $(UTIL_OBJS_DIR)/, $(patsubst %.cpp, %.o, $(notdir $(UTIL_SOURCE)))) -BOUNDS_CHECK_SOURCE_DIR := $(abspath ../../../../third_party/bounds_checking_function/src) -BOUNDS_CHECK_SOURCE := $(wildcard $(BOUNDS_CHECK_SOURCE_DIR)/*.c) -BOUNDS_CHECK_OBJS_DIR := $(BUILD_DIR)/bounds_checking_function -BOUNDS_CHECK_OBJS := $(addprefix $(BOUNDS_CHECK_OBJS_DIR)/, $(patsubst %.c, %.o, $(notdir $(BOUNDS_CHECK_SOURCE)))) - MAIN_SOURCE := $(wildcard *.cpp) MAIN_OBJS := $(addprefix $(BUILD_DIR)/, $(patsubst %.cpp, %.o, $(MAIN_SOURCE))) all:$(TARGET) -$(TARGET) : $(BOUNDS_CHECK_OBJS) $(UTIL_OBJS) $(AST_OBJS) $(LEXER_OBJS) $(PREPROCESSOR_OBJS) $(PARSER_OBJS) $(CODEGEN_OBJS) $(MAIN_OBJS) +$(TARGET) : $(UTIL_OBJS) $(AST_OBJS) $(LEXER_OBJS) $(PREPROCESSOR_OBJS) $(PARSER_OBJS) $(CODEGEN_OBJS) $(MAIN_OBJS) $(Q) $(CXX) $(CXXFLAGS) $(INCLUDES) $^ -o $@ $(Q) echo $(TARGET):build successful. @@ -78,10 +72,6 @@ $(UTIL_OBJS_DIR)/%.o : $(UTIL_SOURCE_DIR)/%.cpp $(Q) mkdir -p $(UTIL_OBJS_DIR) $(Q) $(CXX) $(CXXFLAGS) $(INCLUDES) -c $^ -o $@ -$(BOUNDS_CHECK_OBJS_DIR)/%.o : $(BOUNDS_CHECK_SOURCE_DIR)/%.c - $(Q) mkdir -p $(BOUNDS_CHECK_OBJS_DIR) - $(Q) $(CXX) $(CXXFLAGS) $(INCLUDES) -c $^ -o $@ - clean: $(Q) rm -rf $(TARGET) $(BUILD_DIR) diff --git a/tools/hdi-gen/MakefileWin b/tools/hdi-gen/MakefileWin index 6d5a4bf9..d2794c59 100755 --- a/tools/hdi-gen/MakefileWin +++ b/tools/hdi-gen/MakefileWin @@ -4,8 +4,7 @@ # the GPL, or the BSD license, at your option. # See the LICENSE file in the root of this repository for complete details. -BOUNDS_CHECK_DIR:=$(subst /,\, $(abspath ..\..\..\..\third_party\bounds_checking_function)) -INCLUDES:=-I $(PWD) -I $(BOUNDS_CHECK_DIR)\include +INCLUDES:=-I $(PWD) CODEGEN_SOURCE_DIR:=$(PWD)\codegen CODEGEN_SOURCE:=$(wildcard $(CODEGEN_SOURCE_DIR)/*.cpp) @@ -37,16 +36,11 @@ UTIL_SOURCE:=$(wildcard $(UTIL_SOURCE_DIR)/*.cpp) UTIL_OBJS_DIR:=$(BUILD_DIR)\util UTIL_OBJS:=$(addprefix $(UTIL_OBJS_DIR)\, $(patsubst %.cpp, %.o, $(notdir $(UTIL_SOURCE)))) -BOUNDS_CHECK_SOURCE_DIR:=$(subst /,\, $(abspath ..\..\..\..\third_party\bounds_checking_function\src)) -BOUNDS_CHECK_SOURCE:=$(wildcard $(BOUNDS_CHECK_SOURCE_DIR)/*.c) -BOUNDS_CHECK_OBJS_DIR:=$(BUILD_DIR)\bounds_checking_function -BOUNDS_CHECK_OBJS:=$(addprefix $(BOUNDS_CHECK_OBJS_DIR)\, $(patsubst %.c, %.o, $(notdir $(BOUNDS_CHECK_SOURCE)))) - MAIN_SOURCE:=$(wildcard *.cpp) MAIN_OBJS:=$(addprefix $(BUILD_DIR)\, $(patsubst %.cpp, %.o, $(MAIN_SOURCE))) all : $(TARGET) -$(TARGET) : $(BOUNDS_CHECK_OBJS) $(UTIL_OBJS) $(AST_OBJS) $(LEXER_OBJS) $(PREPROCESSOR_OBJS) $(PARSER_OBJS) $(CODEGEN_OBJS) $(MAIN_OBJS) +$(TARGET) : $(UTIL_OBJS) $(AST_OBJS) $(LEXER_OBJS) $(PREPROCESSOR_OBJS) $(PARSER_OBJS) $(CODEGEN_OBJS) $(MAIN_OBJS) $(Q) $(CXX) $(CXXFLAGS) $(INCLUDES) $^ -o $@ $(Q) echo $(TARGET):build successful. @@ -78,10 +72,6 @@ $(UTIL_OBJS_DIR)\\%.o : $(UTIL_SOURCE_DIR)\\%.cpp $(Q) if not exist $(UTIL_OBJS_DIR) (mkdir $(UTIL_OBJS_DIR)) $(Q) $(CXX) $(CXXFLAGS) $(INCLUDES) -c $^ -o $@ -$(BOUNDS_CHECK_OBJS_DIR)\\%.o : $(BOUNDS_CHECK_SOURCE_DIR)\\%.c - $(Q) if not exist $(BOUNDS_CHECK_OBJS_DIR) (mkdir $(BOUNDS_CHECK_OBJS_DIR)) - $(Q) $(CXX) $(CXXFLAGS) $(INCLUDES) -c $^ -o $@ - clean: $(Q) if exist $(TARGET) (del $(TARGET)) $(Q) if exist $(BUILD_DIR) (rd /s /q $(BUILD_DIR)) diff --git a/tools/hdi-gen/codegen/c_client_proxy_code_emitter.cpp b/tools/hdi-gen/codegen/c_client_proxy_code_emitter.cpp index 126b5893..3d058489 100644 --- a/tools/hdi-gen/codegen/c_client_proxy_code_emitter.cpp +++ b/tools/hdi-gen/codegen/c_client_proxy_code_emitter.cpp @@ -15,7 +15,7 @@ namespace HDI { bool CClientProxyCodeEmitter::ResolveDirectory(const String &targetDirectory) { if (ast_->GetASTFileType() == ASTFileType::AST_IFACE || ast_->GetASTFileType() == ASTFileType::AST_ICALLBACK) { - directory_ = GetFilePath(targetDirectory); + directory_ = GetFileParentPath(targetDirectory); } else { return false; } diff --git a/tools/hdi-gen/codegen/c_custom_types_code_emitter.cpp b/tools/hdi-gen/codegen/c_custom_types_code_emitter.cpp index bf3ce1c8..29ddc64e 100644 --- a/tools/hdi-gen/codegen/c_custom_types_code_emitter.cpp +++ b/tools/hdi-gen/codegen/c_custom_types_code_emitter.cpp @@ -19,7 +19,7 @@ bool CCustomTypesCodeEmitter::ResolveDirectory(const String &targetDirectory) return false; } - directory_ = GetFilePath(targetDirectory); + directory_ = GetFileParentPath(targetDirectory); if (!File::CreateParentDir(directory_)) { Logger::E("CCustomTypesCodeEmitter", "Create '%s' failed!", directory_.string()); return false; diff --git a/tools/hdi-gen/codegen/c_interface_code_emitter.cpp b/tools/hdi-gen/codegen/c_interface_code_emitter.cpp index cde22667..cdefc530 100644 --- a/tools/hdi-gen/codegen/c_interface_code_emitter.cpp +++ b/tools/hdi-gen/codegen/c_interface_code_emitter.cpp @@ -15,7 +15,7 @@ namespace HDI { bool CInterfaceCodeEmitter::ResolveDirectory(const String &targetDirectory) { if (ast_->GetASTFileType() == ASTFileType::AST_IFACE || ast_->GetASTFileType() == ASTFileType::AST_ICALLBACK) { - directory_ = GetFilePath(targetDirectory); + directory_ = GetFileParentPath(targetDirectory); } else { return false; } diff --git a/tools/hdi-gen/codegen/c_service_driver_code_emitter.cpp b/tools/hdi-gen/codegen/c_service_driver_code_emitter.cpp index 65a34045..40e4c1d8 100644 --- a/tools/hdi-gen/codegen/c_service_driver_code_emitter.cpp +++ b/tools/hdi-gen/codegen/c_service_driver_code_emitter.cpp @@ -19,7 +19,7 @@ bool CServiceDriverCodeEmitter::ResolveDirectory(const String &targetDirectory) return false; } - directory_ = GetFilePath(targetDirectory); + directory_ = GetFileParentPath(targetDirectory); if (!File::CreateParentDir(directory_)) { Logger::E("CServiceDriverCodeEmitter", "Create '%s' failed!", directory_.string()); return false; diff --git a/tools/hdi-gen/codegen/c_service_impl_code_emitter.cpp b/tools/hdi-gen/codegen/c_service_impl_code_emitter.cpp index 92a3bf3b..1bcc5909 100644 --- a/tools/hdi-gen/codegen/c_service_impl_code_emitter.cpp +++ b/tools/hdi-gen/codegen/c_service_impl_code_emitter.cpp @@ -15,7 +15,7 @@ namespace HDI { bool CServiceImplCodeEmitter::ResolveDirectory(const String &targetDirectory) { if (ast_->GetASTFileType() == ASTFileType::AST_IFACE || ast_->GetASTFileType() == ASTFileType::AST_ICALLBACK) { - directory_ = GetFilePath(targetDirectory); + directory_ = GetFileParentPath(targetDirectory); } else { return false; } diff --git a/tools/hdi-gen/codegen/c_service_stub_code_emitter.cpp b/tools/hdi-gen/codegen/c_service_stub_code_emitter.cpp index 1ceadbce..d86566f2 100644 --- a/tools/hdi-gen/codegen/c_service_stub_code_emitter.cpp +++ b/tools/hdi-gen/codegen/c_service_stub_code_emitter.cpp @@ -16,7 +16,7 @@ namespace HDI { bool CServiceStubCodeEmitter::ResolveDirectory(const String &targetDirectory) { if (ast_->GetASTFileType() == ASTFileType::AST_IFACE || ast_->GetASTFileType() == ASTFileType::AST_ICALLBACK) { - directory_ = GetFilePath(targetDirectory); + directory_ = GetFileParentPath(targetDirectory); } else { return false; } diff --git a/tools/hdi-gen/codegen/code_emitter.cpp b/tools/hdi-gen/codegen/code_emitter.cpp index 911e2e3d..c212359c 100644 --- a/tools/hdi-gen/codegen/code_emitter.cpp +++ b/tools/hdi-gen/codegen/code_emitter.cpp @@ -106,10 +106,20 @@ bool CodeEmitter::NeedFlag(const AutoPtr &method) return false; } -String CodeEmitter::GetFilePath(const String &outDir) +/** + * ForExample: + * -r option: -r ohos.hdi:./drivers/interface + * outDir: ./out + * package: ohos.hdi.foo.v1_0 + * subPackage: foo.v1_0 + * subPath: foo/v1_0 + * GenPath: ./out/foo/v1_0/ + */ +String CodeEmitter::GetFileParentPath(const String &outDir) { String outPath = outDir.EndsWith(File::separator) ? outDir.Substring(0, outDir.GetLength() - 1) : outDir; - String subPath = Options::GetInstance().GetSubPath(ast_->GetPackageName()); + String subPackage = Options::GetInstance().GetSubPackage(ast_->GetPackageName()); + String subPath = subPackage.Replace('.', File::separator); if (subPath.IsEmpty()) { return File::AdapterPath(String::Format("%s/", outPath.string(), subPath.string())); } else { diff --git a/tools/hdi-gen/codegen/code_emitter.h b/tools/hdi-gen/codegen/code_emitter.h index e6e5db4c..86b9e474 100644 --- a/tools/hdi-gen/codegen/code_emitter.h +++ b/tools/hdi-gen/codegen/code_emitter.h @@ -83,7 +83,7 @@ protected: bool NeedFlag(const AutoPtr &method); - String GetFilePath(const String &outDir); + String GetFileParentPath(const String &outDir); String PackageToFilePath(const String &packageName); diff --git a/tools/hdi-gen/codegen/cpp_client_proxy_code_emitter.cpp b/tools/hdi-gen/codegen/cpp_client_proxy_code_emitter.cpp index cf5246a2..8f514d72 100644 --- a/tools/hdi-gen/codegen/cpp_client_proxy_code_emitter.cpp +++ b/tools/hdi-gen/codegen/cpp_client_proxy_code_emitter.cpp @@ -15,7 +15,7 @@ namespace HDI { bool CppClientProxyCodeEmitter::ResolveDirectory(const String &targetDirectory) { if (ast_->GetASTFileType() == ASTFileType::AST_IFACE || ast_->GetASTFileType() == ASTFileType::AST_ICALLBACK) { - directory_ = GetFilePath(targetDirectory); + directory_ = GetFileParentPath(targetDirectory); } else { return false; } diff --git a/tools/hdi-gen/codegen/cpp_custom_types_code_emitter.cpp b/tools/hdi-gen/codegen/cpp_custom_types_code_emitter.cpp index 1d16c9b4..0b369d28 100644 --- a/tools/hdi-gen/codegen/cpp_custom_types_code_emitter.cpp +++ b/tools/hdi-gen/codegen/cpp_custom_types_code_emitter.cpp @@ -18,7 +18,7 @@ bool CppCustomTypesCodeEmitter::ResolveDirectory(const String &targetDirectory) return false; } - directory_ = GetFilePath(targetDirectory); + directory_ = GetFileParentPath(targetDirectory); if (!File::CreateParentDir(directory_)) { Logger::E("CppCustomTypesCodeEmitter", "Create '%s' failed!", directory_.string()); return false; diff --git a/tools/hdi-gen/codegen/cpp_interface_code_emitter.cpp b/tools/hdi-gen/codegen/cpp_interface_code_emitter.cpp index 9a768f69..07d455e0 100644 --- a/tools/hdi-gen/codegen/cpp_interface_code_emitter.cpp +++ b/tools/hdi-gen/codegen/cpp_interface_code_emitter.cpp @@ -15,7 +15,7 @@ namespace HDI { bool CppInterfaceCodeEmitter::ResolveDirectory(const String &targetDirectory) { if (ast_->GetASTFileType() == ASTFileType::AST_IFACE || ast_->GetASTFileType() == ASTFileType::AST_ICALLBACK) { - directory_ = GetFilePath(targetDirectory); + directory_ = GetFileParentPath(targetDirectory); } else { return false; } diff --git a/tools/hdi-gen/codegen/cpp_service_driver_code_emitter.cpp b/tools/hdi-gen/codegen/cpp_service_driver_code_emitter.cpp index c29d1fea..5ffe9598 100644 --- a/tools/hdi-gen/codegen/cpp_service_driver_code_emitter.cpp +++ b/tools/hdi-gen/codegen/cpp_service_driver_code_emitter.cpp @@ -19,7 +19,7 @@ bool CppServiceDriverCodeEmitter::ResolveDirectory(const String &targetDirectory return false; } - directory_ = GetFilePath(targetDirectory); + directory_ = GetFileParentPath(targetDirectory); if (!File::CreateParentDir(directory_)) { Logger::E("CppServiceDriverCodeEmitter", "Create '%s' failed!", directory_.string()); return false; diff --git a/tools/hdi-gen/codegen/cpp_service_impl_code_emitter.cpp b/tools/hdi-gen/codegen/cpp_service_impl_code_emitter.cpp index 30ea3322..f70bdbca 100644 --- a/tools/hdi-gen/codegen/cpp_service_impl_code_emitter.cpp +++ b/tools/hdi-gen/codegen/cpp_service_impl_code_emitter.cpp @@ -15,7 +15,7 @@ namespace HDI { bool CppServiceImplCodeEmitter::ResolveDirectory(const String &targetDirectory) { if (ast_->GetASTFileType() == ASTFileType::AST_IFACE || ast_->GetASTFileType() == ASTFileType::AST_ICALLBACK) { - directory_ = GetFilePath(targetDirectory); + directory_ = GetFileParentPath(targetDirectory); } else { return false; } diff --git a/tools/hdi-gen/codegen/cpp_service_stub_code_emitter.cpp b/tools/hdi-gen/codegen/cpp_service_stub_code_emitter.cpp index 8f6885a1..f140f780 100644 --- a/tools/hdi-gen/codegen/cpp_service_stub_code_emitter.cpp +++ b/tools/hdi-gen/codegen/cpp_service_stub_code_emitter.cpp @@ -15,7 +15,7 @@ namespace HDI { bool CppServiceStubCodeEmitter::ResolveDirectory(const String &targetDirectory) { if (ast_->GetASTFileType() == ASTFileType::AST_IFACE || ast_->GetASTFileType() == ASTFileType::AST_ICALLBACK) { - directory_ = GetFilePath(targetDirectory); + directory_ = GetFileParentPath(targetDirectory); } else { return false; } diff --git a/tools/hdi-gen/codegen/java_client_interface_code_emitter.cpp b/tools/hdi-gen/codegen/java_client_interface_code_emitter.cpp index 4b7a97a6..e3883bf2 100644 --- a/tools/hdi-gen/codegen/java_client_interface_code_emitter.cpp +++ b/tools/hdi-gen/codegen/java_client_interface_code_emitter.cpp @@ -15,7 +15,7 @@ namespace HDI { bool JavaClientInterfaceCodeEmitter::ResolveDirectory(const String &targetDirectory) { if (ast_->GetASTFileType() == ASTFileType::AST_IFACE || ast_->GetASTFileType() == ASTFileType::AST_ICALLBACK) { - directory_ = GetFilePath(targetDirectory); + directory_ = GetFileParentPath(targetDirectory); } else { return false; } diff --git a/tools/hdi-gen/codegen/java_client_proxy_code_emitter.cpp b/tools/hdi-gen/codegen/java_client_proxy_code_emitter.cpp index 438e0507..cfe3d417 100644 --- a/tools/hdi-gen/codegen/java_client_proxy_code_emitter.cpp +++ b/tools/hdi-gen/codegen/java_client_proxy_code_emitter.cpp @@ -14,7 +14,7 @@ namespace HDI { bool JavaClientProxyCodeEmitter::ResolveDirectory(const String &targetDirectory) { if (ast_->GetASTFileType() == ASTFileType::AST_IFACE || ast_->GetASTFileType() == ASTFileType::AST_ICALLBACK) { - directory_ = GetFilePath(targetDirectory); + directory_ = GetFileParentPath(targetDirectory); } else { return false; } diff --git a/tools/hdi-gen/preprocessor/preprocessor.cpp b/tools/hdi-gen/preprocessor/preprocessor.cpp index de8e9901..4b96ef55 100644 --- a/tools/hdi-gen/preprocessor/preprocessor.cpp +++ b/tools/hdi-gen/preprocessor/preprocessor.cpp @@ -157,7 +157,13 @@ bool Preprocessor::ParsePackage(Lexer &lexer, FileDetail &info) bool Preprocessor::ParseImports(Lexer &lexer, FileDetail &info) { Token token = lexer.PeekToken(); - while (token.kind_ == TokenType::IMPORT) { + while (token.kind_ != TokenType::END_OF_FILE) { + if (token.kind_ != TokenType::IMPORT) { + lexer.GetToken(); + token = lexer.PeekToken(); + continue; + } + lexer.GetToken(); token = lexer.PeekToken(); if (token.kind_ != TokenType::ID) { @@ -188,7 +194,7 @@ bool Preprocessor::LoadOtherIdlFiles(const FileDetail &ownerFileDetail, FileDeta continue; } - String otherFilePath = Options::GetInstance().GetPackagePath(importName) + ".idl"; + String otherFilePath = Options::GetInstance().GetImportFilePath(importName); FileDetail otherFileDetail; if (!ParseFileDetail(otherFilePath, otherFileDetail)) { return false; diff --git a/tools/hdi-gen/util/file.cpp b/tools/hdi-gen/util/file.cpp index 7d551202..2d3aae69 100644 --- a/tools/hdi-gen/util/file.cpp +++ b/tools/hdi-gen/util/file.cpp @@ -15,7 +15,6 @@ #include #include -#include "securec.h" #include "util/string_builder.h" namespace OHOS { @@ -105,7 +104,7 @@ int File::Read() return -1; } - (void)memset_s(buffer_, BUFFER_SIZE, 0, BUFFER_SIZE); + (void)memset(buffer_, 0, BUFFER_SIZE); size_t count = fread(buffer_, 1, BUFFER_SIZE - 1, fd_); if (count < BUFFER_SIZE - 1) { isError_ = ferror(fd_) != 0; diff --git a/tools/hdi-gen/util/options.cpp b/tools/hdi-gen/util/options.cpp index 90d16f19..03ff4dda 100644 --- a/tools/hdi-gen/util/options.cpp +++ b/tools/hdi-gen/util/options.cpp @@ -224,7 +224,7 @@ void Options::ShowUsage() const */ String Options::GetRootPackage(const String &package) { - const auto &packagePaths = GetPackagePath(); + const auto &packagePaths = GetPackagePathMap(); for (const auto &packageRoot : packagePaths) { if (package.StartsWith(packageRoot.first)) { return packageRoot.first; @@ -242,7 +242,7 @@ String Options::GetRootPackage(const String &package) */ String Options::GetRootPath(const String &package) { - const auto &packagePaths = GetPackagePath(); + const auto &packagePaths = GetPackagePathMap(); for (const auto &packageRoot : packagePaths) { if (package.StartsWith(packageRoot.first)) { return packageRoot.second; @@ -268,26 +268,6 @@ String Options::GetSubPackage(const String &package) return package.Substring(rootPackage.GetLength() + 1); } -/* - * For Example - * -r option: -r ohos.hdi:./drivers/interface - * package:ohos.hdi.foo.v1_0 - * subPackage:foo/v1_0 - */ -String Options::GetSubPath(const String &package) -{ - String rootPackage = GetRootPackage(package); - if (rootPackage.IsEmpty()) { - return package; - } - - String subPath = package.Substring(rootPackage.GetLength() + 1).Replace('.', File::separator); - if (subPath.IsEmpty()) { - return ""; - } - return subPath; -} - /* * For Example * -r option: -r ohos.hdi:./drivers/interface @@ -298,7 +278,7 @@ String Options::GetPackagePath(const String &package) { String rootPackage = ""; String rootPath = ""; - const auto &packagePaths = GetPackagePath(); + const auto &packagePaths = GetPackagePathMap(); for (const auto &packageRoot : packagePaths) { if (package.StartsWith(packageRoot.first)) { rootPackage = packageRoot.first; @@ -307,7 +287,9 @@ String Options::GetPackagePath(const String &package) } if (rootPackage.IsEmpty()) { - return package.Replace('.', File::separator); + // The current path is the root path + String curPath = File::AdapterPath(package.Replace('.', File::separator)); + return File::RealPath(curPath); } if (rootPath.EndsWith(File::separator)) { diff --git a/tools/hdi-gen/util/options.h b/tools/hdi-gen/util/options.h index 24d54178..892184cc 100644 --- a/tools/hdi-gen/util/options.h +++ b/tools/hdi-gen/util/options.h @@ -74,7 +74,7 @@ public: return sourceFiles_; } - inline PkgPathMap GetPackagePath() const + inline PkgPathMap GetPackagePathMap() const { return packagePath_; } @@ -111,8 +111,6 @@ public: String GetSubPackage(const String &package); - String GetSubPath(const String &package); - String GetPackagePath(const String &package); String GetImportFilePath(const String &import); diff --git a/tools/hdi-gen/util/string.cpp b/tools/hdi-gen/util/string.cpp index 10d3aa14..e25046d1 100644 --- a/tools/hdi-gen/util/string.cpp +++ b/tools/hdi-gen/util/string.cpp @@ -8,6 +8,7 @@ #include "util/string.h" +#include #include #include #include @@ -15,7 +16,6 @@ #include #include -#include "securec.h" #include "util/logger.h" #include "util/string_builder.h" @@ -57,12 +57,7 @@ SharedData *SharedData::Allocate(int size) return nullptr; } - auto handle = reinterpret_cast(malloc(sizeof(SharedData) + size + 1)); - if (handle == nullptr) { - Logger::E(String::TAG, "Fail to malloc %lu bytes memory", size); - return handle; - } - + auto handle = reinterpret_cast(calloc(1, sizeof(SharedData) + size + 1)); new (handle) SharedData(1, size); return handle; } @@ -104,12 +99,7 @@ String::String(const char *string) if (string_ == nullptr) { return; } - - if (strcpy_s(string_, strlen(string) + 1, string) != EOK) { - Logger::E(String::TAG, "The Construct of \"%s\" is failed.", string); - SharedData::Release(string_); - string_ = nullptr; - } + (void)strcpy(string_, string); } } @@ -118,7 +108,7 @@ String::String(const char *string, size_t length) if (string != nullptr) { string_ = SharedData::ToString(SharedData::Allocate(length)); if (string_ != nullptr) { - (void)memcpy_s(string_, length + 1, string, length); + (void)strncpy(string_, string, length); string_[length] = '\0'; } } @@ -140,7 +130,7 @@ String::String(int size) { string_ = SharedData::ToString(SharedData::Allocate(size)); if (string_ != nullptr) { - (void)memset_s(string_, size + 1, 0, size + 1); + (void)memset(string_, 0, size + 1); } } @@ -691,12 +681,7 @@ String &String::operator=(const char *string) return *this; } - if (strcpy_s(string_, strlen(string) + 1, string) != EOK) { - Logger::E(String::TAG, "The operator= of \"%s\" is failed.", string); - SharedData::Release(string_); - string_ = nullptr; - } - + (void)strcpy(string_, string); return *this; } @@ -727,6 +712,7 @@ String &String::operator+=(const char *other) } int thisSize = GetLength(); + int otherSize = strlen(other); int newSize = thisSize + strlen(other); String newString(newSize); if (newString.string_ == nullptr) { @@ -735,15 +721,11 @@ String &String::operator+=(const char *other) } if (string_ != nullptr && thisSize > 0) { - if (memcpy_s(newString.string_, newSize + 1, string_, thisSize) != EOK) { - Logger::E(String::TAG, "The operator+= of \"%s\" is failed. 2", string_); - goto finished; - } + (void)strncpy(newString.string_, string_, thisSize); } - if (strcpy_s(newString.string_ + thisSize, newSize + 1 - thisSize, other) != EOK) { - Logger::E(String::TAG, "The operator+= of \"%s\" is failed.", string_); - goto finished; + if (other != nullptr && otherSize > 0) { + (void)strncpy(newString.string_ + thisSize, other, otherSize); } finished: @@ -760,7 +742,8 @@ String &String::operator+=(const String &other) } int thisSize = GetLength(); - int newSize = thisSize + other.GetLength(); + int otherSize = other.GetLength(); + int newSize = thisSize + otherSize; String newString(newSize); if (newString.string_ == nullptr) { Logger::E(String::TAG, "The operator+= of \"%s\" is failed. 1", string_); @@ -768,15 +751,11 @@ String &String::operator+=(const String &other) } if (string_ != nullptr && thisSize > 0) { - if (memcpy_s(newString.string_, newSize + 1, string_, thisSize) != EOK) { - Logger::E(String::TAG, "The operator+= of \"%s\" is failed. 2", string_); - goto finished; - } + (void)strncpy(newString.string_, string_, thisSize); } - if (strcpy_s(newString.string_ + thisSize, newSize + 1 - thisSize, other.string_) != EOK) { - Logger::E(String::TAG, "The operator+= of \"%s\" is failed. 3", string_); - goto finished; + if (other.string_ != nullptr && otherSize > 0) { + (void)strncpy(newString.string_ + thisSize, other.string_, otherSize); } finished: @@ -794,7 +773,7 @@ String String::Format(const char *format, ...) va_copy(argsCopy, args); char buf[LINE_MAX_SIZE] = {0}; - int len = vsnprintf_s(buf, LINE_MAX_SIZE, LINE_MAX_SIZE - 1, format, args); + int len = vsnprintf(buf, LINE_MAX_SIZE, format, args); String string; if (len <= 0) { va_end(args); @@ -809,7 +788,7 @@ String String::Format(const char *format, ...) return string; } - if (vsnprintf_s(string.string_, len + 1, len, format, argsCopy) < 0) { + if (vsnprintf(string.string_, len + 1, format, argsCopy) < 0) { va_end(args); va_end(argsCopy); return string; diff --git a/tools/hdi-gen/util/string.h b/tools/hdi-gen/util/string.h index 0b6f1f7b..1ec900e0 100644 --- a/tools/hdi-gen/util/string.h +++ b/tools/hdi-gen/util/string.h @@ -45,7 +45,10 @@ public: inline bool IsEmpty() const { - return string_ == nullptr || string_[0] == '\0'; + if (string_ == nullptr) { + return true; + } + return string_[0] == '\0' ? true : false; } int GetLength() const; diff --git a/tools/hdi-gen/util/string_builder.cpp b/tools/hdi-gen/util/string_builder.cpp index f8b711fd..c9bba797 100644 --- a/tools/hdi-gen/util/string_builder.cpp +++ b/tools/hdi-gen/util/string_builder.cpp @@ -7,7 +7,9 @@ */ #include "util/string_builder.h" -#include "securec.h" + +#include +#include #include "util/logger.h" namespace OHOS { @@ -48,7 +50,7 @@ StringBuilder &StringBuilder::Append(const char *string) } } - (void)memcpy_s(buffer_ + position_, capacity_ - position_, string, len); + (void)memcpy(buffer_ + position_, string, len); position_ += len; return *this; } @@ -66,7 +68,7 @@ StringBuilder &StringBuilder::Append(const String &string) } } - (void)memcpy_s(buffer_ + position_, capacity_ - position_, string.string(), len); + (void)memcpy(buffer_ + position_, string.string(), len); position_ += len; return *this; } @@ -79,7 +81,7 @@ StringBuilder &StringBuilder::AppendFormat(const char *format, ...) va_copy(argsCopy, args); char buf[LINE_MAX_SIZE] = {0}; - int len = vsnprintf_s(buf, LINE_MAX_SIZE, LINE_MAX_SIZE - 1, format, args); + int len = vsnprintf(buf, LINE_MAX_SIZE, format, args); if (len <= 0) { va_end(args); va_end(argsCopy); @@ -94,7 +96,7 @@ StringBuilder &StringBuilder::AppendFormat(const char *format, ...) } } - if (vsnprintf_s(buffer_ + position_, len + 1, len, format, argsCopy) < 0) { + if (vsnprintf(buffer_ + position_, len + 1, format, argsCopy) < 0) { va_end(args); va_end(argsCopy); return *this; @@ -132,7 +134,7 @@ bool StringBuilder::Grow(size_t size) } if (buffer_ != nullptr) { - (void)memcpy_s(newBuffer, newSize, buffer_, capacity_); + (void)memcpy(newBuffer, buffer_, capacity_); free(buffer_); } buffer_ = newBuffer; diff --git a/utils/src/hdf_map.c b/utils/src/hdf_map.c index 627c9e66..21fc612c 100644 --- a/utils/src/hdf_map.c +++ b/utils/src/hdf_map.c @@ -120,7 +120,6 @@ int32_t MapSet(Map *map, const char *key, const void *value, uint32_t valueSize) { struct MapNode *node = NULL; uint32_t hash; - if (map == NULL || key == NULL || value == NULL || valueSize == 0) { return HDF_ERR_INVALID_PARAM; } @@ -149,20 +148,23 @@ int32_t MapSet(Map *map, const char *key, const void *value, uint32_t valueSize) return HDF_SUCCESS; } } + // Increase the bucket size when nodes is nullptr + if (map->nodes == NULL) { + MapInit(map); + MapResize(map, HDF_MIN_MAP_SIZE); + } // Increase the bucket size to decrease the possibility of map search conflict. if (map->nodeSize >= map->bucketSize) { uint32_t size = (map->bucketSize < HDF_MIN_MAP_SIZE) ? HDF_MIN_MAP_SIZE : \ (map->bucketSize << HDF_ENLARGE_FACTOR); MapResize(map, size); } - node = MapCreateNode(key, hash, value, valueSize); if (node == NULL) { return HDF_ERR_INVALID_OBJECT; } MapAddNode(map, node); map->nodeSize++; - return HDF_SUCCESS; }