From 8b4c83f527ecc2f88a365b798f4daf9e5baa35a1 Mon Sep 17 00:00:00 2001 From: Stephen Heumann Date: Sat, 5 Nov 2016 00:47:35 -0500 Subject: [PATCH] Give an error when trying to use sizeof on incomplete struct or union types. The following demonstrates cases that would erroneously be allowed (and misleadingly give a size of 0) before: #include struct s *S; int main(void) { printf("%lu %lu\n", sizeof(struct s), sizeof *S); } --- Expression.pas | 9 ++++----- Scanner.pas | 2 +- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/Expression.pas b/Expression.pas index a042d89..984478f 100644 --- a/Expression.pas +++ b/Expression.pas @@ -1199,9 +1199,8 @@ var codeGeneration := lCodeGeneration and (numErrors = 0); op^.token.lval := expressionType^.size; with expressionType^ do - if kind = arrayType then - if (elements = 0) or (size = 0) then - Error(49); + if (size = 0) or ((kind = arrayType) and (elements = 0)) then + Error(49); end; {else} op^.left := nil; end {if sizeofsy} @@ -1607,8 +1606,8 @@ if token.kind in startExpression then begin sp^.token.kind := ulongconst; sp^.token.class := longConstant; sp^.token.lval := typeSpec^.size; - if typeSpec^.kind = arrayType then - if (typeSpec^.elements = 0) or (typeSpec^.size = 0) then + with typeSpec^ do + if (size = 0) or ((kind = arrayType) and (elements = 0)) then Error(49); sp^.next := stack; stack := sp; diff --git a/Scanner.pas b/Scanner.pas index 40e6186..59cceab 100644 --- a/Scanner.pas +++ b/Scanner.pas @@ -529,7 +529,7 @@ if list or (numErr <> 0) then begin 46: msg := @'you must initialize the individual elements of a struct, union, or non-char array'; 47: msg := @'type conflict'; 48: msg := @'pointer initializers must resolve to an integer, address or string'; - 49: msg := @'the array size could not be determined'; + 49: msg := @'the size could not be determined'; 50: msg := @'only parameters or types may be declared here'; 51: msg := @'lint: undefined function'; 52: msg := @'you cannot initialize a type';