Do not allow NUL bytes in string literals or character constants.

This commit is contained in:
Paul Eggert
2003-10-07 07:32:57 +00:00
parent 91d2c560a8
commit 92ac370570
3 changed files with 17 additions and 1 deletions

View File

@@ -1,3 +1,10 @@
2003-10-07 Paul Eggert <eggert@twinsun.com>
* doc/bison.texinfo (Symbols): NUL bytes are not allowed in string
literals, unfortunately.
* src/scan-gram.l (<SC_ESCAPED_STRING,SC_ESCAPED_CHARACTER>):
Complain about NUL bytes in character constants or string literals.
2003-10-05 Paul Eggert <eggert@twinsun.com> 2003-10-05 Paul Eggert <eggert@twinsun.com>
* NEWS: Don't document %no-default-prec, as it's still * NEWS: Don't document %no-default-prec, as it's still

View File

@@ -2481,7 +2481,8 @@ does not enforce this convention, but if you depart from it, people who
read your program will be confused. read your program will be confused.
All the escape sequences used in string literals in C can be used in All the escape sequences used in string literals in C can be used in
Bison as well. However, unlike Standard C, trigraphs have no special Bison as well, except that you must not use a null character within a
string literal. Also, unlike Standard C, trigraphs have no special
meaning in Bison string literals, nor is backslash-newline allowed. A meaning in Bison string literals, nor is backslash-newline allowed. A
literal string token must contain two or more characters; for a token literal string token must contain two or more characters; for a token
containing just one character, use a character token (see above). containing just one character, use a character token (see above).

View File

@@ -371,6 +371,7 @@ splice (\\[ \f\t\v]*\n)*
return STRING; return STRING;
} }
\0 complain_at (*loc, _("invalid null character"));
.|\n STRING_GROW; .|\n STRING_GROW;
<<EOF>> unexpected_eof (token_start, "\""); BEGIN INITIAL; <<EOF>> unexpected_eof (token_start, "\""); BEGIN INITIAL;
} }
@@ -397,6 +398,7 @@ splice (\\[ \f\t\v]*\n)*
return ID; return ID;
} }
\0 complain_at (*loc, _("invalid null character"));
.|\n STRING_GROW; .|\n STRING_GROW;
<<EOF>> unexpected_eof (token_start, "'"); BEGIN INITIAL; <<EOF>> unexpected_eof (token_start, "'"); BEGIN INITIAL;
} }
@@ -412,6 +414,8 @@ splice (\\[ \f\t\v]*\n)*
unsigned long c = strtoul (yytext + 1, 0, 8); unsigned long c = strtoul (yytext + 1, 0, 8);
if (UCHAR_MAX < c) if (UCHAR_MAX < c)
complain_at (*loc, _("invalid escape sequence: %s"), quote (yytext)); complain_at (*loc, _("invalid escape sequence: %s"), quote (yytext));
else if (! c)
complain_at (*loc, _("invalid null character: %s"), quote (yytext));
else else
obstack_1grow (&obstack_for_string, c); obstack_1grow (&obstack_for_string, c);
} }
@@ -422,6 +426,8 @@ splice (\\[ \f\t\v]*\n)*
c = strtoul (yytext + 2, 0, 16); c = strtoul (yytext + 2, 0, 16);
if (UCHAR_MAX < c || get_errno ()) if (UCHAR_MAX < c || get_errno ())
complain_at (*loc, _("invalid escape sequence: %s"), quote (yytext)); complain_at (*loc, _("invalid escape sequence: %s"), quote (yytext));
else if (! c)
complain_at (*loc, _("invalid null character: %s"), quote (yytext));
else else
obstack_1grow (&obstack_for_string, c); obstack_1grow (&obstack_for_string, c);
} }
@@ -441,6 +447,8 @@ splice (\\[ \f\t\v]*\n)*
int c = convert_ucn_to_byte (yytext); int c = convert_ucn_to_byte (yytext);
if (c < 0) if (c < 0)
complain_at (*loc, _("invalid escape sequence: %s"), quote (yytext)); complain_at (*loc, _("invalid escape sequence: %s"), quote (yytext));
else if (! c)
complain_at (*loc, _("invalid null character: %s"), quote (yytext));
else else
obstack_1grow (&obstack_for_string, c); obstack_1grow (&obstack_for_string, c);
} }