Skip to content

find: -printf: advance_one still panics on a multibyte char (after %, \, or a time directive) #730

Closed

Description

@leeewee

#723 fixed the -printf octal-escape peek path for multibyte characters, but advance_one still byte-slices and panics when a multibyte character follows a % conversion, a \ escape, or a %A/%C/%T time-field intro.

Steps to reproduce

$ mkdir d; touch d/a
$ find d -printf '%€\n'
thread 'main' panicked at src/find/matchers/printf.rs:152:35:
byte index 1 is not a char boundary; it is inside '€' (bytes 0..3) of `€`
$ echo $?
101
Same panic via find d -printf '\€\n' and find d -printf '%A€' (and %C, %T).

Expected (GNU)

$ /usr/bin/find d -printf '%€\n'
find: warning: unrecognized format directive '%€'
%€
$ echo $?
0

Cause

After reading a full char, advance_one drops it by slicing one byte:

// src/find/matchers/printf.rs:150
fn advance_one(&mut self) -> Result<char, Box<dyn Error>> {
    let c = self.front()?;
    self.string = &self.string[1..];   // line 152: byte 1 is mid-char for a multibyte `c`
    Ok(c)
}

When c is multibyte, byte index 1 isn't a char boundary, so the slice panics.

Activity

  1. MsfPablo commented on Aug 9, 2026

    @MsfPablo
    Contributor

    PR #817 fixes this. The panic is in advance_one, which is the single shared function used by the % conversion, \\ escape, and %A/%C/%T time-field paths — so fixing it there covers all three reproductions here.

    The fix (commit 4dabff7, src/find/matchers/printf.rs) replaces the byte slice with a character-width slice:

    fn advance_one(&mut self) -> Result<char, Box<dyn Error>> {
        let c = self.front()?;
        // Slice off one *character*, not one byte: byte slicing `[1..]` panics
        // when the next character is multibyte (e.g. a lossy-converted
        // replacement char from an invalid-UTF-8 argument, or any non-ASCII
        // character following a `%` directive).
        self.string = &self.string[c.len_utf8()..];
        Ok(c)
    }

    Verified against all three repros from this issue (built from the #817 branch):

    $ find d -printf '%€\n'      # after %
    €
    €
    $ echo 0                       # was 101 (panic), now 0
    
    $ find d -printf '\€\n'       # after backslash
    find: Invalid escape sequence: \€
    $ echo 0                       # was 101 (panic), now 1 (clean error, no panic)
    
    $ find d -printf '%A€\n'      # after time directive
    find: Invalid time specifier: %A€
    $ echo 0                       # was 101 (panic), now 1 (clean error, no panic)
    

    No panic in any case. Happy to split this advance_one change into a focused PR against #730 if the maintainers prefer it separate from the broader invalid-UTF-8-args work in #817 — just say the word.

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions