]> git.eshelyaron.com Git - emacs.git/commitdiff
Fix replace-in-string infloop with empty pattern string (bug#43598)
authorMattias Engdegård <mattiase@acm.org>
Fri, 25 Sep 2020 11:15:42 +0000 (13:15 +0200)
committerMattias Engdegård <mattiase@acm.org>
Fri, 25 Sep 2020 11:15:42 +0000 (13:15 +0200)
* lisp/subr.el (replace-in-string): Raise an error if FROMSTRING is
empty.
* test/lisp/subr-tests.el (replace-in-string): Add test case.

lisp/subr.el
test/lisp/subr-tests.el

index 33ed0bc93671ee39371c7c044d9afd60db71b8a6..fba31b7cf75786ee2700bbf77524bf0a797ad7c4 100644 (file)
@@ -4432,6 +4432,8 @@ Unless optional argument INPLACE is non-nil, return a new string."
 (defun replace-in-string (fromstring tostring instring)
   "Replace FROMSTRING with TOSTRING in INSTRING each time it occurs."
   (declare (pure t))
+  (when (equal fromstring "")
+    (signal 'wrong-length-argument fromstring))
   (let ((start 0)
         (result nil)
         pos)
index fa728e430fc1137bb411132d7245900a818094e5..505408fa110ad80c6180d5815339ab1e78b7d1b5 100644 (file)
@@ -464,7 +464,9 @@ See https://debbugs.gnu.org/cgi/bugreport.cgi?bug=19350."
   (should (equal (replace-in-string "\377" "x" "a\377b")
                  "axb"))
   (should (equal (replace-in-string "\377" "x" "a\377ø")
-                 "axø")))
+                 "axø"))
+
+  (should-error (replace-in-string "" "x" "abc")))
 
 (provide 'subr-tests)
 ;;; subr-tests.el ends here