Skip to content

stat: reject a % directive with flags or a precision - #15352

Closed
ozenyunus35 wants to merge 6 commits into
uutils:mainfrom
ozenyunus35:stat-invalid-percent-directive
Closed

ozenyunus35 wants to merge 6 commits into
uutils:mainfrom
ozenyunus35:stat-invalid-percent-directive

Conversation

@ozenyunus35

@ozenyunus35 ozenyunus35 commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Closes #15348

A % reached after flags, a width or a precision (%0%, %-%, %#%, %++-+%, %.3%) printed ?. Only a bare %% is the escape; GNU rejects the others as an invalid directive after printing the text before it, e.g. stat: '%0%': invalid directive. handle_percent_case already did this for a width (%9%); it now does it for flags and a precision too.

Test: test_percent_after_flags_is_an_invalid_directive (the formats from the issue, plus %% still printing %), which fails without the change.

I used an AI assistant to help draft parts of the code and tests; I reviewed the changes and ran the tests myself.

`%0%`, `%-%` or `%.3%` printed `?`. Only a bare `%%` is the escape;
GNU rejects the others as an invalid directive, after printing the text
before it.

Part of uutils#15348
GNU prints both through out_uint, which drops the `+` and ` ` flags, so
`%+s` is `7`, not `+7`.

Closes uutils#15348
Comment thread src/uu/stat/src/stat.rs

// A `%` after flags, a width or a precision (`%0%`, `%-%`, `%.3%`) is
// not the `%%` escape; GNU rejects it as an invalid directive.
if chars[j] == '%' {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

the width branch above already does the same chars[j] == '%' check, could be dedup, no?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes, the width branch now only handles the end of the format and leaves the % case to the check below.

Comment thread tests/by-util/test_stat.rs Outdated
}

#[test]
fn test_size_ignores_sign_flags() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

please add a test for %+S with -f too

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I've dropped the %s / %S part from this PR, so it's back to the % after flags fix only.

@collinfunk

Copy link
Copy Markdown

GNU prints both through out_uint

I suspect you are just pointing your LLM at GNU's source code and telling it to copy what it finds, which is frustrating.

Comment thread src/uu/stat/src/stat.rs Outdated

// Reject directives like `%<NUMBER>` by checking if width has been parsed.
if j >= bound || chars[j] == '%' {
if j >= bound {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

now that the % check moved below, this block does the same as the check_bound on the next line, could be removed, no?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Right, removed it: check_bound on the next line reports the same directive when the width ends the format.

@codspeed

codspeed Bot commented Oct 15, 2026

Copy link
Copy Markdown

Merging this PR will regress 1 benchmark

⚠️ 125 benchmarks spent significant time in system calls

System calls cannot be consistently instrumented, so they are not included in the measure, which understates the real cost. Please switch to the Walltime instrument to accurately measure system calls.

Measurement and system calls

ℹ️ 1 benchmark has no flame graph

The flamegraph could not be generated because too much time was spent in runtime internals, making the visualization not useful for performance analysis.

⚡ 1 improved benchmark
❌ 1 regressed benchmark
✅ 399 untouched benchmarks
⏩ 70 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
❌ Simulation factor_five_38_bit_primes 1.8 s 2 s -10.73%
⚡ Simulation factor_three_39_bit_primes 800.7 ms 511 ms +56.69%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing ozenyunus35:stat-invalid-percent-directive (89cdef7) with main (2fb77e8)

Open in CodSpeed

Footnotes

  1. 70 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@ozenyunus35

Copy link
Copy Markdown
Contributor Author

Closing this, as I can't follow up on it further. Thanks for the reviews.

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.

bug(stat): Parser Edge Cases (--format option) (Fuzzing Part 1)

3 participants