Skip to content

tr: do not expand [c*n] repeats character by character - #14524

Merged
sylvestre merged 7 commits into
uutils:mainfrom
TanbirRamim:tr-repeat-count
Sep 30, 2026
Merged

sylvestre merged 7 commits into
uutils:mainfrom
TanbirRamim:tr-repeat-count

Conversation

@TanbirRamim

Copy link
Copy Markdown
Contributor

A [c*n] repeat was expanded into n bytes up front, so tr '[a*9223372036854775808]' b died with capacity overflow and tr '[a*99999999999999]' b with an allocation failure before reading any input. A large count in SET2 did not allocate, but spun in a count() over the expansion.

The sets are now resolved as runs of a repeated character. The lengths that the existing checks need come from the runs, and the two sets are lined up run by run: a run in SET1 maps its character to whatever it lines up with in SET2, and only the last mapping matters, so one pair per run boundary is enough. The vectors handed to the translate, delete and squeeze operations keep the same mappings in the same order and the same set membership, just without the repetition. I compared the old and new binaries on a few thousand combinations of sets and flags and got identical output for all of them.

Fixes #14420

A `[c*n]` repeat was expanded into `n` bytes up front, so a large count
aborted with a capacity overflow or an allocation failure before any
input was read, and a large count in SET2 spun in a `count()` over the
expansion.

Resolve the sets as runs of a repeated character instead. The lengths the
existing checks need come from the runs, and the two sets are lined up
run by run: a run in SET1 maps its character to whatever it lines up with
in SET2, and only the last mapping matters, so one pair per run boundary
is enough. The vectors handed to the translate, delete and squeeze
operations keep the same mappings in the same order and the same set
membership, just without the repetition.

Fixes uutils#14420
Copilot AI lite review requested due to automatic review settings September 12, 2026 20:55

Copilot AI left a comment

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.

🟡 Changes recommended

The implementation has a length-alignment issue, and one regression test is invalid on supported 32-bit targets.

Get a fresh assessment by requesting another Copilot review.

Pull request overview

Updates tr to handle large [c*n] repeats as compact runs instead of eagerly expanding them, with regression coverage.

Changes:

  • Implements run-based set resolution and mapping.
  • Preserves translation, deletion, and squeeze behavior.
  • Adds large-repeat regression tests.
File summaries
File Summary
src/uu/tr/src/operation.rs Implements run-based processing. Moderate issue: saturated lengths can misalign positions beyond usize::MAX.
tests/by-util/test_tr.rs Adds large-repeat tests. Moderate issue: a repeat-count test must be gated to 64-bit targets.
Review details

Suppressed comments (2)

src/uu/tr/src/operation.rs:362

  • Using usize::MAX as the prefix length does not mean “the whole set” once cumulative repeat lengths reach that value: remaining becomes zero and later runs are skipped. For example, tr -c '[a*18446744073709551614]bc' x incorrectly translates c, even though c is in SET1. Compute the complement over all runs separately from finite-prefix truncation.
            Self::complement_of_prefix(&set1, usize::MAX)

src/uu/tr/src/operation.rs:377

  • set1_len and set2_len have already been saturated before this subtraction, so [c*] padding is wrong when fixed lengths cross usize::MAX. With SET1 [a*18446744073709551615]b and SET2 [x*18446744073709551614][y*]z, the star should repeat once, but this computes zero and maps a to z instead of y; retain exact or wider lengths for the compensation calculation.
        let star_compensate_len = set1_len.saturating_sub(set2_len);
  • Files reviewed: 2/2 changed files
  • Comments generated: 2
  • Review effort level: Lite

💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/uu/tr/src/operation.rs Resolved
Comment thread tests/by-util/test_tr.rs Resolved
Review pointed out that summing the run lengths with saturating
arithmetic made positions past usize::MAX collide: the complement could
skip characters at the end of SET1, a [c*] star could pad too little, and
misaligned classes could pass the alignment check. Sum the lengths as
u128 instead, and stop using usize::MAX as a "whole set" sentinel.

The tests with repeat counts that only parse on 64-bit targets are now
gated to those targets.
Copilot AI review requested due to automatic review settings September 12, 2026 21:19

Copilot AI left a comment

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.

🔵 Needs a closer look

Preserve widened run lengths through pairing to avoid incorrect SET1/SET2 mappings.

Review details

Suppressed comments (1)

Previously missed (1) — in code that hasn't changed since the last review.

src/uu/tr/src/operation.rs:374

  • This conversion silently clamps a valid [c*] padding length when the SET1 expansion exceeds usize::MAX by more than usize::MAX. That shifts every following SET2 run (including character classes) earlier and can reject or apply an incorrect mapping; keep the run length widened (for example, as u128) through pairing instead of saturating it here.
  • Files reviewed: 2/2 changed files
  • Comments generated: 0 new
  • Review effort level: Lite

@github-actions

github-actions Bot commented Sep 12, 2026 •

Copy link
Copy Markdown

GNU testsuite comparison:

GNU test failed: tests/id/setgid. tests/id/setgid is passing on 'main'. Maybe you have to rebase?

The test binary is 64-bit but it drives a wasm32 coreutils, where these
counts do not fit in usize and are rejected as invalid, so the pointer
width guard alone does not exclude them.
Copilot AI review requested due to automatic review settings September 12, 2026 23:27

Copilot AI left a comment

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI review requested due to automatic review settings September 12, 2026 23:27

Copilot AI left a comment

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Copilot AI review requested due to automatic review settings September 19, 2026 18:26

Copilot AI left a comment

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Comment thread src/uu/tr/src/operation.rs Outdated
let set1_len: u128 = set1_runs.iter().map(|(_, n)| *n as u128).sum();

let set2_len = set2
let set2_len: u128 = set2

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.

expanded_len already returns 0 for CharStar, so this is just Self::expanded_len_of(&set2), no?

Comment thread src/uu/tr/src/operation.rs Outdated

let set2_solved: Vec<_> = set2.iter().flat_map(Self::flatten).collect();
let set2_runs: Vec<(u8, usize)> = Self::runs(&set2).collect();
let set2_len: u128 = set2_runs.iter().map(|(_, n)| *n as u128).sum();

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.

set2_len is computed a second time and shadowed here, could you please give it a different name or reuse a small helper for the runs sum? the same sum is also at line 365

Comment thread tests/by-util/test_tr.rs
.succeeds()
.stdout_only("xz");
new_ucmd!()
.args(&["[a*3]bc", "x[y*]z"])

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.

this case has nothing to do with squeeze, please move it into its own test

Copilot AI review requested due to automatic review settings September 25, 2026 03:08

Copilot AI left a comment

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@TanbirRamim

Copy link
Copy Markdown
Contributor Author

Done in the follow-up commit. The first set2_len is now just expanded_len_of(&set2) and renamed set2_fixed_len, since a star has no length until it becomes a repeat. Both run sums go through a small runs_len helper now, so nothing is shadowed. The [a*3]bc / x[y*]z case has its own test, test_repeat_in_set1_padded_by_star_in_set2.

@codspeed

codspeed Bot commented Sep 25, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 8 untouched benchmarks
⏩ 440 skipped benchmarks1


Comparing TanbirRamim:tr-repeat-count (ec226ce) with main (84d75fc)2

Open in CodSpeed

Footnotes

  1. 440 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. ↩

  2. No successful run was found on main (2f0bd06) during the generation of this report, so 84d75fc was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@TanbirRamim

Copy link
Copy Markdown
Contributor Author

The CodSpeed regressions are in factor (three_39_bit_primes) and du, which this PR doesn't touch, so it looks like the benchmark variance tracked in #14872 / #14960. I merged main to get a fresh run.

Copilot AI lite review requested due to automatic review settings September 29, 2026 20:55

Copilot AI left a comment

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.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@sylvestre
sylvestre merged commit cacdd8d into uutils:main Sep 30, 2026
102 of 103 checks passed
@sylvestre

Copy link
Copy Markdown
Contributor

Thanks for your PR

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.

tr: a large [c*n] repeat count is materialized unbounded — capacity overflow panic / allocation abort (exit 134)

3 participants