aboutsummaryrefslogtreecommitdiffstats
path: root/src/core/thread/FileReadTask.cpp
diff options
context:
space:
mode:
authorsaturneric <[email protected]>2024-01-16 03:49:50 +0000
committersaturneric <[email protected]>2024-01-16 03:49:50 +0000
commit4994f4eaa1211d402b791660ad6221154a4c2405 (patch)
treec3f0cdc0008849de79c6bd8030c0c65c5d02f9f3 /src/core/thread/FileReadTask.cpp
parentfix: slove a heap-use-after-free problem (diff)
downloadGpgFrontend-4994f4eaa1211d402b791660ad6221154a4c2405.tar.gz
GpgFrontend-4994f4eaa1211d402b791660ad6221154a4c2405.zip
fix: make task and threading system safer
Diffstat (limited to 'src/core/thread/FileReadTask.cpp')
-rw-r--r--src/core/thread/FileReadTask.cpp17
1 files changed, 9 insertions, 8 deletions
diff --git a/src/core/thread/FileReadTask.cpp b/src/core/thread/FileReadTask.cpp
index 38a0ab37..c757d4c0 100644
--- a/src/core/thread/FileReadTask.cpp
+++ b/src/core/thread/FileReadTask.cpp
@@ -32,14 +32,14 @@ namespace GpgFrontend::UI {
constexpr size_t kBufferSize = 8192;
-FileReadTask::FileReadTask(QString path) : Task("file_read_task") {
+FileReadTask::FileReadTask(QString path)
+ : Task("file_read_task"), read_file_path_(std::move(path)) {
HoldOnLifeCycle(true);
connect(this, &FileReadTask::SignalFileBytesReadNext, this,
- &FileReadTask::read_bytes);
- read_file_path_ = path;
+ &FileReadTask::slot_read_bytes);
}
-void FileReadTask::Run() {
+auto FileReadTask::Run() -> int {
if (QFileInfo(read_file_path_).isFile()) {
GF_CORE_LOG_DEBUG("read open file: {}", read_file_path_);
@@ -49,23 +49,24 @@ void FileReadTask::Run() {
if (!(target_file_.isOpen() && target_file_.isReadable())) {
GF_CORE_LOG_ERROR("file not open or not readable");
if (target_file_.isOpen()) target_file_.close();
- return;
+ return -1;
}
GF_CORE_LOG_DEBUG("started reading: {}", read_file_path_);
- read_bytes();
+ slot_read_bytes();
} else {
emit SignalFileBytesReadEnd();
}
+ return 0;
}
-void FileReadTask::read_bytes() {
+void FileReadTask::slot_read_bytes() {
QByteArray read_buffer;
if (!target_file_.atEnd() &&
(read_buffer = target_file_.read(kBufferSize)).size() > 0) {
GF_CORE_LOG_DEBUG("io thread read bytes: {}", read_buffer.size());
emit SignalFileBytesRead(std::move(read_buffer));
} else {
- GF_CORE_LOG_DEBUG("io read bytes end");
+ GF_CORE_LOG_DEBUG("io thread read bytes end");
emit SignalFileBytesReadEnd();
// announce finish task
emit SignalTaskShouldEnd(0);