Skip to content
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

allow_invalid_utf8(true) required only in debug build #3344

Closed
2 tasks done
njaard opened this issue Jan 25, 2022 · 2 comments
Closed
2 tasks done

allow_invalid_utf8(true) required only in debug build #3344

njaard opened this issue Jan 25, 2022 · 2 comments
Labels
A-validators Area: ArgMatches validation logi C-bug Category: Updating dependencies S-wont-fix Status: Closed as there is no plan to fix this

Comments

@njaard
Copy link

njaard commented Jan 25, 2022

Please complete the following tasks

  • I have searched the discussions
  • I have searched the existing issues

Rust Version

1.58.1

Clap Version

3.0.12

Minimal reproducible code

	use clap::Arg;
	let matches = clap::App::new("app")
		.arg(Arg::new("f")
			.takes_value(true)
		)
		.get_matches();
	matches.value_of_os("f");

This program fails like so:

thread 'main' panicked at 'Must use `Arg::allow_invalid_utf8` with `_os` lookups at `f`', src/main.rs:12:13

But only in a debug build.

Steps to reproduce the bug with the above code

Succeeds: cargo run --release -- blah
Fails: cargo run -- blah

Actual Behaviour

Is allow_invalid_utf8(true) actually required? i don't know

Expected Behaviour

It should just work.

Additional Context

Why is this option even required? It's a giant footgun.

If you really want to do that check, maybe change takes_value to takes_string and takes_path. But as it is, it serves no purpose.

Debug Output

No response

@njaard njaard added the C-bug Category: Updating dependencies label Jan 25, 2022
@epage
Copy link
Member

epage commented Jan 25, 2022

Why is this option even required? It's a giant footgun.

Clap was more of a footgun before this existed

In v2, if you did value_of and the user passed an invalid utf-8 string, clap would panic. Your options were one of

  • Pass AppSettings::StrictUtf8 which prevented invalid utf-8 when it was intended
  • Always use value_of_os and do the conversion yourself
  • Write your own validators and make sure your accesses matched your definitions with those validators

Now, StrictUtf8 is the default and you can opt-out with allow_invalid_utf8 on a per-argument basis.

The asserts serve a couple of purposes

  • Help users know that value_of_os should be paired with allow_invalid_utf8 to be effective (if you have strict utf-8, value_of_os offers you nothing that value_of gives you, if you use allow invalid utf-8, value_of will panic on invalid characters
    • This eases the transition from clap2, eases general on-boarding, and lower maintenance effort (can make changes more confidently)
  • Prepare the way for Provide data in the application domain rather than the CLI domain #2683 which will be the ultimately solution for this

99% of clap's error reporting is done through debug asserts

  • Clap assumes these are development errors, that there isn't a reason to recover from them, and that the majority of testing happens in debug mode (check out App::debug_assert though it doesn't help in this case, and trycmd)
  • We limit it to debug for faster smaller binary sizes and faster runtime performance

@epage epage added A-validators Area: ArgMatches validation logi S-triage Status: New; needs maintainer attention. labels Jan 25, 2022
@epage
Copy link
Member

epage commented Feb 2, 2022

As this is by design and there aren't any more comments, I'm closing this out. If there are any concerns, feel free to raise them

@epage epage closed this as completed Feb 2, 2022
@epage epage added S-wont-fix Status: Closed as there is no plan to fix this and removed S-triage Status: New; needs maintainer attention. labels Feb 2, 2022
tkmcmaster added a commit to FamilySearch/pewpew that referenced this issue Sep 8, 2022
- new versions of Clap panic with invalid utf8 passed to value_of_os()
  - clap-rs/clap#3344
tkmcmaster added a commit to FamilySearch/pewpew that referenced this issue Sep 9, 2022
* Fixed an issue with out directories and stats file params

- new versions of Clap panic with invalid utf8 passed to value_of_os()
  - clap-rs/clap#3344

* Added comments to changes

* Added tests for the command line parser

- Split the parsing code into separate functions that can be tested
- Moved the logger init out of the match functions so we can test the cli parsing
- Added tests around the path checks. Found we weren't parsing the try -d the same and fixed it

* Added additional tests for the cli parsing

* Cleaned up the cli tests

* Updated the cargo deny.toml
- Unicode license is allowed under our current whitelist
- Ignoring the time advisory since chrono should not be impacted. chronotope/chrono#602
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-validators Area: ArgMatches validation logi C-bug Category: Updating dependencies S-wont-fix Status: Closed as there is no plan to fix this
Projects
None yet
Development

No branches or pull requests

2 participants