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 <stdio.h>
struct s *S;
int main(void)
{
        printf("%lu %lu\n", sizeof(struct s), sizeof *S);
}
This commit is contained in:
Stephen Heumann 2016-11-05 00:47:35 -05:00
parent 5950002d03
commit 8b4c83f527
2 changed files with 5 additions and 6 deletions

View File

@ -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;

View File

@ -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';