Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/make make(1): avoid undefined behavior in Cmd_Exec



details:   https://anonhg.NetBSD.org/src/rev/4fa6a85a41f0
branches:  trunk
changeset: 937123:4fa6a85a41f0
user:      rillig <rillig%NetBSD.org@localhost>
date:      Sun Aug 09 09:26:21 2020 +0000

description:
make(1): avoid undefined behavior in Cmd_Exec

Iterating the command output backwards was dangerous since at the end,
the pointer cp pointed outside of the array.  Even without dereferencing
this pointer, this already invokes undefined behavior (C11, 6.5.6p8).
Don't risk anything.  Iterating forwards is probably faster anyway, since
it is more common.

diffstat:

 usr.bin/make/main.c |  15 ++++++---------
 1 files changed, 6 insertions(+), 9 deletions(-)

diffs (45 lines):

diff -r ffb0b89c7fe9 -r 4fa6a85a41f0 usr.bin/make/main.c
--- a/usr.bin/make/main.c       Sun Aug 09 09:23:17 2020 +0000
+++ b/usr.bin/make/main.c       Sun Aug 09 09:26:21 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: main.c,v 1.299 2020/08/09 09:07:54 rillig Exp $        */
+/*     $NetBSD: main.c,v 1.300 2020/08/09 09:26:21 rillig Exp $        */
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -69,7 +69,7 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: main.c,v 1.299 2020/08/09 09:07:54 rillig Exp $";
+static char rcsid[] = "$NetBSD: main.c,v 1.300 2020/08/09 09:26:21 rillig Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
@@ -81,7 +81,7 @@
 #if 0
 static char sccsid[] = "@(#)main.c     8.3 (Berkeley) 3/19/94";
 #else
-__RCSID("$NetBSD: main.c,v 1.299 2020/08/09 09:07:54 rillig Exp $");
+__RCSID("$NetBSD: main.c,v 1.300 2020/08/09 09:26:21 rillig Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -1699,14 +1699,11 @@
            *errfmt = "\"%s\" returned non-zero status";
 
        /* Convert newlines to spaces.  A final newline is just stripped */
-       cp = &res[res_len];
-       if (res_len > 0 && *--cp == '\n')
-           *cp-- = '\0';
-       while (cp >= res) {
+       if (res_len > 0 && res[res_len - 1] == '\n')
+           res[res_len - 1] = '\0';
+       for (cp = res; *cp != '\0'; cp++)
            if (*cp == '\n')
                *cp = ' ';
-           cp--;
-       }
        break;
     }
     return res;



Home | Main Index | Thread Index | Old Index