Skip to content

Commit 61315ab

Browse files
committed
fix(compact): use saturating subtraction to prevent underflow
1 parent de6da50 commit 61315ab

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/cortex-compact/src/compactor.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,10 @@ impl Compactor {
106106
}];
107107
new_items.extend(items.into_iter().skip(preserved_start));
108108

109-
let tokens_after = current_tokens - tokens_in_compacted + summary_tokens;
109+
// Use saturating arithmetic to prevent underflow if tokens_in_compacted > current_tokens
110+
let tokens_after = current_tokens
111+
.saturating_sub(tokens_in_compacted)
112+
.saturating_add(summary_tokens);
110113

111114
let result =
112115
CompactionResult::success(summary, current_tokens, tokens_after, items_removed);

0 commit comments

Comments
 (0)