From 391373decd64c0de2118be9d5c2fdfb1e7b15dec Mon Sep 17 00:00:00 2001 From: "zhangyafei.echo" Date: Wed, 10 Apr 2024 15:44:00 +0800 Subject: [PATCH] fix wildcard pointer. Sig:SIG_ApplicationFramework Feature or BugFix: Feature Binary Source: No Signed-off-by: zhangyafei.echo Change-Id: I3b05ba51fc65e71ec4c69244b02d8220a7d05fc9 --- frameworks/native/runtime/js_worker.cpp | 33 +++++++------------ frameworks/native/runtime/js_worker.h | 8 ++--- .../runtime/ohos_js_environment_impl.cpp | 2 ++ test/unittest/runtime_test/js_worker_test.cpp | 3 +- 4 files changed, 20 insertions(+), 26 deletions(-) diff --git a/frameworks/native/runtime/js_worker.cpp b/frameworks/native/runtime/js_worker.cpp index b77785935f..658447e90a 100644 --- a/frameworks/native/runtime/js_worker.cpp +++ b/frameworks/native/runtime/js_worker.cpp @@ -164,8 +164,8 @@ AssetHelper::~AssetHelper() } } -void AssetHelper::operator()(const std::string& uri, uint8_t** buff, size_t* buffSize, std::string& ami, - bool& useSecureMem, bool isRestricted) +void AssetHelper::operator()(const std::string& uri, uint8_t** buff, size_t* buffSize, std::vector& content, + std::string& ami, bool& useSecureMem, bool isRestricted) { if (uri.empty() || buff == nullptr || buffSize == nullptr || workerInfo_ == nullptr) { TAG_LOGE(AAFwkTag::JSRUNTIME, "Input params invalid."); @@ -211,10 +211,10 @@ void AssetHelper::operator()(const std::string& uri, uint8_t** buff, size_t* buf TAG_LOGD(AAFwkTag::JSRUNTIME, "Get asset, ami: %{private}s", ami.c_str()); if (ami.find(CACHE_DIRECTORY) != std::string::npos) { - if (!ReadAmiData(ami, buff, buffSize, useSecureMem, isRestricted)) { + if (!ReadAmiData(ami, buff, buffSize, content, useSecureMem, isRestricted)) { TAG_LOGE(AAFwkTag::JSRUNTIME, "Get buffer by ami failed."); } - } else if (!ReadFilePathData(filePath, buff, buffSize, useSecureMem, isRestricted)) { + } else if (!ReadFilePathData(filePath, buff, buffSize, content, useSecureMem, isRestricted)) { TAG_LOGE(AAFwkTag::JSRUNTIME, "Get buffer by filepath failed."); } } else { @@ -249,10 +249,10 @@ void AssetHelper::operator()(const std::string& uri, uint8_t** buff, size_t* buf ami = workerInfo_->codePath + filePath; TAG_LOGD(AAFwkTag::JSRUNTIME, "Get asset, ami: %{private}s", ami.c_str()); if (ami.find(CACHE_DIRECTORY) != std::string::npos) { - if (!ReadAmiData(ami, buff, buffSize, useSecureMem, isRestricted)) { + if (!ReadAmiData(ami, buff, buffSize, content, useSecureMem, isRestricted)) { TAG_LOGE(AAFwkTag::JSRUNTIME, "Get buffer by ami failed."); } - } else if (!ReadFilePathData(filePath, buff, buffSize, useSecureMem, isRestricted)) { + } else if (!ReadFilePathData(filePath, buff, buffSize, content, useSecureMem, isRestricted)) { TAG_LOGE(AAFwkTag::JSRUNTIME, "Get buffer by filepath failed."); } } @@ -303,7 +303,7 @@ bool AssetHelper::GetSafeData(const std::string& ami, uint8_t** buff, size_t* bu return true; } -bool AssetHelper::ReadAmiData(const std::string& ami, uint8_t** buff, size_t* buffSize, +bool AssetHelper::ReadAmiData(const std::string& ami, uint8_t** buff, size_t* buffSize, std::vector& content, bool& useSecureMem, bool isRestricted) { // Current function is a private, validity of workerInfo_ has been checked by caller. @@ -344,22 +344,14 @@ bool AssetHelper::ReadAmiData(const std::string& ami, uint8_t** buff, size_t* bu return false; } - auto temp = std::make_unique(fileLen); - if (temp == nullptr) { - TAG_LOGE(AAFwkTag::JSRUNTIME, "Alloc mem failed."); - return false; - } - - stream.seekg(0, std::ios::beg); - stream.read(reinterpret_cast(temp.get()), fileLen); - - *buff = temp.get(); - *buffSize = fileLen; + content.resize(fileLen); + stream.seekg(0); + stream.read(reinterpret_cast(content.data()), content.size()); return true; } bool AssetHelper::ReadFilePathData(const std::string& filePath, uint8_t** buff, size_t* buffSize, - bool& useSecureMem, bool isRestricted) + std::vector& content, bool& useSecureMem, bool isRestricted) { auto bundleMgrHelper = DelayedSingleton::GetInstance(); if (bundleMgrHelper == nullptr) { @@ -453,8 +445,7 @@ bool AssetHelper::ReadFilePathData(const std::string& filePath, uint8_t** buff, return false; } - *buff = dataPtr.get(); - *buffSize = fileLen; + content.assign(dataPtr.get(), dataPtr.get() + fileLen); return true; } diff --git a/frameworks/native/runtime/js_worker.h b/frameworks/native/runtime/js_worker.h index 158feecb50..3d979bda9b 100644 --- a/frameworks/native/runtime/js_worker.h +++ b/frameworks/native/runtime/js_worker.h @@ -44,16 +44,16 @@ public: virtual ~AssetHelper(); - void operator()(const std::string& uri, uint8_t** buff, size_t* buffSize, std::string& ami, - bool& useSecureMem, bool isRestricted = false); + void operator()(const std::string& uri, uint8_t** buff, size_t* buffSize, std::vector& content, + std::string& ami, bool& useSecureMem, bool isRestricted = false); private: std::string NormalizedFileName(const std::string& fileName) const; - bool ReadAmiData(const std::string& ami, uint8_t** buff, size_t* buffSize, + bool ReadAmiData(const std::string& ami, uint8_t** buff, size_t* buffSize, std::vector& content, bool& useSecureMem, bool isRestricted); - bool ReadFilePathData(const std::string& filePath, uint8_t** buff, size_t* buffSize, + bool ReadFilePathData(const std::string& filePath, uint8_t** buff, size_t* buffSize, std::vector& content, bool& useSecureMem, bool isRestricted); void GetAmi(std::string& ami, const std::string& filePath); diff --git a/frameworks/native/runtime/ohos_js_environment_impl.cpp b/frameworks/native/runtime/ohos_js_environment_impl.cpp index ae6219e29c..48e269a200 100644 --- a/frameworks/native/runtime/ohos_js_environment_impl.cpp +++ b/frameworks/native/runtime/ohos_js_environment_impl.cpp @@ -160,9 +160,11 @@ void OHOSJsEnvironmentImpl::InitWorkerModule(NativeEngine* engine, std::shared_p { TAG_LOGD(AAFwkTag::JSRUNTIME, "called"); CHECK_POINTER(engine); + CHECK_POINTER(workerInfo); engine->SetInitWorkerFunc(InitWorkerFunc); engine->SetOffWorkerFunc(OffWorkerFunc); engine->SetGetAssetFunc(AssetHelper(workerInfo)); + engine->SetApiVersion(workerInfo->apiTargetVersion); engine->SetGetContainerScopeIdFunc(GetContainerId); engine->SetInitContainerScopeFunc(UpdateContainerScope); diff --git a/test/unittest/runtime_test/js_worker_test.cpp b/test/unittest/runtime_test/js_worker_test.cpp index 30db60238a..516acfa687 100644 --- a/test/unittest/runtime_test/js_worker_test.cpp +++ b/test/unittest/runtime_test/js_worker_test.cpp @@ -84,11 +84,12 @@ HWTEST_F(JsWorkerTest, AssetHelper_0100, TestSize.Level1) std::string uri = "/data"; uint8_t *buff = nullptr; size_t buffSize; + std::vector content; std::string ami; bool useSecureMem; bool isRestricted = false; auto func = TestGetGetAssetFunc(); - func("/data", &buff, &buffSize, ami, useSecureMem, isRestricted); + func("/data", &buff, &buffSize, content, ami, useSecureMem, isRestricted); EXPECT_EQ(useSecureMem, false); }