Skip to content

Conversation

jeckersb
Copy link
Collaborator

@jeckersb jeckersb commented Sep 5, 2025

This moves the bulk of the parsing logic into Parameter. Its
parse method returns a 2-tuple, its items being:

  1. An Option<Parameter> which contains the parsed Parameter.
    This will be None if the provided input is empty or contains only
    whitespace.

  2. A byte slice of the rest of the input which was not consumed by
    the yielded Parameter.

This also adds new tests for some cases that would fail under the
previous parser implementation.

Signed-off-by: John Eckersberg jeckersb@redhat.com

@bootc-bot bootc-bot bot requested a review from cgwalters September 5, 2025 14:33
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request is a great improvement, transitioning from infallible From traits to fallible TryFrom for more robust kernel command line parameter parsing. The added validation for keys and improved whitespace handling are valuable additions. I've found a critical issue where invalid input can cause a panic, a high-severity bug in ParameterStr parsing, and a minor opportunity for code simplification. Overall, these changes significantly enhance the reliability of the parsing logic.

Copy link
Collaborator

@cgwalters cgwalters left a comment

Choose a reason for hiding this comment

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

Looks OK to me; I think it's notable that this doesn't change any external users though so it means we're not fixing any bugs. That's fine of course, just an internal cleanup.

@jeckersb
Copy link
Collaborator Author

jeckersb commented Sep 5, 2025

Looks OK to me; I think it's notable that this doesn't change any external users though so it means we're not fixing any bugs. That's fine of course, just an internal cleanup.

Primary motivation for this is that I wanted to use Parameter in composefs-boot, and I realized you could feed it anything which made me sad

/// Any leading or trailing ascii whitespace is trimmed.
///
/// Returns an error if the key contains internal whitespace.
fn try_from(value: &'a [u8]) -> Result<Self, Self::Error> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think it might improve ergonomics to define this in terms of a function that returns Option, and have this one just use .ok_or_else.

The reason here is that the one that returns Option can be const and so since Rust 1.83 it should be possible to do e.g.:

let parameter = const { Parameter::from_bytes("foo").unwrap() }

So for constant strings like this if it's invalid we fail at compile time, not runtime.

@jeckersb
Copy link
Collaborator Author

jeckersb commented Sep 5, 2025

Bah it occurs to me that most of my premise here is just wrong. Going back to #1425 (comment), you can in fact quote the key and have internal whitespace. Dumb, but valid.

@jeckersb
Copy link
Collaborator Author

jeckersb commented Sep 5, 2025

I think maybe the better way to approach this is to move the parameter splitting out of Cmdline and into Parameter/ParameterStr themselves. Then initializing a new Parameter from a slice of bytes (or &str or whatnot) will consume as much of the slice as necessary to emit itself, and also hand back everything else that it didn't consume. I think everything except empty/whitespace should parse, so you'd end up with something like this under impl Parameter:

fn parse(input: &'a [u8]) -> (Option<Self>, &'a [u8])

Then Cmdline can just emit items while parsing parameters is Some.

This would also make the semantics in external use case of Parameter in places like composefs-boot more obvious.

@jeckersb jeckersb marked this pull request as draft September 5, 2025 16:53
@jeckersb
Copy link
Collaborator Author

jeckersb commented Sep 5, 2025

Another thing is probably to not implement From and/or TryFrom for ParameterKey/ParameterKeyStr so they can't be constructed out of context from their parent Parameter/ParameterStr respectively.

@cgwalters
Copy link
Collaborator

Primary motivation for this is that I wanted to use Parameter in composefs-boot, and I realized you could feed it anything which made me sad

Isn't there a more fundamental problem in that composefs-boot still has its own karg parsing distinct from this crate, so we either need to lower this to that (or I'm still tempted to just move composefs-boot over here)

@jeckersb
Copy link
Collaborator Author

jeckersb commented Sep 5, 2025

Primary motivation for this is that I wanted to use Parameter in composefs-boot, and I realized you could feed it anything which made me sad

Isn't there a more fundamental problem in that composefs-boot still has its own karg parsing distinct from this crate, so we either need to lower this to that (or I'm still tempted to just move composefs-boot over here)

Yeah I was/am in the process of removing all of the kargs parsing bits over there and having it use kernel_cmdline from here instead where needed.

@jeckersb jeckersb force-pushed the cmdline-improvements branch from 39ee611 to 8c63497 Compare September 5, 2025 22:52
@jeckersb jeckersb changed the title kernel_cmdline: Improve parameter parsing with validation and whitespace handling kernel_cmdline: Refactor parsing to improve quote and whitespace handling Sep 5, 2025
@jeckersb
Copy link
Collaborator Author

jeckersb commented Sep 5, 2025

Ok pretty much completely redid this, I think this is better but I gotta stop messing with it for now.

/gemini review

@jeckersb jeckersb marked this pull request as ready for review September 5, 2025 22:56
@bootc-bot bootc-bot bot requested a review from cgwalters September 5, 2025 22:56
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request is a solid refactoring of the kernel command line parsing logic. Moving the parsing into Parameter::parse and introducing CmdlineIter significantly improves the code's clarity, maintainability, and correctness, especially around whitespace and quote handling. The new tests are a great addition and cover important edge cases. I found one minor issue in a test case, which I've detailed below.

…ling

This moves the bulk of the parsing logic into `Parameter`.  Its
`parse` method returns a 2-tuple, its items being:

1.  An `Option<Parameter>` which contains the parsed `Parameter`.
This will be `None` if the provided input is empty or contains only
whitespace.

2.  A byte slice of the rest of the input which was not consumed by
the yielded `Parameter`.

This also adds new tests for some cases that would fail under the
previous parser implementation.

Signed-off-by: John Eckersberg <jeckersb@redhat.com>
@jeckersb jeckersb force-pushed the cmdline-improvements branch from 8c63497 to 4335ba1 Compare September 5, 2025 22:59
@cgwalters cgwalters merged commit e219a1b into bootc-dev:main Sep 9, 2025
33 of 34 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.

2 participants