Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Question on re-search-forward and infinite loop

4 views
Skip to first unread message

David Maus

unread,
Nov 26, 2009, 4:01:04 PM11/26/09
to help-gn...@gnu.org
Hi,

By accident I stumpled on a way to force Emacs in a infinite loop with
`re-search-forward'.

If I create a shiny new buffer, insert something like this:

,----
| (while (re-search-forward "^[ \t]*"))
| Something else
`----

-- go back beyond the while-clause and evaluate it, Emacs enters an
infinite loop. As far as I was able to find out
`re-search-forward' with this regexp moves point to the beginning
of the next line above the "S" of "Something else" and stays there
forever so it never reaches end-of-buffer.

As I dunno much (or better: anything) on Emacs' interiors my question
is: Why does this happen?

Regards,

-- David

--
OpenPGP... 0x316F4BE4670716FD
Jabber.... dmj...@jabber.org
Email..... maus....@gmail.com
ICQ....... 241051416


Lennart Borgman

unread,
Nov 26, 2009, 4:11:24 PM11/26/09
to David Maus, help-gn...@gnu.org
On Thu, Nov 26, 2009 at 10:01 PM, David Maus <maus....@gmail.com> wrote:
> Hi,
>
> By accident I stumpled on a way to force Emacs in a infinite loop with
> `re-search-forward'.
>
> If I create a shiny new buffer, insert something like this:
>
> ,----
> | (while (re-search-forward "^[ \t]*"))
> | Something else
> `----
>
>  -- go back beyond the while-clause and evaluate it, Emacs enters an
>    infinite loop. As far as I was able to find out
>    `re-search-forward' with this regexp moves point to the beginning
>    of the next line above the "S" of "Something else" and stays there
>    forever so it never reaches end-of-buffer.
>
> As I dunno much (or better: anything) on Emacs' interiors my question
> is: Why does this happen?


If the match on next line is 0 chars long then it will not move forward.


Barry Margolin

unread,
Nov 26, 2009, 6:17:16 PM11/26/09
to
In article <mailman.11588.12592692...@gnu.org>,
David Maus <maus....@gmail.com> wrote:

> Hi,
>
> By accident I stumpled on a way to force Emacs in a infinite loop with
> `re-search-forward'.
>
> If I create a shiny new buffer, insert something like this:
>
> ,----
> | (while (re-search-forward "^[ \t]*"))
> | Something else
> `----
>
> -- go back beyond the while-clause and evaluate it, Emacs enters an
> infinite loop. As far as I was able to find out
> `re-search-forward' with this regexp moves point to the beginning
> of the next line above the "S" of "Something else" and stays there
> forever so it never reaches end-of-buffer.
>
> As I dunno much (or better: anything) on Emacs' interiors my question
> is: Why does this happen?

Your regexp matches zero or more whitespace characters at the beginning
of a line. If there's an empty line at the end of the buffer, it will
match that. But since it's a zero-length match, point is left at that
position, and the next time around it again matches it.

In general, whenever a regexp can match a zero-length string, I think
that type of loop will go infinitely.

--
Barry Margolin, bar...@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***

David Maus

unread,
Nov 30, 2009, 3:24:34 AM11/30/09
to help-gn...@gnu.org
Hi there,

At Thu, 26 Nov 2009 18:17:16 -0500,


Barry Margolin wrote:
>
> Your regexp matches zero or more whitespace characters at the beginning
> of a line. If there's an empty line at the end of the buffer, it will
> match that. But since it's a zero-length match, point is left at that
> position, and the next time around it again matches it.
>
> In general, whenever a regexp can match a zero-length string, I think
> that type of loop will go infinitely.

Thanks, this makes perfect sense to me.

0 new messages