[m-rev.] diff: do not allow unterminated octal escapes
Julien Fischer
jfischer at opturion.com
Mon Dec 15 00:55:06 AEDT 2014
Do not allow unterminated octal escapes in string literals.
library/lexer.m:
Fix an old XXX: do not allow unterminated octal escapes in string
literals; we haven't needed to preserve bug-for-bug compatibility
with NU-Prolog for a very long time.
tests/hard_coded/special_char.m:
Terminate octal escapes.
tests/invalid/Mmakefile:
tests/invalid/unterminated_octal_escape.{m,err_exp}:
Check for an error if an unterminated octal escape is encountered.
Julien.
diff --git a/library/lexer.m b/library/lexer.m
index 1d0bd0c..b7c2a28 100644
--- a/library/lexer.m
+++ b/library/lexer.m
@@ -1431,12 +1431,7 @@ get_octal_escape(Stream, QuoteChar, Chars, OctalChars, Token, !IO) :-
finish_octal_escape(Stream, QuoteChar, Chars,
OctalChars, Token, !IO)
;
- % XXX We don't report this as an error since we need bug-for-bug
- % compatibility with NU-Prolog.
- % Token = error("unterminated octal escape")
- io.putback_char(Stream, Char, !IO),
- finish_octal_escape(Stream, QuoteChar, Chars, OctalChars, Token,
- !IO)
+ Token = error("unterminated octal escape")
)
).
@@ -1454,12 +1449,8 @@ string_get_octal_escape(String, Len, QuoteChar, Chars, OctalChars,
string_finish_octal_escape(String, Len, QuoteChar, Chars,
OctalChars, Posn0, Token, Context, !Posn)
;
- % XXX We don't report this as an error since we need bug-for-bug
- % compatibility with NU-Prolog.
- % Token = error("unterminated octal escape")
- string_ungetchar(String, !Posn),
- string_finish_octal_escape(String, Len, QuoteChar, Chars,
- OctalChars, Posn0, Token, Context, !Posn)
+ string_get_context(Posn0, Context, !Posn),
+ Token = error("unterminated octal escape")
)
;
Token = eof,
diff --git a/tests/hard_coded/special_char.m b/tests/hard_coded/special_char.m
index efb51d3..9676752 100644
--- a/tests/hard_coded/special_char.m
+++ b/tests/hard_coded/special_char.m
@@ -12,7 +12,7 @@ main -->
print("Hello world\r\n"),
print("\r\n"),
print("\a\b\v\f\t\n"),
- print("\077"),
- print("\0123"),
- print("\0321"),
+ print("\077\"),
+ print("\0123\"),
+ print("\0321\"),
print("\n").
diff --git a/tests/invalid/Mmakefile b/tests/invalid/Mmakefile
index 2c7c1e5..086dc46 100644
--- a/tests/invalid/Mmakefile
+++ b/tests/invalid/Mmakefile
@@ -263,6 +263,7 @@ SINGLEMODULE= \
unsatisfiable_constraint_bug \
unsatisfiable_constraint_msg \
unsatisfiable_super \
+ unterminated_octal_escape \
user_eq_dummy \
uu_type \
vars_in_wrong_places \
diff --git a/tests/invalid/unterminated_octal_escape.err_exp b/tests/invalid/unterminated_octal_escape.err_exp
new file mode 100644
index 0000000..67dd16a
--- /dev/null
+++ b/tests/invalid/unterminated_octal_escape.err_exp
@@ -0,0 +1,2 @@
+unterminated_octal_escape.m:009: Error: no clauses for predicate `main'/2.
+unterminated_octal_escape.m:014: Syntax error: unterminated octal escape.
diff --git a/tests/invalid/unterminated_octal_escape.m b/tests/invalid/unterminated_octal_escape.m
new file mode 100644
index 0000000..fbd385b
--- /dev/null
+++ b/tests/invalid/unterminated_octal_escape.m
@@ -0,0 +1,15 @@
+% Older versions of Mercury used to accept unterminated octal escapes
+% in order to be bug-for-bug compatible with NU-Prolog.
+%
+:- module unterminated_octal_escape.
+:- interface.
+
+:- import_module io.
+
+:- pred main(io::di, io::uo) is det.
+
+:- implementation.
+
+main(!IO) :-
+ io.write_string("\43", !IO),
+ io.nl(!IO).
More information about the reviews
mailing list