Skip to content

Commit 9adf56d

Browse files
committed
Validate PiecewiseSequence buffer lengths after copy
Signed-off-by: Daniel King <dan@spiraldb.com>
1 parent 5860e0b commit 9adf56d

5 files changed

Lines changed: 15 additions & 30 deletions

File tree

vortex-array/src/arrays/bool/compute/take.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -175,19 +175,16 @@ where
175175
L: UnsignedPType,
176176
{
177177
let mut values = BitBufferMut::with_capacity(output_len);
178-
let mut computed_len = 0usize;
179178
for (&start, &length) in starts.iter().zip_eq(lengths) {
180179
let start = start.as_();
181180
let length = length.as_();
182-
computed_len = computed_len
183-
.checked_add(length)
184-
.ok_or_else(|| vortex_err!("PiecewiseSequenceArray output length overflows usize"))?;
185181
values.append_buffer(&source.slice(start..).slice(..length));
186182
}
187183

188184
vortex_ensure!(
189-
computed_len == output_len,
190-
"PiecewiseSequenceArray expanded length {computed_len} does not match declared length {output_len}"
185+
values.len() == output_len,
186+
"PiecewiseSequenceArray expanded length {} does not match declared length {output_len}",
187+
values.len()
191188
);
192189
Ok(values.freeze())
193190
}

vortex-array/src/arrays/decimal/compute/take.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -216,19 +216,16 @@ where
216216
T: NativeDecimalType,
217217
{
218218
let mut result = BufferMut::<T>::with_capacity(output_len);
219-
let mut computed_len = 0usize;
220219
for (&start, &length) in starts.iter().zip_eq(lengths) {
221220
let start = start.as_();
222221
let length = length.as_();
223-
computed_len = computed_len
224-
.checked_add(length)
225-
.ok_or_else(|| vortex_err!("PiecewiseSequenceArray output length overflows usize"))?;
226222
result.extend_from_slice(&values[start..][..length]);
227223
}
228224

229225
vortex_ensure!(
230-
computed_len == output_len,
231-
"PiecewiseSequenceArray expanded length {computed_len} does not match declared length {output_len}"
226+
result.len() == output_len,
227+
"PiecewiseSequenceArray expanded length {} does not match declared length {output_len}",
228+
result.len()
232229
);
233230
Ok(result.freeze())
234231
}

vortex-array/src/arrays/primitive/compute/take/mod.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -242,19 +242,16 @@ where
242242
L: UnsignedPType,
243243
{
244244
let mut values = BufferMut::<T>::with_capacity(output_len);
245-
let mut computed_len = 0usize;
246245
for (&start, &length) in starts.iter().zip_eq(lengths) {
247246
let start = start.as_();
248247
let length = length.as_();
249-
computed_len = computed_len
250-
.checked_add(length)
251-
.ok_or_else(|| vortex_err!("PiecewiseSequenceArray output length overflows usize"))?;
252248
values.extend_from_slice(&source[start..][..length]);
253249
}
254250

255251
vortex_ensure!(
256-
computed_len == output_len,
257-
"PiecewiseSequenceArray expanded length {computed_len} does not match declared length {output_len}"
252+
values.len() == output_len,
253+
"PiecewiseSequenceArray expanded length {} does not match declared length {output_len}",
254+
values.len()
258255
);
259256
Ok(values.freeze())
260257
}

vortex-array/src/arrays/varbin/compute/take.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -505,14 +505,10 @@ where
505505
let mut new_offsets = BufferMut::<NewOffset>::with_capacity(output_len + 1);
506506
new_offsets.push(NewOffset::zero());
507507
let mut output_bytes = 0usize;
508-
let mut computed_len = 0usize;
509508

510509
for (&start, &length) in starts.iter().zip_eq(lengths) {
511510
let start = start.as_();
512511
let length = length.as_();
513-
computed_len = computed_len
514-
.checked_add(length)
515-
.ok_or_else(|| vortex_err!("PiecewiseSequenceArray output length overflows usize"))?;
516512
if length == 0 {
517513
continue;
518514
}
@@ -542,8 +538,9 @@ where
542538
.ok_or_else(|| vortex_err!("PiecewiseSequence VarBin output byte length overflow"))?;
543539
}
544540
vortex_ensure!(
545-
computed_len == output_len,
546-
"PiecewiseSequenceArray expanded length {computed_len} does not match declared length {output_len}"
541+
new_offsets.len() == output_len + 1,
542+
"PiecewiseSequenceArray expanded length {} does not match declared length {output_len}",
543+
new_offsets.len() - 1
547544
);
548545

549546
let mut new_data = ByteBufferMut::with_capacity(output_bytes);

vortex-array/src/arrays/varbinview/compute/take.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -190,19 +190,16 @@ where
190190
L: UnsignedPType,
191191
{
192192
let mut views = BufferMut::<BinaryView>::with_capacity(output_len);
193-
let mut computed_len = 0usize;
194193
for (&start, &length) in starts.iter().zip_eq(lengths) {
195194
let start = start.as_();
196195
let length = length.as_();
197-
computed_len = computed_len
198-
.checked_add(length)
199-
.ok_or_else(|| vortex_err!("PiecewiseSequenceArray output length overflows usize"))?;
200196
views.extend_from_slice(&source[start..][..length]);
201197
}
202198

203199
vortex_ensure!(
204-
computed_len == output_len,
205-
"PiecewiseSequenceArray expanded length {computed_len} does not match declared length {output_len}"
200+
views.len() == output_len,
201+
"PiecewiseSequenceArray expanded length {} does not match declared length {output_len}",
202+
views.len()
206203
);
207204
Ok(views.freeze())
208205
}

0 commit comments

Comments
 (0)