Subject: Re: bin/25899: sed fails when 2048'th character is a backslash
To: None <tech-userlevel@NetBSD.org>
From: Min Sik Kim <minskim@NetBSD.org>
List: tech-userlevel
Date: 06/11/2004 22:26:25
--Signature=_Fri__11_Jun_2004_22_26_25_-0500_QfZ.QGZq9lOnsc=5
Content-Type: text/plain; charset=US-ASCII
Content-Disposition: inline
Content-Transfer-Encoding: 7bit

* Min Sik Kim, Thu, 10 Jun 2004 20:04:39 -0500 (CDT):
> >Number:         25899
> >Category:       bin
> >Synopsis:       sed fails when 2048'th character is a backslash
> >Description:
> When reading a command from a file using '-f' option, sed fails as
> follows if the 2048'th character in a subst command is a backslash.
> 
>   % sed -f sedcmd
>   sed: 1: sedcmd: \ not defined in the RE

FreeBSD already fixed this as follows.  Is it okay to commit?

-- 
Min Sik Kim


Index: compile.c
===================================================================
RCS file: /cvsroot/src/usr.bin/sed/compile.c,v
retrieving revision 1.25
diff -u -r1.25 compile.c
--- compile.c	7 Aug 2003 11:15:49 -0000	1.25
+++ compile.c	12 Jun 2004 03:23:23 -0000
@@ -481,6 +481,7 @@
 	static char lbuf[_POSIX2_LINE_MAX + 1];
 	int asize, ref, size;
 	char c, *text, *op, *sp;
+	int sawesc = 0;
 
 	c = *p++;			/* Terminator character */
 	if (c == '\0')
@@ -494,9 +495,29 @@
 	do {
 		op = sp = text + size;
 		for (; *p; p++) {
-			if (*p == '\\') {
-				p++;
-				if (strchr("123456789", *p) != NULL) {
+			if (*p == '\\' || sawesc) {
+				/*
+				 * If this is a continuation from the last
+				 * buffer, we won't have a character to
+				 * skip over.
+				 */
+				if (sawesc)
+					sawesc = 0;
+				else
+					p++;
+
+				if (*p == '\0') {
+					/*
+					 * This escaped character is continued
+					 * in the next part of the line.  Note
+					 * this fact, then cause the loop to
+					 * exit w/ normal EOL case and reenter
+					 * above with the new buffer.
+					 */
+					sawesc = 1;
+					p--;
+					continue;
+				} else if (strchr("123456789", *p) != NULL) {
 					*sp++ = '\\';
 					ref = *p - '0';
 					if (s->re != NULL &&

--Signature=_Fri__11_Jun_2004_22_26_25_-0500_QfZ.QGZq9lOnsc=5
Content-Type: application/pgp-signature

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (NetBSD)

iQEVAwUBQMp34fQsKR/Zi7L+AQI0wAf/d6FMxoHRumPOVeH1WzxyEV/TkxRp7dKM
LNXhKb1Tfx3dPFjAPjTY/eJ2AjxqHcZeOz4B/cA6Gagz0gOEm/6U92LGXyE/zAAN
a8Q7nzuN9Ijh5q683IipOMB/rx7sU0RgeUTwcTUmaCMrn4Y7hpzW1VrifRv7X0FZ
OtZ7PvkBDzgTMb6BDVb8rBYvnML+UjXxBuOayMDi4iaNxFi3Nh8ylYm9Pcp05tUY
5fAqLvLH4KV8valFaSjE0sL2C+ydu+yXSZgAfRWLMol4swn2RphyExzc3PSY5UKX
cKDer+zAJemyR7GCPhWku5q+z/zvCzUqSDQvvm6/8gFWiC1AcpXHQQ==
=pHk4
-----END PGP SIGNATURE-----

--Signature=_Fri__11_Jun_2004_22_26_25_-0500_QfZ.QGZq9lOnsc=5--