Skip to content

Commit

Permalink
auto_array: protect from various calls when length_ is 0
Browse files Browse the repository at this point in the history
cubeb-coreaudio-rs has hit a case when running its tests on MacOS 12
where it fails the `assert(destination && source);` in `PodCopy` because
it tried to push 0 samples to an auto_array of length 0, as the internal
auto_array buffer had not been allocated yet.
  • Loading branch information
Pehrsons committed Dec 12, 2023
1 parent 5f0d173 commit 50de9b8
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/cubeb_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,9 @@ template <typename T> class auto_array {
if (length_ + length > capacity_) {
reserve(length_ + length);
}
PodCopy(data_ + length_, elements, length);
if (data_ && length_) {
PodCopy(data_ + length_, elements, length);
}
length_ += length;
}

Expand All @@ -195,7 +197,9 @@ template <typename T> class auto_array {
if (length_ + length > capacity_) {
reserve(length + length_);
}
PodZero(data_ + length_, length);
if (data_ && length_) {
PodZero(data_ + length_, length);
}
length_ += length;
}

Expand All @@ -208,8 +212,10 @@ template <typename T> class auto_array {
if (length_ + length > capacity_) {
reserve(length + length_);
}
PodMove(data_ + length, data_, length_);
PodZero(data_, length);
if (data_ && length_) {
PodMove(data_ + length, data_, length_);
PodZero(data_, length);
}
length_ += length;
}

Expand All @@ -227,6 +233,9 @@ template <typename T> class auto_array {
if (length > length_) {
return false;
}
if (!length_) {
return true;
}
if (elements) {
PodCopy(elements, data_, length);
}
Expand Down

0 comments on commit 50de9b8

Please sign in to comment.