TrustInSoft / tis-interpreter

An interpreter for finding subtle bugs in programs written in standard C
565 stars 28 forks source link

Wrong error, Valid C rejected: struct with flexible array member considered incomplete type #118

Open ch3root opened 8 years ago

ch3root commented 8 years ago

Source code:

int main()
{
  struct s1 {
    int i;
    int a[];
  };

  struct s2 {
    struct s1 s;
  };
}

tis-interpreter (21f4c7a763b4601d723ea5749185c97115c9c98a) output:

test.c:8:[kernel] user error: field s is declared with incomplete type struct s1
[kernel] user error: stopping on file "test.c" that has errors. Add '-kernel-msg-key pp'
                     for preprocessing command.
[kernel] Frama-C aborted: invalid user input.

The error is right but the message is wrong. struct s1 is not an incomplete type (and tis-interpreter, for example, doesn't complain about applying sizeof to it). Flexible array members are a special case.

gcc (GCC) 7.0.0 20160627 (experimental):

$ gcc -std=c11 -pedantic -Wall -Wextra -O3 -fsanitize=undefined test.c && ./a.out
test.c: In function ‘main’:
test.c:9:15: warning: invalid use of structure with flexible array member [-Wpedantic]
     struct s1 s;
               ^

clang version 3.9.0 (trunk 271312):

$ clang -std=c11 -Weverything -Wno-padded -O3 -fsanitize=undefined test.c && ./a.out
test.c:9:15: warning: 's' may not be nested in a struct due to flexible array member [-Wflexible-array-extensions]
    struct s1 s;
              ^
1 warning generated.
ch3root commented 8 years ago

Structs with flexible array members are permitted in unions. Probably tis-interpreter rejects it for same reason -- considering such a struct an incomplete type.

Source code:

int main()
{
  struct s {
    int i;
    int a[];
  };

  union u {
    struct s s;
  };
}

tis-interpreter (21f4c7a763b4601d723ea5749185c97115c9c98a) output:

test.c:8:[kernel] user error: field s is declared with incomplete type struct s
[kernel] user error: stopping on file "test.c" that has errors. Add '-kernel-msg-key pp'
                     for preprocessing command.
[kernel] Frama-C aborted: invalid user input.

gcc (GCC) 7.0.0 20160627 (experimental):

$ gcc -std=c11 -pedantic -Wall -Wextra -O3 -fsanitize=undefined test.c && ./a.out

clang version 3.9.0 (trunk 271312):

$ clang -std=c11 -Weverything -O3 -fsanitize=undefined test.c && ./a.out