Re: [hackers] [sbase] [PATCH] ed: Do not try to read-in a nonexistant file

From: Roberto E. Vargas Caballero <>
Date: Mon, 28 Dec 2015 19:25:57 +0100

On Sat, Dec 26, 2015 at 05:02:39PM -0500, Wolfgang Corcoran-Mathe wrote:
> This fixes a segfault caused by running ed with a
> nonexistant filename argument, e.g. 'ed not_a_file_yet'.

Good catch, but I don't like the solution. I think you are
fixing the problem in the incorrect place. The problem here
is not that the file doesn't exists, but the problem is
we are calling undo(), which tries to undo the work of the
current command, which is controled by newcmd, and newcmd
is 0 because we are out of edit().

It causes that doread() calls to error(), which calls
to undo() which calls to error(), and then we have
an infinite recursivity.

I think the solution must be removing the call to error()
in undo(). I think something like this patch solves the

diff --git a/ b/
index 9fb18da..3ca45d2 100644
--- a/
+++ b/
_AT_@ -12,5 +12,5 @@ RANLIB = ranlib
 # for NetBSD add -D_NETBSD_SOURCE
 # -lrt might be needed on some systems
-CFLAGS = -std=c99 -Wall -pedantic
+CFLAGS = -g -std=c99 -Wall -pedantic
diff --git a/ed.c b/ed.c
index 8903957..623c6b4 100644
--- a/ed.c
+++ b/ed.c
_AT_@ -282,7 +282,7 @@ undo(void)
         struct link *p;
         if ( == 0)
- error("nothing to undo");
+ return;
         for (p = &udata.vec[];; --p) {
                 zero[p->from1].next = p->to1;
                 zero[p->from2].prev = p->to2;
_AT_@ -1101,6 +1101,8 @@ repeat:
                 if (nlines > 0)
                         goto bad_address;
+ if ( == 0)
+ error("nothing to undo");
         case 's':

Received on Mon Dec 28 2015 - 19:25:57 CET

This archive was generated by hypermail 2.3.0 : Mon Dec 28 2015 - 19:36:11 CET