summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorotto <otto@openbsd.org>2003-09-30 18:46:11 +0000
committerotto <otto@openbsd.org>2003-09-30 18:46:11 +0000
commit6b960c42588505cff54349b3976b34d7a04db5bc (patch)
tree0af75de49a5a7618378730724118fc556ccdd715
parentTeach dc(1) how to read strings with unbalanced braces by introducing (diff)
downloadwireguard-openbsd-6b960c42588505cff54349b3976b34d7a04db5bc.tar.xz
wireguard-openbsd-6b960c42588505cff54349b3976b34d7a04db5bc.zip
Make sure strings and error messages sent to dc(1) properly escape [, ] and \.
-rw-r--r--usr.bin/bc/bc.y25
-rw-r--r--usr.bin/bc/scan.l9
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");