-
Notifications
You must be signed in to change notification settings - Fork 0
Refacto #32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refacto #32
Changes from 11 commits
8cf63b0
99f6cbd
d196e52
605baf6
045a30d
6f2d489
cd1b2d9
e02f6b8
b706285
0de8576
513e02b
3e14fe5
7fe30d9
1a04e6c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,7 +1,6 @@ | ||||||||||||||||||||||||||||||||||||||
| use super::App; | ||||||||||||||||||||||||||||||||||||||
| use crate::utils::scan::scan_networks; | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| use crossterm::event::KeyEventKind::Press; | ||||||||||||||||||||||||||||||||||||||
| use crossterm::event::{self, Event, KeyCode, KeyEvent, KeyModifiers, poll}; | ||||||||||||||||||||||||||||||||||||||
| use std::io; | ||||||||||||||||||||||||||||||||||||||
| use std::time::Duration; | ||||||||||||||||||||||||||||||||||||||
|
|
@@ -27,120 +26,60 @@ impl App { | |||||||||||||||||||||||||||||||||||||
| /// app.handle_events().unwrap(); | ||||||||||||||||||||||||||||||||||||||
| /// ``` | ||||||||||||||||||||||||||||||||||||||
| pub fn handle_events(&mut self) -> io::Result<()> { | ||||||||||||||||||||||||||||||||||||||
| if poll(Duration::from_micros(1))? { | ||||||||||||||||||||||||||||||||||||||
| match event::read()? { | ||||||||||||||||||||||||||||||||||||||
| Event::Key(KeyEvent { | ||||||||||||||||||||||||||||||||||||||
| code: KeyCode::Char('h'), | ||||||||||||||||||||||||||||||||||||||
| kind: Press, | ||||||||||||||||||||||||||||||||||||||
| .. | ||||||||||||||||||||||||||||||||||||||
| }) => { | ||||||||||||||||||||||||||||||||||||||
| if poll(Duration::from_micros(1))? | ||||||||||||||||||||||||||||||||||||||
| && let Event::Key(KeyEvent { | ||||||||||||||||||||||||||||||||||||||
| code, modifiers, .. | ||||||||||||||||||||||||||||||||||||||
| }) = event::read()? | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| match (code, modifiers) { | ||||||||||||||||||||||||||||||||||||||
| (KeyCode::Char('h'), _) => { | ||||||||||||||||||||||||||||||||||||||
| self.flags.show_help = true; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| Event::Key(KeyEvent { | ||||||||||||||||||||||||||||||||||||||
| code: KeyCode::Char('?'), | ||||||||||||||||||||||||||||||||||||||
| kind: Press, | ||||||||||||||||||||||||||||||||||||||
| .. | ||||||||||||||||||||||||||||||||||||||
| }) => { | ||||||||||||||||||||||||||||||||||||||
| (KeyCode::Char('?'), _) => { | ||||||||||||||||||||||||||||||||||||||
| self.flags.show_help = true; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| Event::Key(KeyEvent { | ||||||||||||||||||||||||||||||||||||||
| code: KeyCode::Esc, | ||||||||||||||||||||||||||||||||||||||
| kind: Press, | ||||||||||||||||||||||||||||||||||||||
| .. | ||||||||||||||||||||||||||||||||||||||
| }) => { | ||||||||||||||||||||||||||||||||||||||
| (KeyCode::Esc, _) => { | ||||||||||||||||||||||||||||||||||||||
| self.exit(); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| Event::Key(KeyEvent { | ||||||||||||||||||||||||||||||||||||||
| code: KeyCode::Char('c'), | ||||||||||||||||||||||||||||||||||||||
| modifiers: KeyModifiers::CONTROL, | ||||||||||||||||||||||||||||||||||||||
| kind: Press, | ||||||||||||||||||||||||||||||||||||||
| .. | ||||||||||||||||||||||||||||||||||||||
| }) => { | ||||||||||||||||||||||||||||||||||||||
| (KeyCode::Char('c'), KeyModifiers::CONTROL) => { | ||||||||||||||||||||||||||||||||||||||
| self.exit(); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| Event::Key(KeyEvent { | ||||||||||||||||||||||||||||||||||||||
| code: KeyCode::Char('r'), | ||||||||||||||||||||||||||||||||||||||
| modifiers: KeyModifiers::CONTROL, | ||||||||||||||||||||||||||||||||||||||
| kind: Press, | ||||||||||||||||||||||||||||||||||||||
| .. | ||||||||||||||||||||||||||||||||||||||
| }) => { | ||||||||||||||||||||||||||||||||||||||
| (KeyCode::Char('r'), KeyModifiers::CONTROL) => { | ||||||||||||||||||||||||||||||||||||||
| scan_networks(self.wifi_list.clone(), self.flags.is_scanning.clone()); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| Event::Key(KeyEvent { | ||||||||||||||||||||||||||||||||||||||
| code: KeyCode::Enter, | ||||||||||||||||||||||||||||||||||||||
| kind: Press, | ||||||||||||||||||||||||||||||||||||||
| .. | ||||||||||||||||||||||||||||||||||||||
| }) => { | ||||||||||||||||||||||||||||||||||||||
| self.prepare_to_connect(); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| Event::Key(KeyEvent { | ||||||||||||||||||||||||||||||||||||||
| code: KeyCode::Char('o'), | ||||||||||||||||||||||||||||||||||||||
| kind: Press, | ||||||||||||||||||||||||||||||||||||||
| .. | ||||||||||||||||||||||||||||||||||||||
| }) => { | ||||||||||||||||||||||||||||||||||||||
| (KeyCode::Char('o'), _) | (KeyCode::Enter, _) => { | ||||||||||||||||||||||||||||||||||||||
| self.prepare_to_connect(); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| Event::Key(KeyEvent { | ||||||||||||||||||||||||||||||||||||||
| code: KeyCode::Up, | ||||||||||||||||||||||||||||||||||||||
| kind: Press, | ||||||||||||||||||||||||||||||||||||||
| .. | ||||||||||||||||||||||||||||||||||||||
| }) => { | ||||||||||||||||||||||||||||||||||||||
| (KeyCode::Up, _) | (KeyCode::Char('k'), _) => { | ||||||||||||||||||||||||||||||||||||||
| self.update_selected_network(-1); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| Event::Key(KeyEvent { | ||||||||||||||||||||||||||||||||||||||
| code: KeyCode::Down, | ||||||||||||||||||||||||||||||||||||||
| kind: Press, | ||||||||||||||||||||||||||||||||||||||
| .. | ||||||||||||||||||||||||||||||||||||||
| }) => { | ||||||||||||||||||||||||||||||||||||||
| (KeyCode::Down, _) | (KeyCode::Char('j'), _) => { | ||||||||||||||||||||||||||||||||||||||
| self.update_selected_network(1); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| // vim style | ||||||||||||||||||||||||||||||||||||||
| Event::Key(KeyEvent { | ||||||||||||||||||||||||||||||||||||||
| code: KeyCode::Char('k'), | ||||||||||||||||||||||||||||||||||||||
| kind: Press, | ||||||||||||||||||||||||||||||||||||||
| .. | ||||||||||||||||||||||||||||||||||||||
| }) => { | ||||||||||||||||||||||||||||||||||||||
| self.update_selected_network(-1); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| Event::Key(KeyEvent { | ||||||||||||||||||||||||||||||||||||||
| code: KeyCode::Char('j'), | ||||||||||||||||||||||||||||||||||||||
| kind: Press, | ||||||||||||||||||||||||||||||||||||||
| .. | ||||||||||||||||||||||||||||||||||||||
| }) => { | ||||||||||||||||||||||||||||||||||||||
| self.update_selected_network(1); | ||||||||||||||||||||||||||||||||||||||
| (KeyCode::Char('d'), _) => { | ||||||||||||||||||||||||||||||||||||||
| self.set_delete_confirmation(); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| Event::Key(KeyEvent { | ||||||||||||||||||||||||||||||||||||||
| code: KeyCode::Char('d'), | ||||||||||||||||||||||||||||||||||||||
| kind: Press, | ||||||||||||||||||||||||||||||||||||||
| .. | ||||||||||||||||||||||||||||||||||||||
| }) => { | ||||||||||||||||||||||||||||||||||||||
| if self | ||||||||||||||||||||||||||||||||||||||
| .wifi_list | ||||||||||||||||||||||||||||||||||||||
| .read() | ||||||||||||||||||||||||||||||||||||||
| .expect("Wifi list lock poisoned while deleting")[self.selected] | ||||||||||||||||||||||||||||||||||||||
| .is_saved | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| self.flags.show_delete_confirmation = true; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| Event::Key(KeyEvent { | ||||||||||||||||||||||||||||||||||||||
| code: KeyCode::Char('s'), | ||||||||||||||||||||||||||||||||||||||
| kind: Press, | ||||||||||||||||||||||||||||||||||||||
| .. | ||||||||||||||||||||||||||||||||||||||
| }) => { | ||||||||||||||||||||||||||||||||||||||
| (KeyCode::Char('s'), _) => { | ||||||||||||||||||||||||||||||||||||||
| self.open_saved_list(); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| Event::Key(KeyEvent { | ||||||||||||||||||||||||||||||||||||||
| code: KeyCode::Char('x'), | ||||||||||||||||||||||||||||||||||||||
| kind: Press, | ||||||||||||||||||||||||||||||||||||||
| .. | ||||||||||||||||||||||||||||||||||||||
| }) => { | ||||||||||||||||||||||||||||||||||||||
| (KeyCode::Char('x'), _) => { | ||||||||||||||||||||||||||||||||||||||
| self.disconnect(); | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| _ => {} | ||||||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| Ok(()) | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||
| fn set_delete_confirmation(&mut self) { | ||||||||||||||||||||||||||||||||||||||
| if self | ||||||||||||||||||||||||||||||||||||||
| .wifi_list | ||||||||||||||||||||||||||||||||||||||
| .read() | ||||||||||||||||||||||||||||||||||||||
| .expect("Wifi list lock poisoned while deleting")[self.selected] | ||||||||||||||||||||||||||||||||||||||
| .is_saved | ||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||
| self.flags.show_delete_confirmation = true; | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+75
to
+84
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: rg -n 'self\.selected' --type rust -C 3Repository: santoshxshrestha/nmtui Length of output: 5479 🏁 Script executed: # Also check for list length checks or boundary validations
rg -n 'wifi_list.*len\|selected.*<\|selected.*>=\|selected.*bounds' --type rust -C 2Repository: santoshxshrestha/nmtui Length of output: 48 🏁 Script executed: # Look at the broader structure - find the struct definition and related methods
rg -n 'struct.*EventHandler\|selected.*:' --type rust -A 5 | head -100Repository: santoshxshrestha/nmtui Length of output: 48 🏁 Script executed: # Find where wifi_list is written/cleared/updated
rg -n 'wifi_list.*write\|wifi_list.*clear\|wifi_list.*=\|refresh.*wifi' --type rust -B 2 -A 3Repository: santoshxshrestha/nmtui Length of output: 48 🏁 Script executed: # Find set_delete_confirmation calls and delete-related flow
rg -n 'set_delete_confirmation\|delete_confirmation' --type rust -B 2 -A 2Repository: santoshxshrestha/nmtui Length of output: 48 🏁 Script executed: # Check for any list length validation before direct indexing
rg -n 'if.*selected.*<\|selected.*>=\|len.*>.*selected' --type rustRepository: santoshxshrestha/nmtui Length of output: 48 🏁 Script executed: # Look for refresh or update patterns more broadly
rg -n 'refresh\|update\|refresh_wifi' --type rust -B 1 -A 2Repository: santoshxshrestha/nmtui Length of output: 48 🏁 Script executed: # Read the event_handlers.rs file to understand context
cat -n src/apps/core/event_handlers.rs | head -120Repository: santoshxshrestha/nmtui Length of output: 3550 🏁 Script executed: # Search for event enum or what triggers delete confirmation
rg -n 'DeleteConfirm\|Delete.*Event\|Event::' --type rust -B 1 -A 1Repository: santoshxshrestha/nmtui Length of output: 48 🏁 Script executed: # Find scan_networks implementation
rg -n 'fn scan_networks' --type rust -A 20Repository: santoshxshrestha/nmtui Length of output: 1316 🏁 Script executed: # Look for where scan_networks updates the wifi_list
rg -n 'scan_networks\|wifi_list.write' --type rust -B 2 -A 3Repository: santoshxshrestha/nmtui Length of output: 48 🏁 Script executed: # Get the full scan_networks function
cat -n src/utils/scan.rs | head -80Repository: santoshxshrestha/nmtui Length of output: 2599 🏁 Script executed: # Look for where the wifi_list is actually written in scan_networks
rg -n 'wifi_list.write' --type rust -B 2 -A 5Repository: santoshxshrestha/nmtui Length of output: 944 Add bounds check to prevent panic if The vulnerable flow: user selects a network (e.g., index 5), rescans with Ctrl+R yielding fewer networks, then immediately presses 'd' without navigating. The Use Proposed fix fn set_delete_confirmation(&mut self) {
- if self
- .wifi_list
- .read()
- .expect("Wifi list lock poisoned while deleting")[self.selected]
- .is_saved
- {
- self.flags.show_delete_confirmation = true;
+ let list = self.wifi_list.read().expect("Wifi list lock poisoned while deleting");
+ if let Some(network) = list.get(self.selected) {
+ if network.is_saved {
+ self.flags.show_delete_confirmation = true;
+ }
}
}Similar unsafe indexing exists in 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo: "redundent" → "redundant".
🧰 Tools
🪛 LanguageTool
[grammar] ~21-~21: Ensure spelling is correct
Context: ...er than Arc wrapped type. - Removed the redundent key press kind. ### Changed - Changed...
(QB_NEW_EN_ORTHOGRAPHY_ERROR_IDS_1)
🤖 Prompt for AI Agents