diff options
author | Otto Moerbeek <otto@cvs.openbsd.org> | 2004-07-09 19:39:41 +0000 |
---|---|---|
committer | Otto Moerbeek <otto@cvs.openbsd.org> | 2004-07-09 19:39:41 +0000 |
commit | 19d9bccee669af1b3d9e02e09bb1cb1287c29fda (patch) | |
tree | c2c667c15ec1efb10ce69e457ffeba7d15e64d61 | |
parent | 3f81fbd840a9ad7851b5a093f30f5116b4bfb061 (diff) |
From the other BSDs: fix a bug that made sed(1) fail if the last
character of the line buffer was a backslash.
ok deraadt@
-rw-r--r-- | usr.bin/sed/compile.c | 31 |
1 files changed, 26 insertions, 5 deletions
diff --git a/usr.bin/sed/compile.c b/usr.bin/sed/compile.c index 7e334585906..f32c6daddb9 100644 --- a/usr.bin/sed/compile.c +++ b/usr.bin/sed/compile.c @@ -1,4 +1,4 @@ -/* $OpenBSD: compile.c,v 1.18 2004/06/13 17:11:17 deraadt Exp $ */ +/* $OpenBSD: compile.c,v 1.19 2004/07/09 19:39:40 otto Exp $ */ /*- * Copyright (c) 1992 Diomidis Spinellis. @@ -35,7 +35,7 @@ #ifndef lint /* from: static char sccsid[] = "@(#)compile.c 8.2 (Berkeley) 4/28/95"; */ -static char *rcsid = "$OpenBSD: compile.c,v 1.18 2004/06/13 17:11:17 deraadt Exp $"; +static char *rcsid = "$OpenBSD: compile.c,v 1.19 2004/07/09 19:39:40 otto Exp $"; #endif /* not lint */ #include <sys/types.h> @@ -440,6 +440,7 @@ compile_subst(char *p, struct s_subst *s) 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') @@ -453,9 +454,29 @@ compile_subst(char *p, struct s_subst *s) 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 && |