diff options
author | Otto Moerbeek <otto@cvs.openbsd.org> | 2003-09-30 18:46:12 +0000 |
---|---|---|
committer | Otto Moerbeek <otto@cvs.openbsd.org> | 2003-09-30 18:46:12 +0000 |
commit | cdb8f537d1a9ab237103625caca94b388292adb2 (patch) | |
tree | 3fa894cf1f759c0e383184e293a435e26a15d402 /usr.bin/bc | |
parent | e6543e9ee25b6a30182de6cc78d812f81727b1a9 (diff) |
Make sure strings and error messages sent to dc(1) properly escape [, ] and \.
Diffstat (limited to 'usr.bin/bc')
-rw-r--r-- | usr.bin/bc/bc.y | 25 | ||||
-rw-r--r-- | usr.bin/bc/scan.l | 9 |
2 files changed, 25 insertions, 9 deletions
diff --git a/usr.bin/bc/bc.y b/usr.bin/bc/bc.y index d10b9962ca6..25a40adf9eb 100644 --- a/usr.bin/bc/bc.y +++ b/usr.bin/bc/bc.y @@ -1,5 +1,5 @@ %{ -/* $OpenBSD: bc.y,v 1.9 2003/09/29 03:24:27 otto Exp $ */ +/* $OpenBSD: bc.y,v 1.10 2003/09/30 18:46:11 otto Exp $ */ /* * Copyright (c) 2003, Otto Moerbeek <otto@drijf.net> @@ -31,7 +31,7 @@ */ #ifndef lint -static const char rcsid[] = "$OpenBSD: bc.y,v 1.9 2003/09/29 03:24:27 otto Exp $"; +static const char rcsid[] = "$OpenBSD: bc.y,v 1.10 2003/09/30 18:46:11 otto Exp $"; #endif /* not lint */ #include <ctype.h> @@ -717,12 +717,25 @@ yywrap(void) void yyerror(char *s) { - if (isspace(*yytext) || !isprint(*yytext)) - printf("c[%s: %s:%d: %s: ascii char 0x%x unexpected]pc\n", - __progname, filename, lineno, s, *yytext); + char *str, *p; + + if (isspace(yytext[0]) || !isprint(yytext[0])) + asprintf(&str, "%s: %s:%d: %s: ascii char 0x%x unexpected", + __progname, filename, lineno, s, yytext[0]); else - printf("c[%s: %s:%d: %s: %s unexpected]pc\n", + asprintf(&str, "%s: %s:%d: %s: %s unexpected", __progname, filename, lineno, s, yytext); + if (str == NULL) + err(1, "cannot allocate string"); + + fputs("c[", stdout); + for (p = str; *p != '\0'; p++) { + if (*p == '[' || *p == ']' || *p =='\\') + putchar('\\'); + putchar(*p); + } + fputs("]pc\n", stdout); + free(str); } void diff --git a/usr.bin/bc/scan.l b/usr.bin/bc/scan.l index 5add37f6d52..a8b0179a302 100644 --- a/usr.bin/bc/scan.l +++ b/usr.bin/bc/scan.l @@ -1,5 +1,5 @@ %{ -/* $OpenBSD: scan.l,v 1.6 2003/09/29 03:24:27 otto Exp $ */ +/* $OpenBSD: scan.l,v 1.7 2003/09/30 18:46:11 otto Exp $ */ /* * Copyright (c) 2003, Otto Moerbeek <otto@drijf.net> @@ -18,7 +18,7 @@ */ #ifndef lint -static const char rcsid[] = "$OpenBSD: scan.l,v 1.6 2003/09/29 03:24:27 otto Exp $"; +static const char rcsid[] = "$OpenBSD: scan.l,v 1.7 2003/09/30 18:46:11 otto Exp $"; #endif /* not lint */ #include <err.h> @@ -55,7 +55,10 @@ DIGIT [0-9A-F] \" BEGIN(string); init_strbuf(); <string>{ - [^"\n]+ add_str(yytext); + [^"\n\\\[\]]+ add_str(yytext); + \[ add_str("\\["); + \] add_str("\\]"); + \\ add_str("\\\\"); \n add_str("\n"); lineno++; \" BEGIN(INITIAL); yylval.str = strbuf; return STRING; <<EOF>> fatal("end of file in string"); |