Skip to content

wc: saturate overflowing total counts - #15475

Open
limadog9 wants to merge 1 commit into
uutils:mainfrom
limadog9:fix/wc-total-overflow-11209
Open

limadog9 wants to merge 1 commit into
uutils:mainfrom
limadog9:fix/wc-total-overflow-11209

Conversation

@limadog9

Copy link
Copy Markdown
Contributor

Use 64-bit counters and checked accumulation for each total, reporting an error when a displayed count overflows. Saturate the metadata size sum used for column widths and cover total output modes with a sparse memfd regression test.

Closes #11209

@oech3

oech3 commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

Can we use regular sparse file instead of memfd, and support more targets?

@limadog9
limadog9 force-pushed the fix/wc-total-overflow-11209 branch from f1c058d to c2ec6b2 Compare October 18, 2026 14:54
@limadog9

Copy link
Copy Markdown
Contributor Author

Replaced memfd with a regular sparse file and enabled the test on Unix targets. It skips if the filesystem can’t support the required file size.

@limadog9
limadog9 force-pushed the fix/wc-total-overflow-11209 branch from c2ec6b2 to 468f221 Compare October 18, 2026 14:58
Comment thread src/uu/wc/locales/en-US.ftl Outdated
Comment on lines +26 to +29
wc-error-total-bytes-overflow = total bytes: Value too large for defined data type
wc-error-total-chars-overflow = total characters: Value too large for defined data type
wc-error-total-lines-overflow = total lines: Value too large for defined data type
wc-error-total-words-overflow = total words: Value too large for defined data type

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.

Would you dedup similar part? By the way, we can choice different error message with GNU if we don't change it's meaning. So we could simply extract error message from std.

@limadog9
limadog9 force-pushed the fix/wc-total-overflow-11209 branch from 468f221 to df1a0a2 Compare October 18, 2026 15:23
@limadog9

Copy link
Copy Markdown
Contributor Author

Extracted the repeated overflow wording into a shared Fluent message used by all four diagnostics.

Comment thread src/uu/wc/src/wc.rs Outdated
}

if settings.total_when.is_total_row_visible(num_inputs) {
for (overflowed, (enabled, message)) in total_overflow.into_iter().zip([

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.

could you please print these in the same order as the columns (lines, words, chars, bytes)? that's also the order GNU uses when several overflow

Comment thread src/uu/wc/src/word_count.rs Outdated
impl WordCount {
/// Accumulate counts, saturating each independently. Return overflow flags
/// for the byte, character, line, and word counts, in that order.
pub fn accumulate(&mut self, other: Self) -> [bool; 4] {

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.

returning a [bool; 4] that callers have to zip by position is easy to get wrong.
could the overflow flags be named fields instead (a small struct, or kept in WordCount)?

Use 64-bit counters and checked accumulation for each total, reporting
an error when a displayed count overflows. Saturate the metadata size
sum used for column widths and cover total output modes with a sparse
file regression test.

Closes uutils#11209
@limadog9
limadog9 force-pushed the fix/wc-total-overflow-11209 branch from df1a0a2 to da50ac4 Compare October 18, 2026 22:37

This branch has not been deployed

No deployments
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.

wc: Arithmetic overflow on large files

3 participants