diff --git a/src/kernel_internals/typing/cabs2cil.ml b/src/kernel_internals/typing/cabs2cil.ml index ffa5db94b5b67ca0be844c766a47d4b75a637b39..385f2198dc87e4b379def5051dd28664120ab273 100644 --- a/src/kernel_internals/typing/cabs2cil.ml +++ b/src/kernel_internals/typing/cabs2cil.ml @@ -3360,7 +3360,8 @@ let rec collectInitializer (Integer.to_int ni), false | _ -> Kernel.fatal ~current:true - "Array length is not a constant expression %a" + "Array length %a is not a compile-time constant: \ + no explicit initializer allowed." Cil_printer.pp_exp len end | _ -> @@ -3678,7 +3679,8 @@ let integerArrayLength (leno: exp option) : int = try lenOfArray leno with LenOfArray -> Kernel.fatal ~current:true - "Initializing non-constant-length array with length=%a" + "Array length %a is not a compile-time constant: \ + no explicit initializer allowed." Cil_printer.pp_exp len let find_field_offset cond (fidlist: fieldinfo list) : offset = @@ -5067,28 +5069,27 @@ and doType (ghost:bool) isFuncArg Kernel.error ~once:true ~current:true "Array length %a does not have an integral type." Cil_printer.pp_exp len'; - if not allowVarSizeArrays then begin - (* Assert that len' is a constant *) - let cst = constFold true len' in - (match cst.enode with - | Const(CInt64(i, _, _)) -> - if Integer.lt i Integer.zero then - Kernel.error ~once:true ~current:true - "Length of array is negative" - | _ -> - if isConstant cst then - (* e.g., there may be a float constant involved. - * We'll leave it to the user to ensure the length is - * non-negative, etc.*) - Kernel.warning ~once:true ~current:true - "Unable to do constant-folding on array length %a. \ - Some CIL operations on this array may fail." - Cil_printer.pp_exp cst - else - Kernel.error ~once:true ~current:true - "Length of array is not a constant: %a" - Cil_printer.pp_exp cst) - end; + (* Check that len' is admissible *) + let cst = constFold true len' in + (match cst.enode with + | Const(CInt64(i, _, _)) -> + if Integer.lt i Integer.zero then + Kernel.error ~once:true ~current:true + "Array length is negative." + | _ when not allowVarSizeArrays -> + if isConstant cst then + (* e.g., there may be a float constant involved. + * We'll leave it to the user to ensure the length is + * non-negative, etc.*) + Kernel.warning ~once:true ~current:true + "Unable to do constant-folding on array length %a. \ + Some CIL operations on this array may fail." + Cil_printer.pp_exp cst + else + Kernel.error ~once:true ~current:true + "Array length %a is not a compile-time constant." + Cil_printer.pp_exp cst + | _ -> ()); if Cil.isZero len' && not allowZeroSizeArrays && not (Cil.gccMode () || Cil.msvcMode ()) then diff --git a/tests/syntax/array_size.i b/tests/syntax/array_size.i new file mode 100644 index 0000000000000000000000000000000000000000..11f210ed5687266e77d9d1496047271aa504a1cb --- /dev/null +++ b/tests/syntax/array_size.i @@ -0,0 +1 @@ +int f(int t[-1]) { return t[0]; } // should raise an error diff --git a/tests/syntax/oracle/array_size.res.oracle b/tests/syntax/oracle/array_size.res.oracle new file mode 100644 index 0000000000000000000000000000000000000000..ec2486e5181ba4a029dc69d0d451180ae504612b --- /dev/null +++ b/tests/syntax/oracle/array_size.res.oracle @@ -0,0 +1,4 @@ +[kernel] Parsing tests/syntax/array_size.i (no preprocessing) +[kernel] tests/syntax/array_size.i:1: User Error: Array length is negative. +[kernel] User Error: stopping on file "tests/syntax/array_size.i" that has errors. +[kernel] Frama-C aborted: invalid user input.