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