GNU bug report logs - #57733
29.0.50; Fix "Invalid search bound (wrong side of point)" error in replace-*-in-region

Previous Next

Package: emacs;

Reported by: Kévin Le Gouguec <kevin.legouguec <at> gmail.com>

Date: Sun, 11 Sep 2022 16:00:02 UTC

Severity: normal

Found in version 29.0.50

Fixed in version 29.1

Done: Lars Ingebrigtsen <larsi <at> gnus.org>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 57733 in the body.
You can then email your comments to 57733 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-gnu-emacs <at> gnu.org:
bug#57733; Package emacs. (Sun, 11 Sep 2022 16:00:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Kévin Le Gouguec <kevin.legouguec <at> gmail.com>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Sun, 11 Sep 2022 16:00:02 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Kévin Le Gouguec <kevin.legouguec <at> gmail.com>
To: bug-gnu-emacs <at> gnu.org
Subject: 29.0.50; Fix "Invalid search bound (wrong side of point)" error in
 replace-*-in-region
Date: Sun, 11 Sep 2022 17:59:52 +0200
[Message part 1 (text/plain, inline)]
The replace-{string,regexp}-in-region functions can throw an error if
the replacement text is large compared to the replaced text: (point)
might move beyond END, which means the underlying {,re-}search-forward
functions are called with BOUND "on the wrong side of point".

Recipe from emacs -Q:

1. Open *scratch* and type:
(with-temp-buffer
  (insert "foo bar baz")
  (replace-regexp-in-region
   "ba." "quux corge grault" (point-min) (point-max)))
2. C-j

Backtrace attached, with suggested patch (including tests) to update END
to reflect the displacement after each replacement.

I'm assuming these are the semantics we prefer, since users can't call
these functions with (> end (point-max)) to guard against that error,
but we could also just bail as soon as (> (point) end).  That would
prevent the error, but it could cause us to return early and miss some
matches.

The patch does change the semantics in the opposite situation (and the
tests check for the new behaviour): when the replacement is shorter than
the original text, it is currently possible for matches that were
originally beyond END to become reachable; the patch prevents that.  I'm
assuming that's TRT because if users say "replace no further than
THERE", they're probably talking about a point of interest (end of a
line, a function…) rather than the numeric value of THERE.


If we want this change to go in: should we call it out in the docstrings
and/or "(elisp) Search and Replace" and/or in NEWS?

FWIW: I've seen no regression with a full "make check", but I've just
written the patch, so I haven't had the time to live with it and see
what breaks.


In GNU Emacs 29.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version
 3.24.34, cairo version 1.16.0) of 2022-09-10 built on amdahl30
Repository revision: 433fc8bebfef40130bf62f17e5349560b62e566e
Repository branch: master
Windowing system distributor 'The X.Org Foundation', version 11.0.12101004
System Description: openSUSE Tumbleweed

Configured using:
 'configure --with-cairo --with-gconf --with-sqlite3 --with-xinput2'

Configured features:
ACL CAIRO DBUS FREETYPE GIF GLIB GMP GNUTLS GPM GSETTINGS HARFBUZZ JPEG
JSON LCMS2 LIBOTF LIBSELINUX LIBSYSTEMD LIBXML2 M17N_FLT MODULES NOTIFY
INOTIFY PDUMPER PNG RSVG SECCOMP SOUND SQLITE3 THREADS TIFF
TOOLKIT_SCROLL_BARS WEBP X11 XDBE XIM XINPUT2 XPM GTK3 ZLIB

Important settings:
  value of $LANG: en_US.UTF-8
  value of $XMODIFIERS: @im=local
  locale-coding-system: utf-8-unix

[0001-Update-search-bound-while-replacing-matches-in-repla.patch (text/x-patch, attachment)]
[invalid-search-bound.backtrace (text/plain, attachment)]

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#57733; Package emacs. (Sun, 11 Sep 2022 16:18:02 GMT) Full text and rfc822 format available.

Message #8 received at 57733 <at> debbugs.gnu.org (full text, mbox):

From: Lars Ingebrigtsen <larsi <at> gnus.org>
To: Kévin Le Gouguec <kevin.legouguec <at> gmail.com>
Cc: 57733 <at> debbugs.gnu.org
Subject: Re: bug#57733: 29.0.50; Fix "Invalid search bound (wrong side of
 point)" error in replace-*-in-region
Date: Sun, 11 Sep 2022 18:17:17 +0200
Kévin Le Gouguec <kevin.legouguec <at> gmail.com> writes:

> Backtrace attached, with suggested patch (including tests) to update END
> to reflect the displacement after each replacement.

Perhaps a simpler solution would to just be to use
`save-restriction'+`narrow-to-region' first and then don't use an END in
the search?





Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#57733; Package emacs. (Sun, 11 Sep 2022 17:10:02 GMT) Full text and rfc822 format available.

Message #11 received at 57733 <at> debbugs.gnu.org (full text, mbox):

From: Kévin Le Gouguec <kevin.legouguec <at> gmail.com>
To: Lars Ingebrigtsen <larsi <at> gnus.org>
Cc: 57733 <at> debbugs.gnu.org
Subject: Re: bug#57733: 29.0.50; Fix "Invalid search bound (wrong side of
 point)" error in replace-*-in-region
Date: Sun, 11 Sep 2022 19:09:04 +0200
[Message part 1 (text/plain, inline)]
Lars Ingebrigtsen <larsi <at> gnus.org> writes:

> Perhaps a simpler solution would to just be to use
> `save-restriction'+`narrow-to-region' first and then don't use an END in
> the search?

That makes a lot of sense, thanks; new patch attached.

[0001-Restrict-replace-in-region-to-the-bounds-defined-by-.patch (text/x-patch, attachment)]

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#57733; Package emacs. (Sun, 11 Sep 2022 17:56:02 GMT) Full text and rfc822 format available.

Message #14 received at 57733 <at> debbugs.gnu.org (full text, mbox):

From: Lars Ingebrigtsen <larsi <at> gnus.org>
To: Kévin Le Gouguec <kevin.legouguec <at> gmail.com>
Cc: 57733 <at> debbugs.gnu.org
Subject: Re: bug#57733: 29.0.50; Fix "Invalid search bound (wrong side of
 point)" error in replace-*-in-region
Date: Sun, 11 Sep 2022 19:55:14 +0200
Kévin Le Gouguec <kevin.legouguec <at> gmail.com> writes:

> That makes a lot of sense, thanks; new patch attached.

Thanks; pushed to Emacs 29.




bug marked as fixed in version 29.1, send any further explanations to 57733 <at> debbugs.gnu.org and Kévin Le Gouguec <kevin.legouguec <at> gmail.com> Request was from Lars Ingebrigtsen <larsi <at> gnus.org> to control <at> debbugs.gnu.org. (Sun, 11 Sep 2022 17:56:02 GMT) Full text and rfc822 format available.

bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Mon, 10 Oct 2022 11:24:09 GMT) Full text and rfc822 format available.

This bug report was last modified 1 year and 170 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.