0
0
mirror of https://github.com/zeromq/libzmq.git synced 2024-12-27 15:41:05 +08:00

Do not break if malloc size is zero

I get a scenario where `size_` is `0` so there is nothing to `malloc()`.

According to `malloc(3)`:

If size is 0, then malloc() returns either NULL, or a unique pointer value thatcan later be
success‐fully passed to free().

According to `free(3)`:

If ptr is NULL, no operation is performed.

So, if `size_` is null and `_data` also, there is no OOM error.

We can adjust the assert to not fail on this scenario.
This commit is contained in:
Marco Casaroli 2023-01-31 14:36:56 +01:00
parent f049edbf12
commit d16db180d3

View File

@ -81,7 +81,7 @@ struct blob_t
_size (size_), _size (size_),
_owned (true) _owned (true)
{ {
alloc_assert (_data); alloc_assert (!_size || _data);
} }
// Creates a blob_t of a given size, an initializes content by copying // Creates a blob_t of a given size, an initializes content by copying
@ -91,8 +91,10 @@ struct blob_t
_size (size_), _size (size_),
_owned (true) _owned (true)
{ {
alloc_assert (_data); alloc_assert (!size_ || _data);
memcpy (_data, data_, size_); if (size_ && _data) {
memcpy (_data, data_, size_);
}
} }
// Creates a blob_t for temporary use that only references a // Creates a blob_t for temporary use that only references a
@ -126,10 +128,12 @@ struct blob_t
{ {
clear (); clear ();
_data = static_cast<unsigned char *> (malloc (other_._size)); _data = static_cast<unsigned char *> (malloc (other_._size));
alloc_assert (_data); alloc_assert (!other_._size || _data);
_size = other_._size; _size = other_._size;
_owned = true; _owned = true;
memcpy (_data, other_._data, _size); if (_size && _data) {
memcpy (_data, other_._data, _size);
}
} }
// Sets a blob_t to a copy of a given buffer. // Sets a blob_t to a copy of a given buffer.
@ -137,10 +141,12 @@ struct blob_t
{ {
clear (); clear ();
_data = static_cast<unsigned char *> (malloc (size_)); _data = static_cast<unsigned char *> (malloc (size_));
alloc_assert (_data); alloc_assert (!size_ || _data);
_size = size_; _size = size_;
_owned = true; _owned = true;
memcpy (_data, data_, size_); if (size_ && _data) {
memcpy (_data, data_, size_);
}
} }
// Empties a blob_t. // Empties a blob_t.