Skip to content

Don't silently truncate integer constants #194

@sim642

Description

@sim642

While looking into goblint/analyzer#1794, I was surprised to find that CIL silently truncates too large integer constants on attribute-less types:

int main( void ) {
  volatile int a = 121111111111111; // Goblint sees (int volatile)121111111111111
  int b = 121111111111111; // Goblint just sees 1623298503
  return 0;
}

After lots of digging, this seems to be done here by kintegerCilint:

cil/src/cil.ml

Lines 6100 to 6113 in a35d59e

let mkCastT ~(e: exp) ~(oldt: typ) ~(newt: typ) =
(* Do not remove old casts because they are conversions !!! *)
if Util.equals (typeSig oldt) (typeSig newt) then begin
e
end else begin
(* Watch out for constants *)
match newt, e with
(* Casts to _Bool are special: they behave like "!= 0" ISO C99 6.3.1.2 *)
TInt(IBool, []), Const(CInt(i, _, _)) ->
let v = if compare i zero_cilint = 0 then zero_cilint else one_cilint in
Const (CInt(v, IBool, None))
| TInt(newik, []), Const(CInt(i, _, _)) -> kintegerCilint newik i
| _ -> CastE(newt,e)
end

It seems weird, especially to do it silently while arbitrarily depending on the presence of attributes. It prevents Goblint from soundly reporting overflows on constants.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions