Skip to content

Commit 80f5f1e

Browse files
Merge pull request #2586 from quickwit-oss/issue/2577-get_batch_multiply_overflow
follow up on the fix of multiply with overflow
2 parents 371dba9 + 519e5d2 commit 80f5f1e

File tree

9 files changed

+23
-19
lines changed

9 files changed

+23
-19
lines changed

bitpacker/src/bitpacker.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ impl BitPacker {
6565

6666
#[derive(Clone, Debug, Default, Copy)]
6767
pub struct BitUnpacker {
68-
num_bits: u32,
68+
num_bits: usize,
6969
mask: u64,
7070
}
7171

@@ -83,7 +83,7 @@ impl BitUnpacker {
8383
(1u64 << num_bits) - 1u64
8484
};
8585
BitUnpacker {
86-
num_bits: u32::from(num_bits),
86+
num_bits: usize::from(num_bits),
8787
mask,
8888
}
8989
}
@@ -94,7 +94,7 @@ impl BitUnpacker {
9494

9595
#[inline]
9696
pub fn get(&self, idx: u32, data: &[u8]) -> u64 {
97-
let addr_in_bits = idx as usize * self.num_bits as usize;
97+
let addr_in_bits = idx as usize * self.num_bits;
9898
let addr = addr_in_bits >> 3;
9999
if addr + 8 > data.len() {
100100
if self.num_bits == 0 {
@@ -134,12 +134,13 @@ impl BitUnpacker {
134134
"Bitwidth must be <= 32 to use this method."
135135
);
136136

137-
let end_idx = start_idx + output.len() as u32;
137+
let end_idx: u32 = start_idx + output.len() as u32;
138138

139-
let end_bit_read = end_idx * self.num_bits;
139+
// We use `usize` here to avoid overflow issues.
140+
let end_bit_read = (end_idx as usize) * self.num_bits;
140141
let end_byte_read = (end_bit_read + 7) / 8;
141142
assert!(
142-
end_byte_read as usize <= data.len(),
143+
end_byte_read <= data.len(),
143144
"Requested index is out of bounds."
144145
);
145146

@@ -159,24 +160,24 @@ impl BitUnpacker {
159160
// We want the start of the fast track to start align with bytes.
160161
// A sufficient condition is to start with an idx that is a multiple of 8,
161162
// so highway start is the closest multiple of 8 that is >= start_idx.
162-
let entrance_ramp_len = 8 - (start_idx % 8) % 8;
163+
let entrance_ramp_len: u32 = 8 - (start_idx % 8) % 8;
163164

164165
let highway_start: u32 = start_idx + entrance_ramp_len;
165166

166-
if highway_start + BitPacker1x::BLOCK_LEN as u32 > end_idx {
167+
if highway_start + (BitPacker1x::BLOCK_LEN as u32) > end_idx {
167168
// We don't have enough values to have even a single block of highway.
168169
// Let's just supply the values the simple way.
169170
get_batch_ramp(start_idx, output);
170171
return;
171172
}
172173

173-
let num_blocks: u32 = (end_idx - highway_start) / BitPacker1x::BLOCK_LEN as u32;
174+
let num_blocks: usize = (end_idx - highway_start) as usize / BitPacker1x::BLOCK_LEN;
174175

175176
// Entrance ramp
176177
get_batch_ramp(start_idx, &mut output[..entrance_ramp_len as usize]);
177178

178179
// Highway
179-
let mut offset = (highway_start * self.num_bits) as usize / 8;
180+
let mut offset = (highway_start as usize * self.num_bits) / 8;
180181
let mut output_cursor = (highway_start - start_idx) as usize;
181182
for _ in 0..num_blocks {
182183
offset += BitPacker1x.decompress(
@@ -188,7 +189,7 @@ impl BitUnpacker {
188189
}
189190

190191
// Exit ramp
191-
let highway_end = highway_start + num_blocks * BitPacker1x::BLOCK_LEN as u32;
192+
let highway_end: u32 = highway_start + (num_blocks * BitPacker1x::BLOCK_LEN) as u32;
192193
get_batch_ramp(highway_end, &mut output[output_cursor..]);
193194
}
194195

bitpacker/src/blocked_bitpacker.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ struct BlockedBitpackerEntryMetaData {
3434

3535
impl BlockedBitpackerEntryMetaData {
3636
fn new(offset: u64, num_bits: u8, base_value: u64) -> Self {
37-
let encoded = offset | (num_bits as u64) << (64 - 8);
37+
let encoded = offset | (u64::from(num_bits) << (64 - 8));
3838
Self {
3939
encoded,
4040
base_value,

columnar/src/tests.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,6 +715,7 @@ fn test_columnar_merging_number_columns() {
715715
// TODO test required_columns
716716
// TODO document edge case: required_columns incompatible with values.
717717

718+
#[allow(clippy::type_complexity)]
718719
fn columnar_docs_and_remap(
719720
) -> impl Strategy<Value = (Vec<Vec<Vec<(&'static str, ColumnValue)>>>, Vec<RowAddr>)> {
720721
proptest::collection::vec(columnar_docs_strategy(), 2..=3).prop_flat_map(

src/indexer/index_writer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2553,7 +2553,7 @@ mod tests {
25532553
#[test]
25542554
fn test_writer_options_validation() {
25552555
let mut schema_builder = Schema::builder();
2556-
let field = schema_builder.add_bool_field("example", STORED);
2556+
let _field = schema_builder.add_bool_field("example", STORED);
25572557
let index = Index::create_in_ram(schema_builder.build());
25582558

25592559
let opt_wo_threads = IndexWriterOptions::builder().num_worker_threads(0).build();

src/postings/skip.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ fn encode_bitwidth(bitwidth: u8, delta_1: bool) -> u8 {
1515
}
1616

1717
fn decode_bitwidth(raw_bitwidth: u8) -> (u8, bool) {
18-
let delta_1 = (raw_bitwidth >> 6 & 1) != 0;
18+
let delta_1 = ((raw_bitwidth >> 6) & 1) != 0;
1919
let bitwidth = raw_bitwidth & 0x3f;
2020
(bitwidth, delta_1)
2121
}

src/schema/document/default_document.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::schema::field_type::ValueParsingError;
1515
use crate::schema::{Facet, Field, NamedFieldDocument, OwnedValue, Schema};
1616
use crate::tokenizer::PreTokenizedString;
1717

18-
#[repr(packed)]
18+
#[repr(C, packed)]
1919
#[derive(Debug, Clone)]
2020
/// A field value pair in the compact tantivy document
2121
struct FieldValueAddr {
@@ -480,7 +480,7 @@ impl<'a> CompactDocValue<'a> {
480480
type Addr = u32;
481481

482482
#[derive(Clone, Copy, Default)]
483-
#[repr(packed)]
483+
#[repr(C, packed)]
484484
/// The value type and the address to its payload in the container.
485485
struct ValueAddr {
486486
type_id: ValueType,
@@ -734,7 +734,7 @@ mod tests {
734734

735735
#[test]
736736
fn test_json_value() {
737-
let json_str = r#"{
737+
let json_str = r#"{
738738
"toto": "titi",
739739
"float": -0.2,
740740
"bool": true,

sstable/src/delta.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ where
8989

9090
fn encode_keep_add(&mut self, keep_len: usize, add_len: usize) {
9191
if keep_len < FOUR_BIT_LIMITS && add_len < FOUR_BIT_LIMITS {
92-
let b = (keep_len | add_len << 4) as u8;
92+
let b = (keep_len | (add_len << 4)) as u8;
9393
self.block.extend_from_slice(&[b])
9494
} else {
9595
let mut buf = [VINT_MODE; 20];

sstable/src/dictionary.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#![allow(clippy::needless_borrows_for_generic_args)]
2+
13
use std::cmp::Ordering;
24
use std::io;
35
use std::marker::PhantomData;

stacker/src/memory_arena.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ impl Addr {
5454

5555
#[inline]
5656
fn new(page_id: usize, local_addr: usize) -> Addr {
57-
Addr((page_id << NUM_BITS_PAGE_ADDR | local_addr) as u32)
57+
Addr(((page_id << NUM_BITS_PAGE_ADDR) | local_addr) as u32)
5858
}
5959

6060
#[inline]

0 commit comments

Comments
 (0)