Skip to content

Discrepancy in output length with special characters #5831

Open

Description

@sylvestre

With

echo "mÃ" > /tmp/a

With our implementation:

$ cat /tmp/a| ./target/debug/coreutils wc  -L -l
      1      2

With GNU:

$ cat /tmp/a| /usr/bin/wc -L -l  
      1      1

Found with the wc differential fuzzer

Activity

  1. cakebaker commented on Jan 13, 2024

    @cakebaker
    Contributor

    I can't reproduce this issue locally. GNU wc returns the same result as uutils wc:

    $ echo "mÃ" > /tmp/a
    $ cat /tmp/a | /usr/bin/wc -L -l
          1       2
    
  2. changed the title [-]Discrepancy in output length with special characters"[/-] [+]Discrepancy in output length with special characters[/+] on Jan 13, 2024
  3. sylvestre commented on Jan 13, 2024

    @sylvestre
    SponsorContributorAuthor

    try with a different local ?
    like with LANG=C ?

  4. cakebaker commented on Jan 13, 2024

    @cakebaker
    Contributor

    I tried LC_ALL=C, LANG=C and LC_COLLATE=C, but no difference. And based on the doc of -L I would expect it to return 2 (and not 1) because there are two characters.

  5. BenWiederhake commented on Mar 2, 2024

    @BenWiederhake
    Collaborator

    @sylvestre Do you still have the file somewhere? I suspect that this is just an encoding issue, because I can't reproduce it either. For reference, here's my /tmp/a:

    $ hexdump -C /tmp/a
    00000000  6d c3 83 0a                                       |m...|
    00000004
  6. RenjiSann commented on Feb 8, 2025

    @RenjiSann
    Collaborator

    I can reproduce:

    $ echo "mÄ" | ../gnu/src/wc -L -l      
          1       1
    $ echo "mÄ" | ./target/debug/wc -L -l 
          1       2
    

    Also, a similar error is happening with -m (number of characters).

    $ echo "mÄ" | ./target/debug/wc -m -c
          3       4
    $ echo "mÄ" | ../gnu/src/wc -m -c      
          4       4
    
    # Without the `\n` added by echo
    $ echo -n "mÄ" | ./target/debug/wc -m -c
          2       3
    $ echo -n "mÄ" | ../gnu/src/wc -m -c 
          3       3
    
  7. rwdj commented on Feb 16, 2025

    @rwdj
    Contributor

    Confirming an issue with both -L and -m:

    $ echo "mÄ" > /tmp/f
    $ hexdump -C /tmp/f
    00000000  6d c3 84 0a                                       |m...|
    00000004
    $ LC_ALL=C cargo run -q -- wc -mL /tmp/f
    3 2 /tmp/f
    $ LC_ALL=C /usr/bin/wc -mL /tmp/f
    4 1 /tmp/f
    

    Although still a bug, it does appear encoding-related:

    $ LC_ALL=en_US.UTF-8 cargo run -q -- wc -mL /tmp/f
    3 2 /tmp/f
    $ LC_ALL=en_US.UTF-8 /usr/bin/wc -mL /tmp/f
    3 2 /tmp/f
    
  8. rwdj commented on Feb 16, 2025

    @rwdj
    Contributor

    To be more specific, it's a matter of if bytes and multibyte chars are the same which depends on locale.

    $ LC_ALL=en_US.UTF-8 cargo run -q -- wc -cm /tmp/f
    3 4 /tmp/f
    $ LC_ALL=C cargo run -q -- wc -cm /tmp/f
    3 4 /tmp/f
    $ LC_ALL=en_US.UTF-8 wc -cm /tmp/f
    3 4 /tmp/f
    $ LC_ALL=C wc -cm /tmp/f
    4 4 /tmp/f
    

    In libc, this is detected by checking the length of a multibyte char is equal to a byte (locale-dependent): MB_CUR_MAX.
    It appears uutils's wc uses utf8::BufReadDecoder thereby assuming always UTF-8.

  9. added
    J - Encoding
    on Jul 4, 2025
  10. added a commit that references this issue on May 25, 2026
    e0eab6d
  11. added a commit that references this issue on Oct 4, 2026
    6403f91
  12. added 5 commits that reference this issue on Oct 21, 2026
    fe07376
    d4fce06
    8cab248
    a40113f
    49cd7ea
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 - wc

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions