Skip to content

wc -m returns character count instead of byte count in C/POSIX locale #9712

Done

Description

@sylvestre

Component

wc -m

Description

GNU wc checks MB_CUR_MAX to determine whether to count bytes or multibyte characters. When MB_CUR_MAX == 1 (C/POSIX locale), it treats each byte as a character.

src/wc.c

static bool
wc (int fd, char const *file_x, struct fstatus *fstatus)
{
  // [...]
  /* If in the current locale, chars are equivalent to bytes, we prefer
     counting bytes, because that's easier.  */
!  if (MB_CUR_MAX > 1)
    {
      count_bytes = print_bytes;
      count_chars = print_chars;
    }
  else
    {
      count_bytes = print_bytes || print_chars;
      count_chars = false;
    }

However, uutils wc ignores locale and always counts UTF-8 characters using bytecount::num_chars().

src/uu/wc/src/count_fast.rs

pub(crate) fn count_bytes_chars_and_lines_fast<
// [...]
>(
    handle: &mut R,
) -> (WordCount, Option<io::Error>) {
    let mut total = WordCount::default();
    let buf: &mut [u8] = &mut AlignedBuffer::default().data;
    loop {
        match handle.read(buf) {
            Ok(0) => return (total, None),
            Ok(n) => {
                if COUNT_BYTES {
                    total.bytes += n;
                }
                if COUNT_CHARS {
!                    total.chars += bytecount::num_chars(&buf[..n]);
                }
                if COUNT_LINES {
                    total.lines += bytecount::count(&buf[..n], b'\n');
                }
            }
            Err(ref e) if e.kind() == ErrorKind::Interrupted => (),
            Err(e) => return (total, Some(e)),
        }
    }
}

Test / Reproduction Steps

$ echo -n "한글"|LC_ALL=C wc -m
6
$ echo -n "한글"|LC_ALL=C coreutils wc -m
2

Impact

wc -m produces different output than GNU in C/POSIX locale environments, breaking compatibility for scripts and CI pipelines that rely on locale-dependent character counting.

Recommendations

Check MB_CUR_MAX (or equivalent locale detection in rust) before counting characters. If MB_CUR_MAX == 1, return byte count instead of UTF-8 character count to match GNU behavior.

Activity

  1. added
    J - Encoding
    on Dec 19, 2025
  2. alexandrefresnais commented on Dec 21, 2025

    @alexandrefresnais
    Contributor

    After a quick look, I am not really sure how we can properly access MB_CUR_MAX from libc. Any idea ?

  3. sylvestre commented on Jan 17, 2026

    @sylvestre
    SponsorContributorAuthor
  4. added a commit that references this issue on May 25, 2026
    e0eab6d
  5. added a commit that references this issue on Oct 4, 2026
    6403f91
  6. added 5 commits that reference this issue on Oct 21, 2026
    fe07376
    d4fce06
    8cab248
    a40113f
    49cd7ea
  7. naoNao89 commented on Nov 20, 2026

    @naoNao89
    Contributor

    this appears to be fixed by #16087

    I reran the original reproducer against GNU wc 9.4 and current uutils main:

    printf '한글' | LC_ALL=C wc -m

    Both now return 6 (uutils previously returned 2).

    also compared main against #11006 across the tested locale/counting combinations and found no behavioral diff

    the ref to this issue in #15254 predates the merge of #16087. Other wc divergences remain, but the specific C/POSIX wc -m issue reported here is resolved

    ithink this can be closed as fixed #16087

  8. xtqqczze commented on Nov 25, 2026

    @xtqqczze
    Collaborator

    Fixed in v0.13.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    J - EncodingU - wcgnu testsuitereported-canonical

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions