Skip to content

Commit 2c40753

Browse files
committed
fix(tui): prevent usize to u16 overflow in card count displays
1 parent de6da50 commit 2c40753

3 files changed

Lines changed: 9 additions & 5 deletions

File tree

src/cortex-tui/src/cards/commands.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,9 @@ impl CardView for CommandsCard {
225225

226226
fn desired_height(&self, max_height: u16, _width: u16) -> u16 {
227227
// Base height for list items + search bar + some padding
228-
let command_count = self.commands.len() as u16;
229-
let content_height = command_count + 2; // +2 for search bar and padding
228+
// Use saturating conversion to prevent overflow when count > u16::MAX
229+
let command_count = u16::try_from(self.commands.len()).unwrap_or(u16::MAX);
230+
let content_height = command_count.saturating_add(2); // +2 for search bar and padding
230231

231232
// Clamp between min 5 and max 14, respecting max_height
232233
content_height.clamp(5, 14).min(max_height)

src/cortex-tui/src/cards/models.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,9 @@ impl CardView for ModelsCard {
147147

148148
fn desired_height(&self, max_height: u16, _width: u16) -> u16 {
149149
// Base height for list items + search bar + some padding
150-
let model_count = self.models.len() as u16;
151-
let content_height = model_count + 2; // +2 for search bar and padding
150+
// Use saturating conversion to prevent overflow when count > u16::MAX
151+
let model_count = u16::try_from(self.models.len()).unwrap_or(u16::MAX);
152+
let content_height = model_count.saturating_add(2); // +2 for search bar and padding
152153

153154
// Clamp between min 5 and max 12, respecting max_height
154155
content_height.clamp(5, 12).min(max_height)

src/cortex-tui/src/cards/sessions.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,9 @@ impl CardView for SessionsCard {
207207

208208
fn desired_height(&self, max_height: u16, _width: u16) -> u16 {
209209
// Base height: sessions + header + search bar + padding
210-
let content_height = self.sessions.len() as u16 + 3;
210+
// Use saturating conversion to prevent overflow when count > u16::MAX
211+
let session_count = u16::try_from(self.sessions.len()).unwrap_or(u16::MAX);
212+
let content_height = session_count.saturating_add(3);
211213
let min_height = 5;
212214
let max_desired = 15;
213215
content_height

0 commit comments

Comments
 (0)