+
Skip to content

Conversation

ic3man5
Copy link
Contributor

@ic3man5 ic3man5 commented Jan 21, 2025

Fixes issue #7185 "sort: errors on overflowing -k argument but shouldn't"

$ printf "2\n1\n" | cargo run sort -k 18446744073709551616
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.10s
     Running `target/debug/coreutils sort -k 18446744073709551616`
1
2

I'm making the assumption based on the issue that we set to usize::MAX.

As this is my first ever commit to a project and new to rust, please let me know what else would have to be done if needed to get this approved.

@ic3man5 ic3man5 changed the title Fixes issue #7185 sort: errors on overflowing -k argument but shouldn't Jan 21, 2025
@ic3man5
Copy link
Contributor Author

ic3man5 commented Jan 21, 2025

$ cargo test sort
    Finished `test` profile [unoptimized + debuginfo] target(s) in 0.12s
     Running unittests src/bin/coreutils.rs (target/debug/deps/coreutils-7c4b7f12d70d4d85)

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

     Running tests/test_util_name.rs (target/debug/deps/test_util_name-76974a15f1516098)

running 0 tests

test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 78 filtered out; finished in 0.00s

     Running tests/tests.rs (target/debug/deps/tests-72f7e07ac2e9e19f)

running 131 tests
test test_comm::test_sorted ... ok
...
test test_sort::test_tmp_files_deleted_on_sigint ... ok

test result: ok. 132 passed; 0 failed; 0 ignored; 0 measured; 3162 filtered out; finished in 0.90s

Copy link

GNU testsuite comparison:

GNU test failed: tests/misc/stdbuf. tests/misc/stdbuf is passing on 'main'. Maybe you have to rebase?
Skip an intermittent issue tests/timeout/timeout (fails in this run but passes in the 'main' branch)
Congrats! The gnu test tests/misc/usage_vs_getopt is no longer failing!

Copy link
Contributor

@cakebaker cakebaker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Can you please run cargo fmt? Thanks!

Comment on lines 700 to 710
let field = match field.parse::<usize>() {
Ok(field) => field,
Err(e) => {
match e.kind() {
IntErrorKind::PosOverflow => usize::MAX,
_ => return Err(format!("failed to parse field index {}", field.quote())),
}
},
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

more rust idiomatic:

Suggested change
let field = match field.parse::<usize>() {
Ok(field) => field,
Err(e) => {
match e.kind() {
IntErrorKind::PosOverflow => usize::MAX,
_ => return Err(format!("failed to parse field index {}", field.quote())),
}
},
};
let field = field.parse::<usize>()
.unwrap_or_else(|e| match e.kind() {
IntErrorKind::PosOverflow => usize::MAX,
_ => panic!("failed to parse field index {}", field.quote()),
});

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this better but is there a way to get away from panic! here? Assuming it would be better to unwind the error over panicing?

The contrib file also mentions we shouldn't panic.
https://github.com/uutils/coreutils/blob/main/CONTRIBUTING.md#dont-panic

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May I suggest this:

        let field = match field.parse::<usize>() {
            Ok(f) => f,
            Err(e) if *e.kind() == IntErrorKind::PosOverflow => usize::MAX,
            Err(_) => return Err(format!("failed to parse field index {}", field.quote()))
        };

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yeah, my bad. Sorry, no crash indeed

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let field = match field.parse::<usize>() {
    Ok(f) => f,
    Err(e) if *e.kind() == IntErrorKind::PosOverflow => usize::MAX,
    Err(e) => {
        return Err(format!(
            "failed to parse field index {} {}",
            field.quote(),
            e
        ))
    }
};

I've refactored to this. I believe this PR should be good to go now.

@ic3man5
Copy link
Contributor Author

ic3man5 commented Jan 21, 2025

Looks good. Can you please run cargo fmt? Thanks!

done

Copy link

GNU testsuite comparison:

GNU test failed: tests/misc/usage_vs_getopt. tests/misc/usage_vs_getopt is passing on 'main'. Maybe you have to rebase?
Skip an intermittent issue tests/tail/inotify-dir-recreate (fails in this run but passes in the 'main' branch)
Congrats! The gnu test tests/misc/stdbuf is no longer failing!

@RenjiSann
Copy link
Collaborator

RenjiSann commented Jan 21, 2025

Before merging, please squash your commits in a single one, whose message follows this syntax

<util>: <what you actually did>

In your example that could be

sort: set -k arg to usize::MAX on overflow 

As the commits gets merged as is in main, we better avoid having commits that don't bring anything and that just get reversed in the next one.

Thank you for your contribution ! 🫶

EDIT : 2 commits actually, one being for the test with a commit message like

test(sort): add test for overflowing -k argument

@ic3man5
Copy link
Contributor Author

ic3man5 commented Jan 22, 2025

Before merging, please squash your commits in a single one, whose message follows this syntax

<util>: <what you actually did>

In your example that could be

sort: set -k arg to usize::MAX on overflow 

As the commits gets merged as is in main, we better avoid having commits that don't bring anything and that just get reversed in the next one.

Thank you for your contribution ! 🫶

EDIT : 2 commits actually, one being for the test with a commit message like

test(sort): add test for overflowing -k argument

rebased down to two commits, never knew git rebase interactive could do this! Should be good to go now.

@RenjiSann
Copy link
Collaborator

Beware of the new line in test_sort.rs that is added in the non-test commit.
Other than that, looks good to me 👍

newline, format, and more rust idiomatic code.

refactor to remove panic!
@ic3man5
Copy link
Contributor Author

ic3man5 commented Jan 22, 2025

Beware of the new line in test_sort.rs that is added in the non-test commit. Other than that, looks good to me 👍

fixed, sorry about that.

@sylvestre sylvestre merged commit f731f2c into uutils:main Jan 22, 2025
64 of 65 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载