mirror of
https://github.com/openharmony/drivers_framework.git
synced 2026-07-20 19:27:49 -04:00
fix:modify security method and fix file path parsing
Signed-off-by: yue <yueyan4@huawei.com>
This commit is contained in:
@@ -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)
|
||||
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -106,10 +106,20 @@ bool CodeEmitter::NeedFlag(const AutoPtr<ASTMethod> &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 {
|
||||
|
||||
@@ -83,7 +83,7 @@ protected:
|
||||
|
||||
bool NeedFlag(const AutoPtr<ASTMethod> &method);
|
||||
|
||||
String GetFilePath(const String &outDir);
|
||||
String GetFileParentPath(const String &outDir);
|
||||
|
||||
String PackageToFilePath(const String &packageName);
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -15,7 +15,6 @@
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#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;
|
||||
|
||||
@@ -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)) {
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
|
||||
#include "util/string.h"
|
||||
|
||||
#include <cstdio>
|
||||
#include <atomic>
|
||||
#include <cctype>
|
||||
#include <cstddef>
|
||||
@@ -15,7 +16,6 @@
|
||||
#include <cstring>
|
||||
#include <new>
|
||||
|
||||
#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<SharedData *>(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<SharedData *>(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;
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -7,7 +7,9 @@
|
||||
*/
|
||||
|
||||
#include "util/string_builder.h"
|
||||
#include "securec.h"
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstring>
|
||||
#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;
|
||||
|
||||
+5
-3
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user