From 8d314811821d48f9c3b795afe21c6b9e83f3f442 Mon Sep 17 00:00:00 2001 From: Stephen Heumann Date: Sun, 29 Oct 2017 20:21:36 -0500 Subject: [PATCH] Report errors for illegal pointer arithmetic operations. These include arithmetic on pointers to incomplete types or functions, as well as subtraction of pointers to incompatible types. --- Expression.pas | 9 +++++++++ Scanner.pas | 1 + 2 files changed, 10 insertions(+) diff --git a/Expression.pas b/Expression.pas index 1a11bcf..58e8de2 100644 --- a/Expression.pas +++ b/Expression.pas @@ -2170,6 +2170,8 @@ procedure ChangePointer (op: pcodes; size: longint; tp: baseTypeEnum); { tp - type of the integer operand } begin {ChangePointer} +if size = 0 then + Error(122); case tp of cgByte,cgUByte,cgWord,cgUWord: begin if (size = long(size).lsw) and (op = pc_adl) @@ -2494,6 +2496,8 @@ var end {if} else {if iType^.kind = pointerType then} begin lSize := iType^.pType^.size; + if lSize = 0 then + Error(122); if long(lSize).msw <> 0 then begin {handle inc/dec of >64K} @@ -3359,6 +3363,11 @@ case tree^.token.kind of if expressionType^.kind in [arrayType,pointerType] then begin {subtraction of two pointers} + if size = 0 then + Error(122) + {NOTE: assumes aType & pType overlap in typeRecord} + else if not CompTypes(lType^.aType, expressionType^.aType) then + Error(47); Gen0(pc_sbl); if size <> 1 then begin GenLdcLong(size); diff --git a/Scanner.pas b/Scanner.pas index 7843788..86d51bb 100644 --- a/Scanner.pas +++ b/Scanner.pas @@ -604,6 +604,7 @@ if list or (numErr <> 0) then begin 119: msg := @'inline specifier is only allowed on functions'; 120: msg := @'non-static inline functions are not supported'; 121: msg := @'invalid digit for binary constant'; + 122: msg := @'arithmetic is not allowed on a pointer to an incomplete or function type'; otherwise: Error(57); end; {case} writeln(msg^);