Package: w3m
Version: 0.5.3+git20230121-2
Severity: minor

When I search backward in w3m by using the '?' command, the program
freezes with high CPU usage for a bit of time if the found text is near
the end of a very long line.

How to reproduce the issue:

    printf 'first line\nsecond line is %*s a long line\nthird line\n' 10000 '' 
> test.txt
    w3m test.txt

Run those commands and then go to the third line and search backward for
the word "line".  Now the program will freeze for a second.  If you
increase the number 10000, the freezing time will increase with the
square of that number.

The reason that this happens when searching backward but not forward is
that in the function `backwardSearch` in search.c there is this loop:

    while (regexMatch(...) == 1)

but in `forwardSearch` it's just an if statement:

    if (regexMatch(...) == 1)

That means that when searching backward, we do one regexMatch for each
character on the line and since the regexMatch itself already searches
through the characters, that's pretty wasteful and that's what gives us
the squared times.

It should be possible to change this so that an if statement is used in
`backwardSearch` just like in `forwardSearch`.  Maybe there could be two
versions of `regexMatch` where one of them searches backwards or just
returns the last match instead of the first one so that we don't have to
call it in a loop.

Reply via email to