[Toybox] Bug: "grep ^." matches only literal '.'

Rob Landley rob at landley.net
Mon Jul 27 07:45:14 UTC 2026


On 7/19/26 19:29, Jörgen Cederlöf via Toybox wrote:
> This was unexpected. If the first one matches, the second one should match too:
> 
>    kirkwood:/etc $ ls -l |grep '^-......r' |wc -l
>    30
>    kirkwood:/etc $ ls -l |grep '^.......r' |wc -l
>    0
>    kirkwood:/etc $ grep --version
>    toybox 0.8.9-android
> 
> It is reproducible on current master (0bb70e68):
> 
>    $ echo foo |./toybox grep ^f || echo FAIL
>    foo
>    $ echo foo |./toybox grep ^. || echo FAIL
>    FAIL
>    $ echo .oo |./toybox grep ^. || echo FAIL
>    .oo
>    $

Hmmm...

   $ echo hello | ./grep '^.'
   $

Yeah, that's borked.

> It matches correctly if the regexp is more complex so the fast path
> does not kick in:
> 
>    $ echo foo |./toybox grep '^.\{1\}' || echo FAIL
>    foo
>    $

Ah. Here's the problem:

         ii = FLAG(i) ? toupper(*ss) : *ss;
         for (seek = TT.fixed[ii]; seek; seek = seek->next) {
           if (*(pp = seek->arg)=='^' && !FLAG(F)) {

The fixed match table starts with a bucket sort on the first character, 
so if the first character isn't in the pattern we're looking for it gets 
discarded before checking for wildcards.

I was trying hard to include as many patterns in the fast path as I 
could (and there's special case code to advance past the ^ 
start-of-string match prefix in both pattern collecting and pattern 
matching), but I missed this one. Not sure if this is something I can 
sanely patch up or if I should disqualify such patterns from the fast 
path, need to stare at it a bit...

Does https://codeberg.org/landley/toybox/commit/3d7c289a0c35 fix it for you?

Thanks,

Rob


More information about the Toybox mailing list