Module Name:    src
Committed By:   rillig
Date:           Sun Aug  9 15:07:13 UTC 2020

Modified Files:
        src/usr.bin/make: var.c

Log Message:
make(1): clean up code for subexpressions in the :C modifier

An ampersand in the replacement string can never produce an
out-of-bounds error or an undefined-subexpression error.  This makes the
error message simpler since it only needs to cover the case of a single
digit.


To generate a diff of this commit:
cvs rdiff -u -r1.444 -r1.445 src/usr.bin/make/var.c

Please note that diffs are not public domain; they are subject to the
copyright notices on the relevant files.

Modified files:

Index: src/usr.bin/make/var.c
diff -u src/usr.bin/make/var.c:1.444 src/usr.bin/make/var.c:1.445
--- src/usr.bin/make/var.c:1.444	Sun Aug  9 14:30:35 2020
+++ src/usr.bin/make/var.c	Sun Aug  9 15:07:13 2020
@@ -1,4 +1,4 @@
-/*	$NetBSD: var.c,v 1.444 2020/08/09 14:30:35 rillig Exp $	*/
+/*	$NetBSD: var.c,v 1.445 2020/08/09 15:07:13 rillig Exp $	*/
 
 /*
  * Copyright (c) 1988, 1989, 1990, 1993
@@ -69,14 +69,14 @@
  */
 
 #ifndef MAKE_NATIVE
-static char rcsid[] = "$NetBSD: var.c,v 1.444 2020/08/09 14:30:35 rillig Exp $";
+static char rcsid[] = "$NetBSD: var.c,v 1.445 2020/08/09 15:07:13 rillig Exp $";
 #else
 #include <sys/cdefs.h>
 #ifndef lint
 #if 0
 static char sccsid[] = "@(#)var.c	8.3 (Berkeley) 3/19/94";
 #else
-__RCSID("$NetBSD: var.c,v 1.444 2020/08/09 14:30:35 rillig Exp $");
+__RCSID("$NetBSD: var.c,v 1.445 2020/08/09 15:07:13 rillig Exp $");
 #endif
 #endif /* not lint */
 #endif
@@ -1384,36 +1384,34 @@ tryagain:
 	    if (*rp == '\\' && (rp[1] == '&' || rp[1] == '\\')) {
 		SepBuf_AddBytes(buf, rp + 1, 1);
 		rp++;
-	    } else if (*rp == '&' ||
-		       (*rp == '\\' && isdigit((unsigned char)rp[1]))) {
-		int n;
-		char errstr[3];
-
-		if (*rp == '&') {
-		    n = 0;
-		    errstr[0] = '&';
-		    errstr[1] = '\0';
-		} else {
-		    n = rp[1] - '0';
-		    errstr[0] = '\\';
-		    errstr[1] = rp[1];
-		    errstr[2] = '\0';
-		    rp++;
-		}
+		continue;
+	    }
+
+	    if (*rp == '&') {
+		SepBuf_AddBytesBetween(buf, wp + m[0].rm_so, wp + m[0].rm_eo);
+		continue;
+	    }
+
+	    if (*rp != '\\' || !isdigit((unsigned char)rp[1])) {
+		SepBuf_AddBytes(buf, rp, 1);
+		continue;
+	    }
+
+	    {			/* \0 to \9 backreference */
+		int n = rp[1] - '0';
+		rp++;
 
 		if (n >= args->nsub) {
-		    Error("No subexpression %s", errstr);
+		    Error("No subexpression \\%d", n);
 		} else if (m[n].rm_so == -1 && m[n].rm_eo == -1) {
-		    Error("No match for subexpression %s", errstr);
+		    Error("No match for subexpression \\%d", n);
 		} else {
 		    SepBuf_AddBytesBetween(buf, wp + m[n].rm_so,
 					   wp + m[n].rm_eo);
 		}
-
-	    } else {
-		SepBuf_AddBytes(buf, rp, 1);
 	    }
 	}
+
 	wp += m[0].rm_eo;
 	if (args->pflags & VARP_SUB_GLOBAL) {
 	    flags |= REG_NOTBOL;

Reply via email to