Skip to content

Commit

Permalink
Merge pull request #217 from arduino/fix-wno-null
Browse files Browse the repository at this point in the history
Fix: do not call "memcpy" if there is a) no data to copy, or b) the src pointer is null.
  • Loading branch information
facchinm authored Sep 27, 2023
2 parents f1ae7d0 + fc4a570 commit 65633ce
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions api/CanMsg.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) { }
Expand All @@ -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() { }
Expand All @@ -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);
}
Expand Down

0 comments on commit 65633ce

Please sign in to comment.