From 33c252b8945e8952cc39db5847c4c72ee3af1f7a Mon Sep 17 00:00:00 2001 From: dailydeath Date: Thu, 26 Jun 2025 14:44:54 +0800 Subject: [PATCH 1/5] UPDATE GeneralTextRecognition --- VisionSDK/GeneralTextRecognition/C++/main.cpp | 7 +++++++ VisionSDK/GeneralTextRecognition/python/main_ocr.py | 12 +++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/VisionSDK/GeneralTextRecognition/C++/main.cpp b/VisionSDK/GeneralTextRecognition/C++/main.cpp index 647c57c1f..a6ea442c7 100644 --- a/VisionSDK/GeneralTextRecognition/C++/main.cpp +++ b/VisionSDK/GeneralTextRecognition/C++/main.cpp @@ -29,6 +29,7 @@ namespace { const int TIME_OUT = 20000; const float SEC2MS = 1000.0; const std::string PICTURE_PATH = "../input_data/"; + const long MAX_FILE_SIZE = 1024 * 1024 * 1024; // 1G } std::string ReadFileContent(const std::string& filePath) @@ -42,6 +43,11 @@ std::string ReadFileContent(const std::string& filePath) file.seekg(0, std::ifstream::end); uint32_t fileSize = file.tellg(); file.seekg(0); + if (fileSize <= 0 || fileSize > MAX_FILE_SIZE){ + file.close(); + LogError << "File size invalid"; + return ""; + } std::vector buffer(fileSize); file.read(buffer.data(), fileSize); file.close(); @@ -71,6 +77,7 @@ APP_ERROR GetPicture(const std::string& filePath, std::vector& pict pictureName.push_back(filename); } } + closedir(dir); return APP_ERR_OK; } diff --git a/VisionSDK/GeneralTextRecognition/python/main_ocr.py b/VisionSDK/GeneralTextRecognition/python/main_ocr.py index efdb38ab5..58e7248ac 100644 --- a/VisionSDK/GeneralTextRecognition/python/main_ocr.py +++ b/VisionSDK/GeneralTextRecognition/python/main_ocr.py @@ -22,6 +22,10 @@ import json from StreamManagerApi import StreamManagerApi, MxDataInput +MAX_FILE_SIZE = 1024 * 1024 * 10 # 10MB +MAX_IMAGE_SIZE = 1024 * 1024 * 1024 # 1G +PATH = "../data/OCR.pipeline" + if __name__ == '__main__': # init stream manager STREAM_NAME = b'OCR' @@ -33,7 +37,10 @@ if __name__ == '__main__': exit() # create streams by pipeline config file - with open("../data/OCR.pipeline", 'rb') as f: + if os.path.getsize(PATH) <= 0 or os.path.getsize(PATH) > MAX_FILE_SIZE: + print("Pipeline file size invalid!") + exit() + with open(PATH, 'rb') as f: pipelineStr = f.read() ret = streamManagerApi.CreateMultipleStreams(pipelineStr) if ret != 0: @@ -54,6 +61,9 @@ if __name__ == '__main__': if lower_file_name.endswith(".jpeg") or lower_file_name.endswith(".jpg") or lower_file_name.endswith(".png"): img_path = os.path.join(DIR_NAME, file_name) print("img_path: ", img_path) + if os.path.getsize(img_path) <= 0 or os.path.getsize(img_path) > MAX_IMAGE_SIZE: + print("Image file size invalid!") + exit() with open(img_path, 'rb') as f: dataInput.data = f.read() -- Gitee From e99b3345bc8d4255eb1f0f65fdcd6dbf44f24614 Mon Sep 17 00:00:00 2001 From: dailydeath Date: Thu, 26 Jun 2025 14:45:28 +0800 Subject: [PATCH 2/5] UPDATE GeneralTextRecognition --- VisionSDK/GeneralTextRecognition/C++/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/VisionSDK/GeneralTextRecognition/C++/main.cpp b/VisionSDK/GeneralTextRecognition/C++/main.cpp index a6ea442c7..61bdef60c 100644 --- a/VisionSDK/GeneralTextRecognition/C++/main.cpp +++ b/VisionSDK/GeneralTextRecognition/C++/main.cpp @@ -77,7 +77,7 @@ APP_ERROR GetPicture(const std::string& filePath, std::vector& pict pictureName.push_back(filename); } } - closedir(dir); + closedir(pDir); return APP_ERR_OK; } -- Gitee From 4f87df3f5f3c6a94f7f7bd83b1309c6e8e78acf8 Mon Sep 17 00:00:00 2001 From: dailydeath Date: Thu, 26 Jun 2025 14:46:00 +0800 Subject: [PATCH 3/5] UPDATE file size check --- VisionSDK/GeneralTextRecognition/C++/main.cpp | 1 - tutorials/OsdSample/C++/main.cpp | 14 ++++++++++++-- tutorials/protocolSample/main.cpp | 10 ++++++++-- tutorials/sampleCustomProto/protoRead/main.py | 9 +++++++-- 4 files changed, 27 insertions(+), 7 deletions(-) diff --git a/VisionSDK/GeneralTextRecognition/C++/main.cpp b/VisionSDK/GeneralTextRecognition/C++/main.cpp index 61bdef60c..fd8eca885 100644 --- a/VisionSDK/GeneralTextRecognition/C++/main.cpp +++ b/VisionSDK/GeneralTextRecognition/C++/main.cpp @@ -44,7 +44,6 @@ std::string ReadFileContent(const std::string& filePath) uint32_t fileSize = file.tellg(); file.seekg(0); if (fileSize <= 0 || fileSize > MAX_FILE_SIZE){ - file.close(); LogError << "File size invalid"; return ""; } diff --git a/tutorials/OsdSample/C++/main.cpp b/tutorials/OsdSample/C++/main.cpp index 917d22e65..b067a400e 100644 --- a/tutorials/OsdSample/C++/main.cpp +++ b/tutorials/OsdSample/C++/main.cpp @@ -23,6 +23,8 @@ namespace { const int SLEEP_TIME = 2; +const long MAX_IMAGE_SIZE = 1024 * 1024 * 1024; // 1G +const long MAX_FILE_SIZE = 1024 * 1024 * 100; // 100MB APP_ERROR ReadFile(const std::string& filePath, MxStream::MxstDataInput& dataBuffer) { char c[PATH_MAX + 1] = { 0x00 }; @@ -48,7 +50,7 @@ APP_ERROR ReadFile(const std::string& filePath, MxStream::MxstDataInput& dataBuf long fileSize = ftell(fp); fseek(fp, 0, SEEK_SET); // If file not empty, read it into FileInfo and return it - if (fileSize > 0) { + if (fileSize > 0 && fileSize < MAX_IMAGE_SIZE) { dataBuffer.dataSize = fileSize; dataBuffer.dataPtr = new (std::nothrow) uint32_t[fileSize]; if (dataBuffer.dataPtr == nullptr) { @@ -79,6 +81,10 @@ std::string ReadFileContent(const std::string& filePath) file.seekg(0, std::ifstream::end); uint32_t fileSize = file.tellg(); file.seekg(0); + if (fileSize <=0 || fileSize > MAX_FILE_SIZE){ + LogError << "Invalid file size"; + return ""; + } std::vector buffer; buffer.resize(fileSize); file.read(buffer.data(), fileSize); @@ -97,6 +103,10 @@ std::string ReadPipelineConfig(const std::string& pipelineConfigPath) file.seekg(0, std::ifstream::end); uint32_t fileSize = file.tellg(); file.seekg(0); + if (fileSize <=0 || fileSize > MAX_FILE_SIZE){ + LogError << "Invalid file size"; + return ""; + } std::unique_ptr data(new char[fileSize]); file.read(data.get(), fileSize); file.close(); @@ -163,7 +173,7 @@ int main(int argc, char* argv[]) // destroy streams mxStreamManager.DestroyAllStreams(); - delete dataBuffer.dataPtr; + delete[] dataBuffer.dataPtr; dataBuffer.dataPtr = nullptr; return 0; diff --git a/tutorials/protocolSample/main.cpp b/tutorials/protocolSample/main.cpp index a05cc1736..282f63eae 100644 --- a/tutorials/protocolSample/main.cpp +++ b/tutorials/protocolSample/main.cpp @@ -110,6 +110,8 @@ namespace { constexpr int COS_LEN_VALUE = 416; constexpr int COS_FORMAT_VALUE = 12; constexpr bool USE_SENDDATA = true; // switch different send data method + const long MAX_IMAGE_SIZE = 1024 * 1024 * 1024; // 1G + const long MAX_FILE_SIZE = 1024 * 1024 * 100; // 100MB // read data File to MxstDataInput structure APP_ERROR ReadFile(const std::string& filePath, MxStream::MxstDataInput& dataBuffer) @@ -137,7 +139,7 @@ namespace { long fileSize = ftell(fp); fseek(fp, 0, SEEK_SET); // If file not empty, read it into FileInfo and return it - if (fileSize > 0) { + if (fileSize > 0 && fileSize < MAX_IMAGE_SIZE) { dataBuffer.dataSize = fileSize; dataBuffer.dataPtr = new (std::nothrow) uint32_t[fileSize]; if (dataBuffer.dataPtr == nullptr) { @@ -169,6 +171,10 @@ namespace { file.seekg(0, std::ifstream::end); uint32_t fileSize = file.tellg(); file.seekg(0); + if (fileSize <=0 || fileSize > MAX_FILE_SIZE){ + LogError << "Invalid file size"; + return ""; + } auto data = std::unique_ptr(new char[fileSize]); file.read(data.get(), fileSize); file.close(); @@ -377,7 +383,7 @@ int main(int argc, char* argv[]) // destroy streams mxStreamManager.DestroyAllStreams(); - delete dataBuffer.dataPtr; + delete[] dataBuffer.dataPtr; dataBuffer.dataPtr = nullptr; return 0; diff --git a/tutorials/sampleCustomProto/protoRead/main.py b/tutorials/sampleCustomProto/protoRead/main.py index 2ff67a3b6..031b18565 100644 --- a/tutorials/sampleCustomProto/protoRead/main.py +++ b/tutorials/sampleCustomProto/protoRead/main.py @@ -20,9 +20,11 @@ limitations under the License. from StreamManagerApi import StreamManagerApi, MxDataInput, StringVector import sys +import os sys.path.append("../../proto") import mxpiSampleProto_pb2 as mxpiSampleProto - +MAX_FILE_SIZE = 1024 * 1204 * 10 # 10MB +PATH = "../pipeline/Sample_proto.pipeline" if __name__ == '__main__': # init stream manager streamManagerApi = StreamManagerApi() @@ -32,7 +34,10 @@ if __name__ == '__main__': exit() # create streams by pipeline config file - with open("../pipeline/Sample_proto.pipeline", 'rb') as f: + if os.path.getsize(PATH) <=0 or os.path.getsize(PATH) > MAX_FILE_SIZE: + print("Pipeline file size invalid!") + exit() + with open(PATH, 'rb') as f: pipelineStr = f.read() ret = streamManagerApi.CreateMultipleStreams(pipelineStr) if ret != 0: -- Gitee From cd13ecdfc3ae353b0a86561d5c031d9009fb1aa1 Mon Sep 17 00:00:00 2001 From: dailydeath Date: Thu, 26 Jun 2025 14:46:06 +0800 Subject: [PATCH 4/5] UPDATE file size check --- VisionSDK/GeneralTextRecognition/C++/main.cpp | 2 +- tutorials/OsdSample/C++/main.cpp | 4 ++-- tutorials/protocolSample/main.cpp | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/VisionSDK/GeneralTextRecognition/C++/main.cpp b/VisionSDK/GeneralTextRecognition/C++/main.cpp index fd8eca885..d5d080d1f 100644 --- a/VisionSDK/GeneralTextRecognition/C++/main.cpp +++ b/VisionSDK/GeneralTextRecognition/C++/main.cpp @@ -43,7 +43,7 @@ std::string ReadFileContent(const std::string& filePath) file.seekg(0, std::ifstream::end); uint32_t fileSize = file.tellg(); file.seekg(0); - if (fileSize <= 0 || fileSize > MAX_FILE_SIZE){ + if (fileSize == 0 || fileSize > MAX_FILE_SIZE){ LogError << "File size invalid"; return ""; } diff --git a/tutorials/OsdSample/C++/main.cpp b/tutorials/OsdSample/C++/main.cpp index b067a400e..e7a0fcaa1 100644 --- a/tutorials/OsdSample/C++/main.cpp +++ b/tutorials/OsdSample/C++/main.cpp @@ -81,7 +81,7 @@ std::string ReadFileContent(const std::string& filePath) file.seekg(0, std::ifstream::end); uint32_t fileSize = file.tellg(); file.seekg(0); - if (fileSize <=0 || fileSize > MAX_FILE_SIZE){ + if (fileSize ==0 || fileSize > MAX_FILE_SIZE){ LogError << "Invalid file size"; return ""; } @@ -103,7 +103,7 @@ std::string ReadPipelineConfig(const std::string& pipelineConfigPath) file.seekg(0, std::ifstream::end); uint32_t fileSize = file.tellg(); file.seekg(0); - if (fileSize <=0 || fileSize > MAX_FILE_SIZE){ + if (fileSize ==0 || fileSize > MAX_FILE_SIZE){ LogError << "Invalid file size"; return ""; } diff --git a/tutorials/protocolSample/main.cpp b/tutorials/protocolSample/main.cpp index 282f63eae..f2c0a9547 100644 --- a/tutorials/protocolSample/main.cpp +++ b/tutorials/protocolSample/main.cpp @@ -171,7 +171,7 @@ namespace { file.seekg(0, std::ifstream::end); uint32_t fileSize = file.tellg(); file.seekg(0); - if (fileSize <=0 || fileSize > MAX_FILE_SIZE){ + if (fileSize ==0 || fileSize > MAX_FILE_SIZE){ LogError << "Invalid file size"; return ""; } -- Gitee From c7c0c1e671a4518de52e1cddae0baefc844e53cd Mon Sep 17 00:00:00 2001 From: dailydeath Date: Thu, 26 Jun 2025 14:46:11 +0800 Subject: [PATCH 5/5] update cleancode --- tutorials/sampleCustomProto/protoRead/main.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tutorials/sampleCustomProto/protoRead/main.py b/tutorials/sampleCustomProto/protoRead/main.py index 031b18565..8df8f236f 100644 --- a/tutorials/sampleCustomProto/protoRead/main.py +++ b/tutorials/sampleCustomProto/protoRead/main.py @@ -17,12 +17,12 @@ See the License for the specific language governing permissions and limitations under the License. """ -from StreamManagerApi import StreamManagerApi, MxDataInput, StringVector - import sys import os sys.path.append("../../proto") import mxpiSampleProto_pb2 as mxpiSampleProto +from StreamManagerApi import StreamManagerApi, MxDataInput, StringVector + MAX_FILE_SIZE = 1024 * 1204 * 10 # 10MB PATH = "../pipeline/Sample_proto.pipeline" if __name__ == '__main__': @@ -34,7 +34,7 @@ if __name__ == '__main__': exit() # create streams by pipeline config file - if os.path.getsize(PATH) <=0 or os.path.getsize(PATH) > MAX_FILE_SIZE: + if os.path.getsize(PATH) <= 0 or os.path.getsize(PATH) > MAX_FILE_SIZE: print("Pipeline file size invalid!") exit() with open(PATH, 'rb') as f: -- Gitee