Skip to content

Commit d3959ce

Browse files
committed
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.
1 parent 01062b4 commit d3959ce

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

cparser/Elab.ml

+12-7
Original file line numberDiff line numberDiff line change
@@ -224,31 +224,36 @@ let elab_char_constant loc wide chars =
224224
let v =
225225
List.fold_left
226226
(fun acc d ->
227-
if acc >= max_val then
227+
if acc < 0L || acc >= max_val then
228228
error loc "character constant overflows";
229-
if d >= max_digit then
230-
warning loc "escape sequence is out of range (code 0x%LX)" d;
229+
if d < 0L || d >= max_digit then
230+
error loc "escape sequence is out of range (code 0x%LX)" d;
231231
Int64.add (Int64.shift_left acc nbits) d)
232232
0L chars in
233233
if not (integer_representable v IInt) then
234234
warning loc "character constant cannot be represented at type 'int'";
235-
(* C99 6.4.4.4 item 10: single character -> represent at type char *)
236-
Ceval.normalize_int v (if List.length chars = 1 then IChar else IInt)
235+
(* C99 6.4.4.4 item 10: single character -> represent at type char
236+
or wchar_t *)
237+
Ceval.normalize_int v
238+
(if List.length chars = 1 then
239+
if wide then wchar_ikind() else IChar
240+
else
241+
IInt)
237242

238243
let elab_string_literal loc wide chars =
239244
let nbits = if wide then 8 * !config.sizeof_wchar else 8 in
240245
let char_max = Int64.shift_left 1L nbits in
241246
List.iter
242247
(fun c ->
243248
if c < 0L || c >= char_max
244-
then warning loc "escape sequence is out of range (code 0x%LX)" c)
249+
then error loc "escape sequence is out of range (code 0x%LX)" c)
245250
chars;
246251
if wide then
247252
CWStr chars
248253
else begin
249254
let res = String.create (List.length chars) in
250255
List.iteri
251-
(fun i c -> res.[i] <- Char.chr (Int64.to_int c))
256+
(fun i c -> res.[i] <- Char.unsafe_chr (Int64.to_int c))
252257
chars;
253258
CStr res
254259
end

0 commit comments

Comments
 (0)