From aeb4f5b7fffbfbb53351d114dab341453649cbd2 Mon Sep 17 00:00:00 2001 From: XieHan Date: Wed, 17 Nov 2021 20:23:04 +0800 Subject: [PATCH] Avoid an undefined behavior of memcpy --- src/factory/Workflow.cc | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/src/factory/Workflow.cc b/src/factory/Workflow.cc index 63ffbb53..48db2857 100644 --- a/src/factory/Workflow.cc +++ b/src/factory/Workflow.cc @@ -162,22 +162,23 @@ SubTask *SeriesWork::pop_task() } ParallelWork::ParallelWork(parallel_callback_t&& cb) : - ParallelTask(NULL, 0), + ParallelTask(new SubTask *[2 * 4], 0), callback(std::move(cb)) { - this->all_series = NULL; - this->buf_size = 0; + this->buf_size = 4; + this->all_series = (SeriesWork **)&this->subtasks[4]; this->context = NULL; } ParallelWork::ParallelWork(SeriesWork *const all_series[], size_t n, parallel_callback_t&& cb) : - ParallelTask(new SubTask *[2 * n], n), + ParallelTask(new SubTask *[2 * (n >= 4 ? n : 4)], n), callback(std::move(cb)) { size_t i; - this->all_series = (SeriesWork **)&this->subtasks[n]; + this->buf_size = (n >= 4 ? n : 4); + this->all_series = (SeriesWork **)&this->subtasks[this->buf_size]; for (i = 0; i < n; i++) { assert(!all_series[i]->in_parallel); @@ -186,7 +187,6 @@ ParallelWork::ParallelWork(SeriesWork *const all_series[], size_t n, this->subtasks[i] = all_series[i]->first; } - this->buf_size = n; this->context = NULL; } @@ -196,9 +196,6 @@ void ParallelWork::expand_buf() size_t size; this->buf_size *= 2; - if (this->buf_size == 0) - this->buf_size = 4; - buf = new SubTask *[2 * this->buf_size]; size = this->subtasks_nr * sizeof (void *); memcpy(buf, this->subtasks, size);