@@ -115,11 +115,17 @@ impl<T> BufferMut<T> {
115115 let size = capacity
116116 . checked_mul ( size_of :: < T > ( ) )
117117 . vortex_expect ( "buffer capacity overflow" ) ;
118- let allocation_size = size
119- . checked_add ( actual. as_usize ( ) )
120- . vortex_expect ( "buffer capacity overflow" ) ;
121- let layout = Layout :: from_size_align ( allocation_size, 1 )
122- . unwrap_or_else ( |_| vortex_panic ! ( "buffer capacity exceeds maximum allocation size" ) ) ;
118+ let layout = if size == 0 {
119+ Layout :: from_size_align ( 0 , actual. as_usize ( ) )
120+ . unwrap_or_else ( |_| vortex_panic ! ( "invalid empty buffer alignment" ) )
121+ } else {
122+ let allocation_size = size
123+ . checked_add ( actual. as_usize ( ) )
124+ . vortex_expect ( "buffer capacity overflow" ) ;
125+ Layout :: from_size_align ( allocation_size, 1 ) . unwrap_or_else ( |_| {
126+ vortex_panic ! ( "buffer capacity exceeds maximum allocation size" )
127+ } )
128+ } ;
123129 let allocation = Allocation :: allocate ( layout, allocator) ;
124130 let offset = allocation. ptr ( ) . as_ptr ( ) . align_offset ( actual. as_usize ( ) ) ;
125131 Self {
@@ -195,11 +201,16 @@ impl<T> BufferMut<T> {
195201 let size = len
196202 . checked_mul ( size_of :: < T > ( ) )
197203 . vortex_expect ( "buffer length overflow" ) ;
198- let allocation_size = size
199- . checked_add ( actual_alignment. as_usize ( ) )
200- . vortex_expect ( "buffer length overflow" ) ;
201- let layout = Layout :: from_size_align ( allocation_size, 1 )
202- . unwrap_or_else ( |_| vortex_panic ! ( "buffer length exceeds maximum allocation size" ) ) ;
204+ let layout = if size == 0 {
205+ Layout :: from_size_align ( 0 , actual_alignment. as_usize ( ) )
206+ . unwrap_or_else ( |_| vortex_panic ! ( "invalid empty buffer alignment" ) )
207+ } else {
208+ let allocation_size = size
209+ . checked_add ( actual_alignment. as_usize ( ) )
210+ . vortex_expect ( "buffer length overflow" ) ;
211+ Layout :: from_size_align ( allocation_size, 1 )
212+ . unwrap_or_else ( |_| vortex_panic ! ( "buffer length exceeds maximum allocation size" ) )
213+ } ;
203214 let allocation = Allocation :: allocate_zeroed ( layout, allocator) ;
204215 let offset = allocation
205216 . ptr ( )
@@ -455,7 +466,12 @@ impl<T> BufferMut<T> {
455466 . vortex_expect ( "buffer capacity overflow" ) ;
456467 let current_size = self . allocation . size ( ) - self . offset ;
457468 let allocation_size = required_size. max ( current_size. saturating_mul ( 2 ) ) ;
458- let layout = Layout :: from_size_align ( allocation_size, self . allocation . alignment ( ) )
469+ let allocation_alignment = if self . allocation . size ( ) == 0 {
470+ 1
471+ } else {
472+ self . allocation . alignment ( )
473+ } ;
474+ let layout = Layout :: from_size_align ( allocation_size, allocation_alignment)
459475 . unwrap_or_else ( |_| vortex_panic ! ( "buffer capacity exceeds maximum allocation size" ) ) ;
460476
461477 let old_offset = self . offset ;
0 commit comments