Skip to content

Commit

Permalink
Merge #76
Browse files Browse the repository at this point in the history
76: hyperloglog: fix potential array out-of-bounds in bias correction r=crepererum a=crepererum

Fixes #74

Co-authored-by: Marco Neumann <marco@crepererum.net>
  • Loading branch information
bors[bot] and crepererum authored Sep 8, 2019
2 parents 9b7aaec + 5cd67f3 commit 5b58115
Showing 1 changed file with 78 additions and 2 deletions.
80 changes: 78 additions & 2 deletions src/hyperloglog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,15 @@ where
// binary search first nearest neighbor
let lookup_array = RAW_ESTIMATE_DATA_VEC[self.b - RAW_ESTIMATE_DATA_OFFSET];
let mut idx_left = match lookup_array.binary_search_by(|v| v.partial_cmp(&e).unwrap()) {
Ok(i) => Some(i), // exact match
Err(i) => Some(i), // no match, i points to left neighbor
Ok(i) => Some(i), // exact match
Err(i) => {
// no match, i points to left neighbor
if i < lookup_array.len() {
Some(i) // neighbor is in-range
} else {
Some(i - 1) // neighbor at the end of the array
}
}
};

let mut idx_right = match idx_left {
Expand Down Expand Up @@ -586,4 +593,73 @@ mod tests {
assert_eq!(hll.count(), 1);
assert!(!hll.is_empty());
}

#[test]
fn issue_74() {
let panic_data = vec![
"ofr-1-1517560282779878449",
"ofr-1-1517589543534331019",
"ofr-1-1517590532450550786",
"ofr-1-1517644560121333465",
"ofr-1-1517746611185649116",
"ofr-1-1518051376300950677",
"ofr-1-1518484387459892414",
"ofr-1-1518488008830355319",
"ofr-1-1518488407814571264",
"ofr-1-1518561818180978525",
"ofr-1-1518678274740717330",
"ofr-1-1519461045930165638",
"ofr-1-1519470647696557288",
"ofr-1-1519567114956309703",
"ofr-1-1519653616441755584",
"ofr-1-1519655049912256356",
"ofr-1-1520105514088138521",
"ofr-1-1520294225822221822",
"ofr-1-1520319017418884884",
"ofr-1-1520505982893295286",
"ofr-1-1520553027150677707",
"ofr-1-1520925550686111649",
"ofr-1-1520927095122167663",
"ofr-1-1521290010424640726",
"ofr-1-1521458659554886917",
"ofr-1-1521943577454052994",
"ofr-1-1521971260753839540",
"ofr-1-1522000670785668758",
"ofr-1-1522043914876749176",
"ofr-1-1522206531944580201",
"ofr-1-1522234960069920034",
"ofr-1-1522333169901504119",
"ofr-1-1522363887846294936",
"ofr-1-1522484446749918495",
"ofr-1-1522600458059122179",
"ofr-1-1522687450205783676",
"ofr-1-1522765602785461678",
"ofr-1-1522815395559769187",
"ofr-1-1522839112893465736",
"ofr-1-1523001178903151627",
"ofr-1-1523018056414397988",
"ofr-1-1523096555609261412",
"ofr-1-1523103371222189143",
"ofr-1-1523256333918667890",
"ofr-1-1523270427746895732",
"ofr-1-1523411745695466681",
"ofr-1-1523630566301631536",
"ofr-1-1523839014553388093",
"ofr-1-1523894230803940925",
"ofr-1-1523931915564221543",
"ofr-1-1524104734332815100",
"ofr-1-1524113364834715372",
"ofr-1-1524209603273164167",
"ofr-1-1524276802153219312",
"ofr-1-1524554894791804305",
"ofr-1-1524621894100584193",
];

let mut hll = HyperLogLog::new(4);
for entry in &panic_data {
hll.add(entry);
}

hll.count();
}
}

0 comments on commit 5b58115

Please sign in to comment.