mirror of
https://github.com/zeromq/libzmq.git
synced 2024-12-26 23:01:04 +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:
parent
f049edbf12
commit
d16db180d3
14
src/blob.hpp
14
src/blob.hpp
@ -81,7 +81,7 @@ struct blob_t
|
||||
_size (size_),
|
||||
_owned (true)
|
||||
{
|
||||
alloc_assert (_data);
|
||||
alloc_assert (!_size || _data);
|
||||
}
|
||||
|
||||
// Creates a blob_t of a given size, an initializes content by copying
|
||||
@ -91,9 +91,11 @@ struct blob_t
|
||||
_size (size_),
|
||||
_owned (true)
|
||||
{
|
||||
alloc_assert (_data);
|
||||
alloc_assert (!size_ || _data);
|
||||
if (size_ && _data) {
|
||||
memcpy (_data, data_, size_);
|
||||
}
|
||||
}
|
||||
|
||||
// Creates a blob_t for temporary use that only references a
|
||||
// pre-allocated block of data.
|
||||
@ -126,22 +128,26 @@ struct blob_t
|
||||
{
|
||||
clear ();
|
||||
_data = static_cast<unsigned char *> (malloc (other_._size));
|
||||
alloc_assert (_data);
|
||||
alloc_assert (!other_._size || _data);
|
||||
_size = other_._size;
|
||||
_owned = true;
|
||||
if (_size && _data) {
|
||||
memcpy (_data, other_._data, _size);
|
||||
}
|
||||
}
|
||||
|
||||
// Sets a blob_t to a copy of a given buffer.
|
||||
void set (const unsigned char *const data_, const size_t size_)
|
||||
{
|
||||
clear ();
|
||||
_data = static_cast<unsigned char *> (malloc (size_));
|
||||
alloc_assert (_data);
|
||||
alloc_assert (!size_ || _data);
|
||||
_size = size_;
|
||||
_owned = true;
|
||||
if (size_ && _data) {
|
||||
memcpy (_data, data_, size_);
|
||||
}
|
||||
}
|
||||
|
||||
// Empties a blob_t.
|
||||
void clear ()
|
||||
|
Loading…
x
Reference in New Issue
Block a user