From f8a513cee895e656563ca90297b46f21e1e1edb3 Mon Sep 17 00:00:00 2001 From: Saturneric Date: Sat, 19 Mar 2022 16:42:47 +0800 Subject: (core, ui): fix codacy issues. 1. The scope of the variable 'r' can be reduced. 2. Class 'IMAPFolder' does not have a copy constructor which is recommended since it has dynamic memory/resource allocation(s). 3. Check buffer boundaries if used in a loop including recursive loops (CWE-120, CWE-20). --- src/ui/thread/FileReadThread.cpp | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) (limited to 'src/ui/thread/FileReadThread.cpp') diff --git a/src/ui/thread/FileReadThread.cpp b/src/ui/thread/FileReadThread.cpp index 258b9405..83731c8a 100644 --- a/src/ui/thread/FileReadThread.cpp +++ b/src/ui/thread/FileReadThread.cpp @@ -47,18 +47,25 @@ void FileReadThread::run() { if (is_regular_file(read_file_path)) { LOG(INFO) << "read open" << read_file_path; - QFile file; - file.setFileName(QString::fromStdString(read_file_path.u8string())); - file.open(QIODevice::ReadOnly); + QFile target_file; + target_file.setFileName(QString::fromStdString(read_file_path.u8string())); + target_file.open(QIODevice::ReadOnly); QByteArray read_buffer; LOG(INFO) << "thread start reading"; const size_t buffer_size = 4096; - while ((read_buffer = file.read(buffer_size)).size() > 0) { + if(!(target_file.isOpen() && target_file.isReadable())) { + LOG(ERROR) << "file not open or not readable"; + if(target_file.isOpen()) + target_file.close(); + return; + } + + while (!target_file.atEnd() && (read_buffer = target_file.read(buffer_size)).size() > 0) { // Check isInterruptionRequested if (QThread::currentThread()->isInterruptionRequested()) { LOG(INFO) << "thread is interruption requested "; - file.close(); + target_file.close(); return; } LOG(INFO) << "block size " << read_buffer.size(); @@ -71,7 +78,7 @@ void FileReadThread::run() { QThread::msleep(128); #endif } - file.close(); + target_file.close(); emit SignalReadDone(); LOG(INFO) << "thread end reading"; } -- cgit v1.2.3