stty: fix: reject "+hex" in parse_saved_state - #9662
Conversation
|
GNU testsuite comparison: |
|
@Rudxain still draft after 2 weeks, are you going to work on it? thanks |
Yes! This PR can be merged as-is, but I want to replace coreutils/src/uucore/src/lib/features/parser/num_parser.rs Lines 39 to 54 in e6467b1 Should I use that? Or is there something more appropriate? |
641efd8 to
4e269cf
Compare
This comment was marked as outdated.
This comment was marked as outdated.
|
Restarted |
This comment was marked as resolved.
This comment was marked as resolved.
|
BTW, this branch is soft-blocked on this PR. Should I include that patch into this PR, or should I keep it separate for better scoping? |
|
GNU testsuite comparison: |
|
Could you please add a test to make sure we don't regress in the future? Thanks |
|
GNU testsuite comparison: |
|
GNU testsuite comparison: |
7d2542f to
4c17ee2
Compare
This comment was marked as outdated.
This comment was marked as outdated.
There was a problem hiding this comment.
Pull request overview
This PR hardens stty’s saved-state parser to reject hex fields with a leading +, matching expected stty behavior and avoiding from_str_radix accepting inputs like +00.
Changes:
- Add an explicit rejection of
+hexfields inparse_saved_state. - Add tests intended to cover valid saved-state parsing and the new
+rejection case.
Suppressed comments (1)
src/uu/stty/src/stty.rs:1449
- This test input also has only 4 parts, so it would return
Noneeven without the new+rejection. Build an input with the correct4 + NCCSpart count so the test specifically validates the new+hexbehavior.
#[test]
fn test_parse_saved_state_no_plus() {
let result = parse_saved_state("+00:+01:+ff:+7f");
assert_eq!(&result, None);
}
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| if part.is_empty() { | ||
| return None; // GNU rejects empty hex values | ||
| } | ||
| // TO-DO: avoid `from_str_radix` |
| if part.as_bytes()[0] == b'+' { | ||
| return None; | ||
| } |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.
Suppressed comments (2)
src/uu/stty/src/stty.rs:1443
- The new
test_sane_parse_saved_stateuses an input with only 4 colon-separated parts, butparse_saved_staterequires exactly4 + nix::libc::NCCSparts, so this test will always fail. The assertion also comparesOption<Vec<u32>>toSome(&[...]), which is a type mismatch.
fn test_sane_parse_saved_state() {
let result = parse_saved_state("00:01:ff:7f");
assert_eq!(&result, Some(&[0, 1, 255, 127]));
}
src/uu/stty/src/stty.rs:543
- Indexing
part.as_bytes()[0]is less readable than a string predicate and relies on the preceding emptiness check to avoid panics. Usingstarts_with('+')is clearer and keeps the same behavior; also consider using the conventionalTODOspelling in the comment.
// TO-DO: avoid `from_str_radix`
if part.as_bytes()[0] == b'+' {
return None;
}
0a83e75 to
c662e77
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 1 out of 1 changed files in this pull request and generated 1 comment.
Suppressed comments (1)
src/uu/stty/src/stty.rs:543
- Using
part.as_bytes()[0]is less idiomatic and ties the check to byte indexing.starts_with('+')is clearer and avoids manual indexing while keeping the same behavior (after the empty-string guard).
// TO-DO: avoid `from_str_radix`
if part.as_bytes()[0] == b'+' {
return None;
}
c662e77 to
6651e6b
Compare
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 1 out of 1 changed files in this pull request and generated no new comments.
Suppressed comments (1)
src/uu/stty/src/stty.rs:542
- The explicit rejection of leading '+' is good, but this block is a bit brittle/unclear:
part.as_bytes()[0]is less readable thanstarts_with('+')and can become a panic hazard if the emptiness guard changes.- The comment about
from_str_radixbeing undocumented here and theTO-DOare confusing once we’re already validating the input.
Consider simplifying to an explicit GNU-compatibility check usingstarts_withand dropping the TODO.
// TO-DO: avoid `from_str_radix`
if part.as_bytes()[0] == b'+' {
return None;
Fixes this pitfall: rust-lang/rust-clippy#16213
Related: #9255