From fbc778079d50a4af45b9a648eab56cef29ac75f4 Mon Sep 17 00:00:00 2001 From: Bernhard Schommer Date: Tue, 27 Mar 2018 17:15:42 +0200 Subject: Sizeof and _Alignof are not allowed on bit-fields (#67) Sizeof and _Alignof are not allowed on bit-fields Sizeof and _Alignof are not allowed to be applied to a expression that designates a bit-field member. Bug 23311 --- cparser/Cutil.ml | 10 ++++++++++ cparser/Cutil.mli | 2 ++ cparser/Elab.ml | 6 +++++- 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/cparser/Cutil.ml b/cparser/Cutil.ml index 30037049..4a5c9427 100644 --- a/cparser/Cutil.ml +++ b/cparser/Cutil.ml @@ -988,6 +988,16 @@ let is_call_to_fun e s = | EVar id -> id.C.name = s | _ -> false +let is_bitfield env e = + match e.edesc with + | EUnop(Odot f,b) -> + let fld = field_of_dot_access env b.etyp f in + fld.fld_bitfield <> None + | EUnop(Oarrow f,b) -> + let fld = field_of_arrow_access env b.etyp f in + fld.fld_bitfield <> None + | _ -> false + (* Assignment compatibility check over attributes. Standard attributes ("const", "volatile", "restrict") can safely be added (to the rhs type to get the lhs type) but must not be dropped. diff --git a/cparser/Cutil.mli b/cparser/Cutil.mli index 87a69f12..fb875b0d 100644 --- a/cparser/Cutil.mli +++ b/cparser/Cutil.mli @@ -243,6 +243,8 @@ val valid_array_size: Env.t -> typ -> int64 -> bool (* Test whether the array size fits in half of the address space *) val is_volatile_variable: Env.t -> exp -> bool (* Test whether the expression is an access to a volatile variable *) +val is_bitfield: Env.t -> exp -> bool + (* Test whether the expression is a bit-field *) (* Constructors *) diff --git a/cparser/Elab.ml b/cparser/Elab.ml index 3ef489c1..204bc515 100644 --- a/cparser/Elab.ml +++ b/cparser/Elab.ml @@ -1619,6 +1619,8 @@ let elab_expr vararg loc env a = let b1,env = elab env a1 in if wrap incomplete_type loc env b1.etyp then error "invalid application of 'sizeof' to an incomplete type %a" (print_typ env) b1.etyp; + if wrap is_bitfield loc env b1 then + error "invalid application of 'sizeof' to a bit-field"; let bdesc = (* Catch special cases sizeof("string literal") *) match b1.edesc with @@ -1641,7 +1643,9 @@ let elab_expr vararg loc env a = | EXPR_ALIGNOF a1 -> let b1,env = elab env a1 in if wrap incomplete_type loc env b1.etyp then - error "invalid application of 'alignof' to an incomplete type %a" (print_typ env) b1.etyp; + error "invalid application of '_Alignof' to an incomplete type %a" (print_typ env) b1.etyp; + if wrap is_bitfield loc env b1 then + error "invalid application of '_Alignof' to a bit-field"; { edesc = EAlignof b1.etyp; etyp = TInt(size_t_ikind(), []) },env | TYPE_ALIGNOF (spec, dcl) -> -- cgit