diff --git a/frameworks/native/neural_network_runtime/nncompiled_cache.cpp b/frameworks/native/neural_network_runtime/nncompiled_cache.cpp index 6917b6e0149c9930d18e2e1659a09d54b60c73a7..a75a62d7ac1d869f5895514c6321ed65ebf995a1 100644 --- a/frameworks/native/neural_network_runtime/nncompiled_cache.cpp +++ b/frameworks/native/neural_network_runtime/nncompiled_cache.cpp @@ -86,13 +86,13 @@ OH_NN_ReturnCode NNCompiledCache::Restore(const std::string& cacheDir, LOGE("[NNCompiledCache] Restore failed, fail to get the real path of cacheInfoPath."); return OH_NN_INVALID_PARAMETER; } - if (access(cacheInfoPath.c_str(), F_OK) != 0) { + if (access(path, F_OK) != 0) { LOGE("[NNCompiledCache] Restore failed, cacheInfoPath is not exist."); return OH_NN_INVALID_PARAMETER; } NNCompiledCacheInfo cacheInfo; - OH_NN_ReturnCode ret = CheckCacheInfo(cacheInfo, cacheInfoPath); + OH_NN_ReturnCode ret = CheckCacheInfo(cacheInfo, path); if (ret != OH_NN_SUCCESS) { LOGE("[NNCompiledCache] Restore failed, error happened when calling CheckCacheInfo."); return ret; @@ -111,11 +111,6 @@ OH_NN_ReturnCode NNCompiledCache::Restore(const std::string& cacheDir, for (uint32_t i = 0; i < cacheInfo.fileNumber; ++i) { std::string cacheModelPath = cacheDir + "/" + m_modelName + std::to_string(i) + ".nncache"; - if (access(cacheModelPath.c_str(), 0) != 0) { - LOGE("[NNCompiledCache] Restore failed, %{public}s is not exist.", cacheModelPath.c_str()); - return OH_NN_INVALID_PARAMETER; - } - OHOS::NeuralNetworkRuntime::Buffer modelBuffer; ret = ReadCacheModelFile(cacheModelPath, modelBuffer); if (ret != OH_NN_SUCCESS) { @@ -342,8 +337,17 @@ OH_NN_ReturnCode NNCompiledCache::CheckCacheInfo(NNCompiledCacheInfo& modelCache OH_NN_ReturnCode NNCompiledCache::ReadCacheModelFile(const std::string& filePath, OHOS::NeuralNetworkRuntime::Buffer& cache) const { - // filePath is validate in NNCompiledCache::Restore, no need to check again. - FILE* pFile = fopen(filePath.c_str(), "rb"); + char path[PATH_MAX]; + if (realpath(filePath.c_str(), path) == nullptr) { + LOGE("[NNCompiledCache] ReadCacheModelFile failed, fail to get the real path of filePath."); + return OH_NN_INVALID_PARAMETER; + } + if (access(path, 0) != 0) { + LOGE("[NNCompiledCache] ReadCacheModelFile failed, %{public}s is not exist.", path); + return OH_NN_INVALID_PARAMETER; + } + + FILE* pFile = fopen(path, "rb"); if (pFile == NULL) { LOGE("[NNCompiledCache] ReadCacheModelFile failed, file fopen failed."); return OH_NN_INVALID_FILE; diff --git a/test/unittest/components/nn_compiled_cache/nn_compiled_cache_test.cpp b/test/unittest/components/nn_compiled_cache/nn_compiled_cache_test.cpp index 8cdc1c7301b96a0c4e6951b792564d47b4479727..690be40bdcba150535508935e57dcaebe6eb790f 100644 --- a/test/unittest/components/nn_compiled_cache/nn_compiled_cache_test.cpp +++ b/test/unittest/components/nn_compiled_cache/nn_compiled_cache_test.cpp @@ -466,19 +466,6 @@ HWTEST_F(NNCompiledCacheTest, nncompiledcachetest_setbackend_002, TestSize.Level EXPECT_EQ(OH_NN_SUCCESS, ret); } -/** - * @tc.name: nncompiledcachetest_setmodelname_001 - * @tc.desc: Verify the QuantParams function return nullptr in case of fd -1. - * @tc.type: FUNC - */ -HWTEST_F(NNCompiledCacheTest, nncompiledcachetest_setmodelname_001, TestSize.Level0) -{ - LOGE("SetModelName nncompiledcachetest_setmodelname_001"); - NNCompiledCache nncompiledCache; - std::string m_modelName; - nncompiledCache.SetModelName(m_modelName); -} - /** * @tc.name: nncompiledcachetest_writecacheinfo_001 * @tc.desc: Verify the QuantParams function return nullptr in case of fd -1.