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.
This commit is contained in:
Stephen Heumann 2017-10-29 20:21:36 -05:00
parent dddae89af0
commit 8d31481182
2 changed files with 10 additions and 0 deletions

View File

@ -2170,6 +2170,8 @@ procedure ChangePointer (op: pcodes; size: longint; tp: baseTypeEnum);
{ tp - type of the integer operand } { tp - type of the integer operand }
begin {ChangePointer} begin {ChangePointer}
if size = 0 then
Error(122);
case tp of case tp of
cgByte,cgUByte,cgWord,cgUWord: begin cgByte,cgUByte,cgWord,cgUWord: begin
if (size = long(size).lsw) and (op = pc_adl) if (size = long(size).lsw) and (op = pc_adl)
@ -2494,6 +2496,8 @@ var
end {if} end {if}
else {if iType^.kind = pointerType then} begin else {if iType^.kind = pointerType then} begin
lSize := iType^.pType^.size; lSize := iType^.pType^.size;
if lSize = 0 then
Error(122);
if long(lSize).msw <> 0 then begin if long(lSize).msw <> 0 then begin
{handle inc/dec of >64K} {handle inc/dec of >64K}
@ -3359,6 +3363,11 @@ case tree^.token.kind of
if expressionType^.kind in [arrayType,pointerType] then begin if expressionType^.kind in [arrayType,pointerType] then begin
{subtraction of two pointers} {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); Gen0(pc_sbl);
if size <> 1 then begin if size <> 1 then begin
GenLdcLong(size); GenLdcLong(size);

View File

@ -604,6 +604,7 @@ if list or (numErr <> 0) then begin
119: msg := @'inline specifier is only allowed on functions'; 119: msg := @'inline specifier is only allowed on functions';
120: msg := @'non-static inline functions are not supported'; 120: msg := @'non-static inline functions are not supported';
121: msg := @'invalid digit for binary constant'; 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); otherwise: Error(57);
end; {case} end; {case}
writeln(msg^); writeln(msg^);