Skip to content

Commit c0e4e7f

Browse files
committed
Fix out of bounds access
Admittidely, code isn't particularly nice but I don't see any obvious better way. Consider len=2, h1_end underflows and hw_begin = 2.
1 parent 35426c3 commit c0e4e7f

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

site/src/comparison.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,10 +1247,20 @@ impl HistoricalData {
12471247

12481248
let len = pcs.len();
12491249
let (h1_end, h2_begin) = if len % 2 == 0 {
1250-
(len / 2 - 2, len / 2 + 1)
1250+
if len / 2 < 2 {
1251+
(0, len / 2 + 1)
1252+
} else {
1253+
(len / 2 - 2, len / 2 + 1)
1254+
}
12511255
} else {
1252-
(len / 2 - 1, len / 2 + 1)
1256+
if len / 2 < 1 {
1257+
(0, len / 2 + 1)
1258+
} else {
1259+
(len / 2 - 1, len / 2 + 1)
1260+
}
12531261
};
1262+
let h2_begin = std::cmp::min(len - 1, h2_begin);
1263+
12541264
let q1 = median(&pcs[..=h1_end]);
12551265
let q3 = median(&pcs[h2_begin..]);
12561266

0 commit comments

Comments
 (0)