aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorXavier Leroy <xavierleroy@users.noreply.github.com>2018-04-06 15:56:05 +0200
committerGitHub <noreply@github.com>2018-04-06 15:56:05 +0200
commite226562cf531906ecd6510c4767316b07ef451f6 (patch)
tree6343e470c1bd7adf43279667bde29bedb640f2ac
parentdf7b43a096e9a274b5b3ba8ca85275c637557c12 (diff)
downloadcompcert-e226562cf531906ecd6510c4767316b07ef451f6.tar.gz
compcert-e226562cf531906ecd6510c4767316b07ef451f6.zip
Reject illegal initializations of aggregates at top-level (#79)
Examples such as the following were accepted but are invalid ISO C: char c[4] = 42; struct S { int x, y; } = 42; This commit rejects such initializations at top-level. Bug 23372
-rw-r--r--cparser/Elab.ml11
1 files changed, 10 insertions, 1 deletions
diff --git a/cparser/Elab.ml b/cparser/Elab.ml
index ccabc3aa..1712b55e 100644
--- a/cparser/Elab.ml
+++ b/cparser/Elab.ml
@@ -1096,6 +1096,9 @@ module I = struct
(* Change the initializer for the current point *)
let set (z, i) i' = (z, i')
+ (* Is the current point at top? *)
+ let at_top = function Ztop(_, _), _ -> true | _, _ -> false
+
(* Put the current point back to the top *)
let rec to_top = function
| Ztop(name, ty), i as zi -> zi
@@ -1381,7 +1384,13 @@ and elab_single zi a il =
from the expression: do as above *)
elab_list (I.set zi (Init_single a)) il false
| TStruct _ | TUnion _ | TArray _ ->
- (* This is an aggregate: we need to drill into it, recursively *)
+ (* This is an aggregate.
+ At top, explicit { } are required. *)
+ if I.at_top zi then begin
+ error loc "invalid initializer, an initializer list was expected";
+ raise Exit
+ end;
+ (* Otherwise we need to drill into the aggregate, recursively *)
begin match I.first env zi with
| I.OK zi' ->
elab_single zi' a il