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

From: Roberto E. Vargas Caballero <k0ga_AT_shike2.com>
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
problem:


diff --git a/config.mk b/config.mk
index 9fb18da..3ca45d2 100644
--- a/config.mk
+++ b/config.mk
_AT_@ -12,5 +12,5 @@ RANLIB = ranlib
 # for NetBSD add -D_NETBSD_SOURCE
 # -lrt might be needed on some systems
 CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_XOPEN_SOURCE=700 -D_FILE_OFFSET_BITS=64
-CFLAGS = -std=c99 -Wall -pedantic
-LDFLAGS = -s
+CFLAGS = -g -std=c99 -Wall -pedantic
+LDFLAGS = -g
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 (udata.nr == 0)
- error("nothing to undo");
+ return;
         for (p = &udata.vec[udata.nr-1]; udata.nr--; --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;
                 chkprint(1);
+ if (udata.nr == 0)
+ error("nothing to undo");
                 undo();
                 break;
         case 's':


Regards,
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