GNU bug report logs - #19208
replace-match unhelpful error message

Previous Next

Package: emacs;

Reported by: phillip.lord <at> newcastle.ac.uk (Phillip Lord)

Date: Fri, 28 Nov 2014 17:01:02 UTC

Severity: minor

Tags: fixed

Fixed in version 27.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 19208 in the body.
You can then email your comments to 19208 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#19208; Package emacs. (Fri, 28 Nov 2014 17:01:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to phillip.lord <at> newcastle.ac.uk (Phillip Lord):
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Fri, 28 Nov 2014 17:01:02 GMT) Full text and rfc822 format available.

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

From: phillip.lord <at> newcastle.ac.uk (Phillip Lord)
To: bug-gnu-emacs <at> gnu.org
Subject: replace-match unhelpful error message
Date: Fri, 28 Nov 2014 17:00:15 +0000
Occurs in Emacs 24.3 and trunk

Running this code in *scratch* buffer

(progn
  (goto-char (point-min))
  (re-search-forward "This")
  (replace-match "That" nil nil nil))

Now undo and run this code

(progn
  (goto-char (point-min))
  (re-search-forward "This")
  (replace-match "That" nil nil nil 1))

This causes the following error.

progn: Args out of range: -1, -1


The error is caused because I have asked for a subexp that doesn't exist in
the regexp. A programmer error for sure, but the Args out of range error seems
rather unfriendly to me, especially as I have no idea where the -1, or
-1 has come from.

"Attempt to replace regexp subexpression that doesn't exist", for
example, would be nicer.






Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#19208; Package emacs. (Sat, 03 Aug 2019 14:02:02 GMT) Full text and rfc822 format available.

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

From: Lars Ingebrigtsen <larsi <at> gnus.org>
To: phillip.lord <at> newcastle.ac.uk (Phillip Lord)
Cc: 19208 <at> debbugs.gnu.org
Subject: Re: bug#19208: replace-match unhelpful error message
Date: Sat, 03 Aug 2019 16:01:15 +0200
phillip.lord <at> newcastle.ac.uk (Phillip Lord) writes:

> (progn
>   (goto-char (point-min))
>   (re-search-forward "This")
>   (replace-match "That" nil nil nil 1))
>
> This causes the following error.
>
> progn: Args out of range: -1, -1
>
> The error is caused because I have asked for a subexp that doesn't exist in
> the regexp. A programmer error for sure, but the Args out of range error seems
> rather unfriendly to me, especially as I have no idea where the -1, or
> -1 has come from.
>
> "Attempt to replace regexp subexpression that doesn't exist", for
> example, would be nicer.

Yup.

This is the code that signals the error:

      if (search_regs.start[sub] < BEGV
	  || search_regs.start[sub] > search_regs.end[sub]
	  || search_regs.end[sub] > ZV)
	args_out_of_range (make_fixnum (search_regs.start[sub]),
			   make_fixnum (search_regs.end[sub]));

Which is a weird way to check for this, surely?  It looks more like a
sanity checks that checks whether start[sub] and end[sub] is inside the
buffer, and that the start is before the end.

This happens to actually work here because when there is no match,
start[sub] is -1, which is always less than BEGV.

And this explains where the -1 in the args out of range comes from.

The sanity check seems sensible (because the buffer may have been
narrowed before calling replace-match), so I propose to add an
additional check that actually explicitly checks whether the subexp is
valid.

Hm...  or is this what the check right above it tried to do?

  if (NILP (subexp))
    sub = 0;
  else
    {
      CHECK_FIXNUM (subexp);
      if (! (0 <= XFIXNUM (subexp) && XFIXNUM (subexp) < search_regs.num_regs))
	args_out_of_range (subexp, make_fixnum (search_regs.num_regs));
      sub = XFIXNUM (subexp);
    }

Yoda conditionals and a !...  Let's see...

No, that's just another sanity check -- num_regs is the max allowed
number of sub-matches.  (I've now added some comments to clarify.)

I've now added a new check to check for this situation, and I've used
your suggested error string.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no




Added tag(s) fixed. Request was from Lars Ingebrigtsen <larsi <at> gnus.org> to control <at> debbugs.gnu.org. (Sat, 03 Aug 2019 14:02:03 GMT) Full text and rfc822 format available.

bug marked as fixed in version 27.1, send any further explanations to 19208 <at> debbugs.gnu.org and phillip.lord <at> newcastle.ac.uk (Phillip Lord) Request was from Lars Ingebrigtsen <larsi <at> gnus.org> to control <at> debbugs.gnu.org. (Sat, 03 Aug 2019 14:02:03 GMT) Full text and rfc822 format available.

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#19208; Package emacs. (Sat, 03 Aug 2019 20:23:02 GMT) Full text and rfc822 format available.

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

From: Paul Eggert <eggert <at> cs.ucla.edu>
To: Lars Ingebrigtsen <larsi <at> gnus.org>
Cc: Phillip Lord <phillip.lord <at> newcastle.ac.uk>, 19208 <at> debbugs.gnu.org
Subject: Re: bug#19208: replace-match unhelpful error message
Date: Sat, 3 Aug 2019 13:22:26 -0700
[Message part 1 (text/plain, inline)]
> No, that's just another sanity check -- num_regs is the max allowed
> number of sub-matches.  (I've now added some comments to clarify.)

Unfortunately that patch messes up the sanity check, as the patched code allows 
'sub' to be negative, or to be equal to search_regs.num_regs, and in either case 
this results in a bad pointer.

> Yoda conditionals and a !...

Actually those were Leibniz conditionals, which are comparisons involving "<" or 
"<=". The idea is that the conditionals' textual order reflects numeric order. 
This is a common style in math when doing range checking, e.g., "0 <= i < n". 
Yoda conditionals are expressions like "0 != x" which I agree are confusing.

I learned Leibniz conditionals from the late Val Schorre, who was *really* good 
at programming and programming style: Knuth credits Schorre with the invention 
of goto-less programming in the early 1960s. When I redid your patch as a 
Leibniz conditional, I instantly spotted the bug that it introduced. You might 
give Leibniz conditionals a try, as they help make code more readable and 
reliable when checking for range errors or overflow.

While in the neighborhood I spotted some other rare possibilities for bad 
behavior due to out-of-range indexes. I fixed the bugs I found by installing the 
attached.
[0001-Fix-rare-undefined-behaviors-in-replace-match.patch (text/x-patch, attachment)]

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#19208; Package emacs. (Sat, 03 Aug 2019 20:32:02 GMT) Full text and rfc822 format available.

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

From: Lars Ingebrigtsen <larsi <at> gnus.org>
To: Paul Eggert <eggert <at> cs.ucla.edu>
Cc: Phillip Lord <phillip.lord <at> newcastle.ac.uk>, 19208 <at> debbugs.gnu.org
Subject: Re: bug#19208: replace-match unhelpful error message
Date: Sat, 03 Aug 2019 22:31:17 +0200
Paul Eggert <eggert <at> cs.ucla.edu> writes:

>> No, that's just another sanity check -- num_regs is the max allowed
>> number of sub-matches.  (I've now added some comments to clarify.)
>
> Unfortunately that patch messes up the sanity check, as the patched
> code allows 'sub' to be negative, or to be equal to
> search_regs.num_regs, and in either case this results in a bad
> pointer.

Sorry; thanks for fixing.

>> Yoda conditionals and a !...
>
> Actually those were Leibniz conditionals, which are comparisons
> involving "<" or "<=". The idea is that the conditionals' textual
> order reflects numeric order. This is a common style in math when
> doing range checking, e.g., "0 <= i < n". Yoda conditionals are
> expressions like "0 != x" which I agree are confusing.

Well, I think both are called Yoda conditionals, and they are
demonstrably confusing for people not used to seeing conditionals
written that way, which is I think pretty much everybody, except four
people working on Emacs.  :-)

In real life, nobody says "if 62 is less than your age, you're eligible
for Medicare".  I think one should write code for legibility -- but
opinions on what's legible various, of course.  Reading "if (62 < age)",
for me, requires mental gymnastics.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#19208; Package emacs. (Sat, 03 Aug 2019 20:52:01 GMT) Full text and rfc822 format available.

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

From: Paul Eggert <eggert <at> cs.ucla.edu>
To: Lars Ingebrigtsen <larsi <at> gnus.org>
Cc: Phillip Lord <phillip.lord <at> newcastle.ac.uk>, 19208 <at> debbugs.gnu.org
Subject: Re: bug#19208: replace-match unhelpful error message
Date: Sat, 3 Aug 2019 13:50:55 -0700
Lars Ingebrigtsen wrote:
> they are
> demonstrably confusing for people not used to seeing conditionals
> written that way, which is I think pretty much everybody, except four
> people working on Emacs.  :-)

Just four?  :-)  (Actually the style is also used in other GNU projects.)

In this particular case the Leibniz style caught a potentially-serious bug. 
Although I understand that it can be a minor irritation for newcomers, it is 
quite useful for range checking and in practice I find that its reliability 
outweighs any irritation. (I didn't like Val Schorre's advice either, when I 
first heard it - but he was right.)




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#19208; Package emacs. (Sun, 04 Aug 2019 00:00:02 GMT) Full text and rfc822 format available.

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

From: Noam Postavsky <npostavs <at> gmail.com>
To: Lars Ingebrigtsen <larsi <at> gnus.org>
Cc: 19208 <at> debbugs.gnu.org, Phillip Lord <phillip.lord <at> newcastle.ac.uk>,
 Paul Eggert <eggert <at> cs.ucla.edu>
Subject: Re: bug#19208: replace-match unhelpful error message
Date: Sat, 03 Aug 2019 19:59:45 -0400
>> Actually those were Leibniz conditionals, which are comparisons
>> involving "<" or "<=". The idea is that the conditionals' textual
>> order reflects numeric order. This is a common style in math when
>> doing range checking, e.g., "0 <= i < n". Yoda conditionals are
>> expressions like "0 != x" which I agree are confusing.
>
> Well, I think both are called Yoda conditionals, and they are
> demonstrably confusing for people not used to seeing conditionals
> written that way, which is I think pretty much everybody, except four
> people working on Emacs.  :-)
>
> In real life, nobody says "if 62 is less than your age, you're eligible
> for Medicare".

People might say things like "your height must be between 120 and 200 cm
to ride this roller coaster" as opposed to "your height must be more
than 120 cm and your height must be less than 200 cm".

> I think one should write code for legibility -- but opinions on what's
> legible various, of course.  Reading "if (62 < age)", for me, requires
> mental gymnastics.

"if (height > 120 && height < 200)" require mental gymnastics, for me,
because each comparison is a different direction.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#19208; Package emacs. (Sun, 04 Aug 2019 09:03:01 GMT) Full text and rfc822 format available.

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

From: Andreas Schwab <schwab <at> linux-m68k.org>
To: Noam Postavsky <npostavs <at> gmail.com>
Cc: 19208 <at> debbugs.gnu.org, Lars Ingebrigtsen <larsi <at> gnus.org>,
 Phillip Lord <phillip.lord <at> newcastle.ac.uk>, Paul Eggert <eggert <at> cs.ucla.edu>
Subject: Re: bug#19208: replace-match unhelpful error message
Date: Sun, 04 Aug 2019 11:02:26 +0200
On Aug 03 2019, Noam Postavsky <npostavs <at> gmail.com> wrote:

> People might say things like "your height must be between 120 and 200 cm
> to ride this roller coaster"

This still puts "height" (the variable) first.

Andreas.

-- 
Andreas Schwab, schwab <at> linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#19208; Package emacs. (Sun, 04 Aug 2019 09:10:02 GMT) Full text and rfc822 format available.

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

From: Andreas Schwab <schwab <at> linux-m68k.org>
To: Paul Eggert <eggert <at> cs.ucla.edu>
Cc: Phillip Lord <phillip.lord <at> newcastle.ac.uk>,
 Lars Ingebrigtsen <larsi <at> gnus.org>, 19208 <at> debbugs.gnu.org
Subject: Re: bug#19208: replace-match unhelpful error message
Date: Sun, 04 Aug 2019 11:09:25 +0200
On Aug 03 2019, Paul Eggert <eggert <at> cs.ucla.edu> wrote:

> Lars Ingebrigtsen wrote:
>> they are
>> demonstrably confusing for people not used to seeing conditionals
>> written that way, which is I think pretty much everybody, except four
>> people working on Emacs.  :-)
>
> Just four?  :-)  (Actually the style is also used in other GNU projects.)

And always the same four people.

Andreas.

-- 
Andreas Schwab, schwab <at> linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#19208; Package emacs. (Sun, 04 Aug 2019 09:19:02 GMT) Full text and rfc822 format available.

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

From: Andreas Schwab <schwab <at> linux-m68k.org>
To: Paul Eggert <eggert <at> cs.ucla.edu>
Cc: Phillip Lord <phillip.lord <at> newcastle.ac.uk>,
 Lars Ingebrigtsen <larsi <at> gnus.org>, 19208 <at> debbugs.gnu.org
Subject: Re: bug#19208: replace-match unhelpful error message
Date: Sun, 04 Aug 2019 11:18:17 +0200
On Aug 03 2019, Paul Eggert <eggert <at> cs.ucla.edu> wrote:

> I learned Leibniz conditionals from the late Val Schorre, who was *really*
> good at programming and programming style: Knuth credits Schorre with the
> invention of goto-less programming in the early 1960s. When I redid your
> patch as a Leibniz conditional, I instantly spotted the bug that it
> introduced. You might give Leibniz conditionals a try, as they help make
> code more readable and reliable when checking for range errors or
> overflow.

Perhaps those that need that construct are not good enough to cope with
natural languages.

Andreas.

-- 
Andreas Schwab, schwab <at> linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#19208; Package emacs. (Sun, 04 Aug 2019 11:48:02 GMT) Full text and rfc822 format available.

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

From: Lars Ingebrigtsen <larsi <at> gnus.org>
To: Noam Postavsky <npostavs <at> gmail.com>
Cc: 19208 <at> debbugs.gnu.org, Phillip Lord <phillip.lord <at> newcastle.ac.uk>,
 Paul Eggert <eggert <at> cs.ucla.edu>
Subject: Re: bug#19208: replace-match unhelpful error message
Date: Sun, 04 Aug 2019 13:47:00 +0200
Noam Postavsky <npostavs <at> gmail.com> writes:

> People might say things like "your height must be between 120 and 200 cm
> to ride this roller coaster" as opposed to "your height must be more
> than 120 cm and your height must be less than 200 cm".

And they don't say "120 cm must be less than your height, which must also
be less than 200 cm".  :-)

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#19208; Package emacs. (Sun, 04 Aug 2019 14:13:02 GMT) Full text and rfc822 format available.

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

From: Noam Postavsky <npostavs <at> gmail.com>
To: Andreas Schwab <schwab <at> linux-m68k.org>
Cc: Phillip Lord <phillip.lord <at> newcastle.ac.uk>,
 Lars Ingebrigtsen <larsi <at> gnus.org>, 19208 <at> debbugs.gnu.org,
 Paul Eggert <eggert <at> cs.ucla.edu>
Subject: Re: bug#19208: replace-match unhelpful error message
Date: Sun, 04 Aug 2019 10:12:06 -0400
Andreas Schwab <schwab <at> linux-m68k.org> writes:

> On Aug 03 2019, Noam Postavsky <npostavs <at> gmail.com> wrote:
>
>> People might say things like "your height must be between 120 and 200 cm
>> to ride this roller coaster"
>
> This still puts "height" (the variable) first.

Lars Ingebrigtsen <larsi <at> gnus.org> writes:
>
> And they don't say "120 cm must be less than your height, which must also
> be less than 200 cm".  :-)

Right, but unless we want to use a macro like

    IN_RANGE_P (height, 120, 200)

I think ((120 < height) && (height < 200)) is the closest we can get to
the natural phrasing.  In Elisp we can do (< 120 height 200) which is
nicer, but we're still stuck with `and' for semi-closed intervals.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#19208; Package emacs. (Sun, 04 Aug 2019 15:14:02 GMT) Full text and rfc822 format available.

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

From: Andreas Schwab <schwab <at> linux-m68k.org>
To: Noam Postavsky <npostavs <at> gmail.com>
Cc: Phillip Lord <phillip.lord <at> newcastle.ac.uk>,
 Lars Ingebrigtsen <larsi <at> gnus.org>, 19208 <at> debbugs.gnu.org,
 Paul Eggert <eggert <at> cs.ucla.edu>
Subject: Re: bug#19208: replace-match unhelpful error message
Date: Sun, 04 Aug 2019 17:13:12 +0200
On Aug 04 2019, Noam Postavsky <npostavs <at> gmail.com> wrote:

> I think ((120 < height) && (height < 200)) is the closest we can get to
> the natural phrasing.

"X is between A and B" means "X is bigger than A and [X is] smaller than
B".  You would never say "A is smaller than X and [X is] smaller than
B".

Andreas.

-- 
Andreas Schwab, schwab <at> linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#19208; Package emacs. (Sun, 04 Aug 2019 17:42:01 GMT) Full text and rfc822 format available.

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

From: Paul Eggert <eggert <at> cs.ucla.edu>
To: Andreas Schwab <schwab <at> linux-m68k.org>, Noam Postavsky <npostavs <at> gmail.com>
Cc: Phillip Lord <phillip.lord <at> newcastle.ac.uk>,
 Lars Ingebrigtsen <larsi <at> gnus.org>, 19208 <at> debbugs.gnu.org
Subject: Re: bug#19208: replace-match unhelpful error message
Date: Sun, 4 Aug 2019 10:41:40 -0700
This is not strictly a question of what one would say in English, as 
mathematical notation is typically a better choice for mathematical notions. For 
example, in C it's typically better to write sixteen as "16" rather than as "6 + 
10" even though the latter is closer to the original English.

If we wanted code to mimic English better, COBOL would be a good choice. (Or 
maybe Perl. :-) But the goal is to write readable code, not to imitate how 
Anglo-Saxons talked.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#19208; Package emacs. (Sun, 04 Aug 2019 18:15:01 GMT) Full text and rfc822 format available.

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

From: Andreas Schwab <schwab <at> linux-m68k.org>
To: Paul Eggert <eggert <at> cs.ucla.edu>
Cc: Phillip Lord <phillip.lord <at> newcastle.ac.uk>,
 Lars Ingebrigtsen <larsi <at> gnus.org>, 19208 <at> debbugs.gnu.org,
 Noam Postavsky <npostavs <at> gmail.com>
Subject: Re: bug#19208: replace-match unhelpful error message
Date: Sun, 04 Aug 2019 20:14:19 +0200
On Aug 04 2019, Paul Eggert <eggert <at> cs.ucla.edu> wrote:

> readable code

Exactly.

Andreas.

-- 
Andreas Schwab, schwab <at> linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#19208; Package emacs. (Mon, 05 Aug 2019 02:26:02 GMT) Full text and rfc822 format available.

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

From: Richard Stallman <rms <at> gnu.org>
To: Noam Postavsky <npostavs <at> gmail.com>
Cc: phillip.lord <at> newcastle.ac.uk, larsi <at> gnus.org, 19208 <at> debbugs.gnu.org,
 schwab <at> linux-m68k.org, eggert <at> cs.ucla.edu
Subject: Re: bug#19208: replace-match unhelpful error message
Date: Sun, 04 Aug 2019 22:25:32 -0400
[[[ To any NSA and FBI agents reading my email: please consider    ]]]
[[[ whether defending the US Constitution against all enemies,     ]]]
[[[ foreign or domestic, requires you to follow Snowden's example. ]]]

  > I think ((120 < height) && (height < 200)) is the closest we can get to
  > the natural phrasing.

That parallels the mathematical expression 120 < h < 200, which is a
plus, but doesn't parallel the textual form "height must be between
120 and 200", which is a minus.

The way to parallel the latter is (height > 120 && height < 200).

I think both of those are ok, but let's avoid other orderings
-- 
Dr Richard Stallman
President, Free Software Foundation (https://gnu.org, https://fsf.org)
Internet Hall-of-Famer (https://internethalloffame.org)






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

This bug report was last modified 4 years and 209 days ago.

Previous Next


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