Skip to content

Commit e7b938f

Browse files
bschommerxavierleroy
authored andcommitted
Check ptr arithmetic for ++ and --
Also: improve check for ptr - integer. (Added by Xavier Leroy <xavier.leroy@college-de-france.fr>)
1 parent d9e1175 commit e7b938f

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

cparser/Elab.ml

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1710,11 +1710,12 @@ let elab_expr ctx loc env a =
17101710
let check_ptr_arith env ty s =
17111711
match unroll env ty with
17121712
| TVoid _ ->
1713-
error "illegal arithmetic on a pointer to void in binary '%c'" s
1713+
error "illegal arithmetic on a pointer to void in %s" s
17141714
| TFun _ ->
1715-
error "illegal arithmetic on a pointer to the function type %a in binary '%c'" (print_typ env) ty s
1716-
| _ -> if incomplete_type env ty then
1717-
error "arithmetic on a pointer to an incomplete type %a in binary '%c'" (print_typ env) ty s
1715+
error "illegal arithmetic on a pointer to the function type %a in %s" (print_typ env) ty s
1716+
| _ ->
1717+
if incomplete_type env ty then
1718+
error "arithmetic on a pointer to an incomplete type %a in %s" (print_typ env) ty s
17181719
in
17191720

17201721
let check_static_var env id sto ty =
@@ -2120,7 +2121,7 @@ let elab_expr ctx loc env a =
21202121
| _, _ -> fatal_error "invalid operands to binary '+' (%a and %a)"
21212122
(print_typ env) b1.etyp (print_typ env) b2.etyp
21222123
in
2123-
check_ptr_arith env ty '+';
2124+
check_ptr_arith env ty "binary '+'";
21242125
TPtr(ty, [])
21252126
end in
21262127
{ edesc = EBinop(Oadd, b1, b2, tyres); etyp = tyres },env
@@ -2135,20 +2136,20 @@ let elab_expr ctx loc env a =
21352136
end else begin
21362137
match wrap unroll loc env b1.etyp, wrap unroll loc env b2.etyp with
21372138
| (TPtr(ty, a) | TArray(ty, _, a)), (TInt _ | TEnum _) ->
2138-
if not (wrap pointer_arithmetic_ok loc env ty) then
2139-
error "illegal pointer arithmetic in binary '-'";
2139+
check_ptr_arith env ty "binary '-'";
21402140
(TPtr(ty, []), TPtr(ty, []))
21412141
| (TPtr(ty1, a1) | TArray(ty1, _, a1)),
21422142
(TPtr(ty2, a2) | TArray(ty2, _, a2)) ->
21432143
if not (compatible_types AttrIgnoreAll env ty1 ty2) then
21442144
error "%a and %a are not pointers to compatible types"
21452145
(print_typ env) b1.etyp (print_typ env) b1.etyp;
2146-
check_ptr_arith env ty1 '-';
2147-
check_ptr_arith env ty2 '-';
2146+
check_ptr_arith env ty1 "binary '-'";
2147+
check_ptr_arith env ty2 "binary '-'";
21482148
if wrap sizeof loc env ty1 = Some 0 then
21492149
error "subtraction between two pointers to zero-sized objects";
21502150
(TPtr(ty1, []), TInt(ptrdiff_t_ikind(), []))
2151-
| _, _ -> fatal_error "invalid operands to binary '-' (%a and %a)"
2151+
| _, _ ->
2152+
fatal_error "invalid operands to binary '-' (%a and %a)"
21522153
(print_typ env) b1.etyp (print_typ env) b2.etyp
21532154
end in
21542155
{ edesc = EBinop(Osub, b1, b2, tyop); etyp = tyres },env
@@ -2306,6 +2307,11 @@ let elab_expr ctx loc env a =
23062307
error "expression is not assignable";
23072308
if not (is_scalar_type env b1.etyp) then
23082309
error "cannot %s value of type %a" msg (print_typ env) b1.etyp;
2310+
begin match unroll env b1.etyp with
2311+
| TPtr (ty, _) | TArray (ty, _ , _) ->
2312+
check_ptr_arith env ty ("unary " ^ msg)
2313+
| _ -> ()
2314+
end;
23092315
{ edesc = EUnop(op, b1); etyp = b1.etyp },env
23102316

23112317
(* Elaboration of binary operators over integers *)

0 commit comments

Comments
 (0)