From fc4a570464c852bf8b464798cf998def82c35e7b Mon Sep 17 00:00:00 2001 From: Alexander Entinger Date: Tue, 26 Sep 2023 10:33:41 +0200 Subject: [PATCH] Fix: do not call "memcpy" if there is a) no data to copy, or the src pointer is null. --- api/CanMsg.h | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/api/CanMsg.h b/api/CanMsg.h index dfece0d6..eaa3f0ed 100644 --- a/api/CanMsg.h +++ b/api/CanMsg.h @@ -45,7 +45,8 @@ class CanMsg : public Printable , data_length{min(can_data_len, MAX_DATA_LENGTH)} , data{0} { - memcpy(data, can_data_ptr, data_length); + if (data_length && can_data_ptr) + memcpy(data, can_data_ptr, data_length); } CanMsg() : CanMsg(0, 0, nullptr) { } @@ -54,7 +55,8 @@ class CanMsg : public Printable { this->id = other.id; this->data_length = other.data_length; - memcpy(this->data, other.data, this->data_length); + if (this->data_length && other.data) + memcpy(this->data, other.data, this->data_length); } virtual ~CanMsg() { } @@ -65,7 +67,8 @@ class CanMsg : public Printable { this->id = other.id; this->data_length = other.data_length; - memcpy(this->data, other.data, this->data_length); + if (this->data_length && other.data) + memcpy(this->data, other.data, this->data_length); } return (*this); }