From d3959ce85c17d31921c0e3143b58734e090002f0 Mon Sep 17 00:00:00 2001 From: Xavier Leroy Date: Sat, 7 Mar 2015 11:49:19 +0100 Subject: Issue #26: problems with big escape sequences in string/char literals. - Error instead of warning if escape sequence overflows one character. - Wrong normalization of L'x' to char instead of wchar_t. - More careful overflow tests. --- cparser/Elab.ml | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) (limited to 'cparser') diff --git a/cparser/Elab.ml b/cparser/Elab.ml index bad92cf6..612103a6 100644 --- a/cparser/Elab.ml +++ b/cparser/Elab.ml @@ -224,16 +224,21 @@ let elab_char_constant loc wide chars = let v = List.fold_left (fun acc d -> - if acc >= max_val then + if acc < 0L || acc >= max_val then error loc "character constant overflows"; - if d >= max_digit then - warning loc "escape sequence is out of range (code 0x%LX)" d; + if d < 0L || d >= max_digit then + error loc "escape sequence is out of range (code 0x%LX)" d; Int64.add (Int64.shift_left acc nbits) d) 0L chars in if not (integer_representable v IInt) then warning loc "character constant cannot be represented at type 'int'"; - (* C99 6.4.4.4 item 10: single character -> represent at type char *) - Ceval.normalize_int v (if List.length chars = 1 then IChar else IInt) + (* C99 6.4.4.4 item 10: single character -> represent at type char + or wchar_t *) + Ceval.normalize_int v + (if List.length chars = 1 then + if wide then wchar_ikind() else IChar + else + IInt) let elab_string_literal loc wide chars = let nbits = if wide then 8 * !config.sizeof_wchar else 8 in @@ -241,14 +246,14 @@ let elab_string_literal loc wide chars = List.iter (fun c -> if c < 0L || c >= char_max - then warning loc "escape sequence is out of range (code 0x%LX)" c) + then error loc "escape sequence is out of range (code 0x%LX)" c) chars; if wide then CWStr chars else begin let res = String.create (List.length chars) in List.iteri - (fun i c -> res.[i] <- Char.chr (Int64.to_int c)) + (fun i c -> res.[i] <- Char.unsafe_chr (Int64.to_int c)) chars; CStr res end -- cgit