Subject: bug#78690: 31.0.50; split string: args out of range with TRIM

From: Eli Zaretskii <[email-masked]>
Date: Thu, 05 Jun 2025 18:48:44 +0300

> Date: Wed, 04 Jun 2025 04:36:26 +0200
> From:  Michael Heerdegen via "Bug reports for GNU Emacs,
>  the Swiss army knife of text editors" <[email-masked]>
> 
> 
> Hello,
> 
> I stumbled across this:
> 
> #+begin_src emacs-lisp
> (split-string
>  "-*- lexical-binding: t; -*-"
>  "-\\*-" nil "[ \t\n\r-]+")
> #+end_src
> 
> ~~>
> 
> | Debugger entered--Lisp error: (args-out-of-range "-*- lexical-binding: t; -*-" 1 0)
> |   (substring "-*- lexical-binding: t; -*-" 1 0)
> |   (let ((this (substring string this-start this-end))) (if trim (progn (let ((tem (string-match (concat trim "\\'") this 0))) (and tem (< tem (length this)) (setq this (substring this 0 tem)))))) (if (or keep-nulls (> (length this) 0)) (progn (setq list (cons this list)))))

It is quite obvious that split-string is not prepared to deal with a
situation where the argument STRING begins with a match for
SEPARATORS.  The breakage here happens because the match for
SEPARATORS at the very beginning of STRING also matches TRIM, but even
if that is not so, a match for SEPARATORS at the beginning of STRING
sets THIS-START incorrectly for the first call to push-one inside the
while-loop.