Skip to content

Commit 2a3f517

Browse files
committed
fix(buffer): reject zero-sized elements at compile time
Signed-off-by: Nicholas Gates <[email protected]>
1 parent d4eb3e8 commit 2a3f517

4 files changed

Lines changed: 57 additions & 241 deletions

File tree

‎vortex-buffer/src/allocation.rs‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -496,20 +496,19 @@ mod tests {
496496
}
497497

498498
#[test]
499-
fn zero_sized_buffers_do_not_allocate() -> VortexResult<()> {
499+
fn empty_buffers_preserve_allocator_without_allocating() -> VortexResult<()> {
500500
let allocator = TrackingAllocator::default();
501501
let state = Arc::clone(&allocator.state);
502502
let allocator = BufferAllocatorRef::new(allocator);
503-
let mut buffer = BufferMut::<()>::zeroed_in(3, allocator.clone());
504-
buffer.push(());
503+
let buffer = BufferMut::<u32>::zeroed_in(0, allocator.clone());
505504
let buffer = buffer.freeze();
506505
let copy = buffer.clone().into_mut();
507506
assert!(copy.allocator().ptr_eq(&allocator));
508507
let mut buffer = buffer
509508
.try_into_mut()
510509
.map_err(|_| vortex_err!("unique buffer"))?;
511-
buffer.push(());
512-
assert_eq!(buffer.len(), 5);
510+
buffer.reserve(0);
511+
assert!(buffer.is_empty());
513512
assert!(buffer.allocator().ptr_eq(&allocator));
514513
drop((copy, buffer));
515514
assert_eq!(state.allocations.load(Ordering::Relaxed), 0);

‎vortex-buffer/src/buffer.rs‎

Lines changed: 35 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,32 @@ use crate::debug::TruncatedDebug;
2828
use crate::trusted_len::TrustedLen;
2929

3030
/// An immutable buffer of items of `T`.
31+
///
32+
/// Zero-sized element types are rejected at compile time when constructing a buffer.
33+
///
34+
/// ```compile_fail
35+
/// use vortex_buffer::Buffer;
36+
/// let _ = Buffer::<()>::empty();
37+
/// ```
38+
///
39+
/// ```compile_fail
40+
/// use vortex_buffer::Buffer;
41+
/// let _ = Buffer::from(vec![(); 3]);
42+
/// ```
43+
///
44+
/// ```compile_fail
45+
/// use vortex_buffer::{Buffer, ByteBuffer};
46+
/// let _ = Buffer::<()>::from_byte_buffer(ByteBuffer::empty());
47+
/// ```
48+
///
49+
/// ```compile_fail
50+
/// use bytes::Bytes;
51+
/// use vortex_buffer::{Alignment, Buffer};
52+
/// let _ = Buffer::<()>::from_bytes_aligned(Bytes::new(), Alignment::none());
53+
/// ```
3154
#[derive(Clone)]
3255
pub struct Buffer<T> {
33-
/// The first element in this view; may dangle for an empty buffer or zero-sized `T`.
56+
/// The first element in this view; may dangle for an empty buffer.
3457
pub(crate) ptr: NonNull<T>,
3558
/// The number of initialized `T` values visible from `ptr`.
3659
pub(crate) length: usize,
@@ -216,6 +239,7 @@ impl<T> Buffer<T> {
216239
///
217240
/// This does not allocate. Empty buffers use an aligned dangling pointer.
218241
pub fn empty_aligned(alignment: Alignment) -> Self {
242+
const { assert!(size_of::<T>() != 0, "ZSTs are not supported") };
219243
if !alignment.is_aligned_to(Alignment::of::<T>()) {
220244
vortex_panic!(
221245
"Alignment {} must align to the scalar type's alignment {}",
@@ -263,12 +287,9 @@ impl<T> Buffer<T> {
263287
/// ## Panics
264288
///
265289
/// Panics if the buffer is not aligned to the given alignment, if the length is not a multiple
266-
/// of the size of `T`, if `T` is zero-sized, or if the given alignment is not aligned to that of `T`.
290+
/// of the size of `T`, or if the given alignment is not aligned to that of `T`.
267291
pub fn from_byte_buffer_aligned(buffer: ByteBuffer, alignment: Alignment) -> Self {
268-
assert!(
269-
size_of::<T>() != 0,
270-
"cannot infer a zero-sized element count from bytes"
271-
);
292+
const { assert!(size_of::<T>() != 0, "ZSTs are not supported") };
272293
if !alignment.is_aligned_to(Alignment::of::<T>()) {
273294
vortex_panic!(
274295
"Alignment {} must be compatible with the scalar type's alignment {}",
@@ -299,12 +320,9 @@ impl<T> Buffer<T> {
299320
/// ## Panics
300321
///
301322
/// Panics if the buffer is not aligned to the size of `T`, or the length is not a multiple of
302-
/// the size of `T`, or if `T` is zero-sized.
323+
/// the size of `T`.
303324
pub fn from_bytes_aligned(bytes: Bytes, alignment: Alignment) -> Self {
304-
assert!(
305-
size_of::<T>() != 0,
306-
"cannot infer a zero-sized element count from bytes"
307-
);
325+
const { assert!(size_of::<T>() != 0, "ZSTs are not supported") };
308326
if !alignment.is_aligned_to(Alignment::of::<T>()) {
309327
vortex_panic!(
310328
"Alignment {} must be compatible with the scalar type's alignment {}",
@@ -555,7 +573,7 @@ impl<T> Buffer<T> {
555573
let subset_end = subset_start
556574
.checked_add(size_of_val(subset))
557575
.vortex_expect("slice_ref address overflow");
558-
if subset.len() > self.len() || subset_start < start || subset_end > end {
576+
if subset_start < start || subset_end > end {
559577
vortex_panic!("slice_ref subset must be contained in the buffer");
560578
}
561579

@@ -627,9 +645,7 @@ impl<T> Buffer<T> {
627645
match Arc::try_unwrap(backing) {
628646
Ok(BufferBacking::Owned(allocation)) => {
629647
let offset = ptr.addr().get() - allocation.ptr().addr().get();
630-
let capacity = if size_of::<T>() == 0 {
631-
usize::MAX
632-
} else if allocation.size() == 0 {
648+
let capacity = if allocation.size() == 0 {
633649
0
634650
} else {
635651
(allocation.size() - offset) / size_of::<T>()
@@ -805,10 +821,11 @@ where
805821
T: Send + Sync + 'static,
806822
{
807823
fn from(value: Vec<T>) -> Self {
824+
const { assert!(size_of::<T>() != 0, "ZSTs are not supported") };
808825
let length = value.len();
809826
let alignment = Alignment::of::<T>();
810827
if std::mem::needs_drop::<T>() {
811-
// Keep the typed owner so its elements are dropped, including zero-sized elements.
828+
// Keep the typed owner so its elements are dropped.
812829
Self {
813830
ptr: NonNull::new(value.as_ptr().cast_mut())
814831
.vortex_expect("a Vec always has a non-null pointer"),
@@ -882,7 +899,7 @@ fn empty_ptr<T>() -> NonNull<T> {
882899

883900
/// Owned iterator over a [`Buffer`].
884901
pub struct BufferIterator<T: Copy> {
885-
// Keep the buffer alive; its length also counts the remaining zero-sized elements.
902+
// Keep the buffer alive for the duration of the iteration.
886903
_buffer: Buffer<T>,
887904
ptr: *const T,
888905
end: *const T,
@@ -898,11 +915,6 @@ impl<T: Copy> Iterator for BufferIterator<T> {
898915

899916
#[inline]
900917
fn next(&mut self) -> Option<Self::Item> {
901-
if size_of::<T>() == 0 {
902-
let value = self._buffer.as_slice().last().copied()?;
903-
self._buffer.length -= 1;
904-
return Some(value);
905-
}
906918
if self.ptr == self.end {
907919
None
908920
} else {
@@ -915,12 +927,7 @@ impl<T: Copy> Iterator for BufferIterator<T> {
915927

916928
#[inline]
917929
fn size_hint(&self) -> (usize, Option<usize>) {
918-
let remaining = if size_of::<T>() == 0 {
919-
self._buffer.length
920-
} else {
921-
// SAFETY: both cursors belong to the buffer and T is not zero-sized.
922-
(unsafe { self.end.offset_from(self.ptr) }) as usize
923-
};
930+
let remaining = unsafe { self.end.offset_from(self.ptr) } as usize;
924931
(remaining, Some(remaining))
925932
}
926933
}

‎vortex-buffer/src/buffer_mut.rs‎

Lines changed: 18 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,26 @@ use crate::debug::TruncatedDebug;
2323
use crate::trusted_len::TrustedLen;
2424

2525
/// A mutable buffer that maintains a runtime-defined alignment through resizing operations.
26+
///
27+
/// Zero-sized element types are rejected at compile time when constructing a buffer.
28+
///
29+
/// ```compile_fail
30+
/// use vortex_buffer::BufferMut;
31+
/// let _ = BufferMut::<()>::empty();
32+
/// ```
33+
///
34+
/// ```compile_fail
35+
/// use vortex_buffer::BufferMut;
36+
/// let _ = BufferMut::<()>::zeroed(3);
37+
/// ```
2638
pub struct BufferMut<T> {
2739
/// The owned allocation, including any bytes before `ptr` used for alignment.
2840
pub(crate) allocation: Allocation,
29-
/// The first element, aligned to `alignment`; it may dangle for an empty or zero-sized buffer.
41+
/// The first element, aligned to `alignment`; it may dangle for an empty buffer.
3042
pub(crate) ptr: std::ptr::NonNull<T>,
3143
/// The number of initialized `T` values starting at `ptr`.
3244
pub(crate) length: usize,
33-
/// The number of `T` values that fit from `ptr`; this is `usize::MAX` for zero-sized `T`.
45+
/// The number of `T` values that fit from `ptr`.
3446
pub(crate) capacity: usize,
3547
/// The minimum alignment maintained for `ptr` across reallocations.
3648
pub(crate) alignment: Alignment,
@@ -106,6 +118,7 @@ impl<T> BufferMut<T> {
106118
preferred_alignment: Option<Alignment>,
107119
allocator: BufferAllocatorRef,
108120
) -> Self {
121+
const { assert!(size_of::<T>() != 0, "ZSTs are not supported") };
109122
let actual = max(
110123
alignment,
111124
preferred_alignment.unwrap_or(Alignment::of::<u8>()),
@@ -137,11 +150,7 @@ impl<T> BufferMut<T> {
137150
let offset = allocation.ptr().as_ptr().align_offset(actual.as_usize());
138151
// SAFETY: the allocation includes enough padding to reach this aligned pointer.
139152
let ptr = unsafe { allocation.ptr().add(offset).cast() };
140-
let capacity = if size_of::<T>() == 0 {
141-
usize::MAX
142-
} else {
143-
(allocation.size() - offset) / size_of::<T>()
144-
};
153+
let capacity = (allocation.size() - offset) / size_of::<T>();
145154
Self {
146155
allocation,
147156
ptr,
@@ -210,6 +219,7 @@ impl<T> BufferMut<T> {
210219
preferred_alignment: Option<Alignment>,
211220
allocator: BufferAllocatorRef,
212221
) -> Self {
222+
const { assert!(size_of::<T>() != 0, "ZSTs are not supported") };
213223
let preferred_alignment = preferred_alignment.unwrap_or(Alignment::of::<u8>());
214224
let actual_alignment = max(preferred_alignment, alignment);
215225
let size = len
@@ -232,11 +242,7 @@ impl<T> BufferMut<T> {
232242
.align_offset(actual_alignment.as_usize());
233243
// SAFETY: the allocation includes enough padding to reach this aligned pointer.
234244
let ptr = unsafe { allocation.ptr().add(offset).cast() };
235-
let capacity = if size_of::<T>() == 0 {
236-
usize::MAX
237-
} else {
238-
(allocation.size() - offset) / size_of::<T>()
239-
};
245+
let capacity = (allocation.size() - offset) / size_of::<T>();
240246
Self {
241247
allocation,
242248
ptr,
@@ -843,14 +849,6 @@ impl<T> BufferMut<T> {
843849
/// We use the lower bound hint on the iterator to manually write data, and then we continue to
844850
/// push items normally past the lower bound.
845851
fn extend_iter(&mut self, mut iter: impl Iterator<Item = T>) {
846-
// Pointer differences cannot count zero-sized elements.
847-
if size_of::<T>() == 0 {
848-
for item in iter {
849-
self.push(item);
850-
}
851-
return;
852-
}
853-
854852
// Since we do not know the length of the iterator, we can only guess how much memory we
855853
// need to reserve. Note that these hints may be inaccurate.
856854
let (lower_bound, _) = iter.size_hint();
@@ -901,14 +899,6 @@ impl<T> BufferMut<T> {
901899
/// The caller guarantees that the iterator will have a trusted upper bound, which allows the
902900
/// implementation to reserve all of the memory needed up front.
903901
pub fn extend_trusted<I: TrustedLen<Item = T>>(&mut self, iter: I) {
904-
// Pointer differences cannot count zero-sized elements.
905-
if size_of::<T>() == 0 {
906-
for item in iter {
907-
self.push(item);
908-
}
909-
return;
910-
}
911-
912902
let (_, upper_bound) = iter.size_hint();
913903
self.reserve(
914904
upper_bound
@@ -1050,15 +1040,6 @@ mod test {
10501040
assert_eq!(buffer.capacity(), capacity * 2);
10511041
}
10521042

1053-
#[test]
1054-
fn zero_sized_elements_grow() {
1055-
let mut buffer = BufferMut::<()>::empty();
1056-
assert_eq!(buffer.capacity(), usize::MAX);
1057-
buffer.push(());
1058-
buffer.push(());
1059-
assert_eq!(buffer.len(), 2);
1060-
}
1061-
10621043
#[test]
10631044
fn static_growth_copies_live_data() {
10641045
let mut buffer = BufferMut::<u32>::with_capacity(1);

0 commit comments

Comments
 (0)