2

I am trying to analyze the following C program:

#include <stdio.h>

typedef struct a {
    int x;
    char y;
} alias;

int main()
{
    typedef struct b {
         int x;
         int y;
    } alias;

    alias *var = (unsigned long*) 0x12345678;
    var->y = 0x00;

    return 0;
}

As typedef is redefined in function "main", I followed the user manual of Frama-C and used the option -c11.

-c11 allows the use of some C11 constructs. Currently supported are typedefs redefinition

However, I got the following error:

redefinition of a typedef in a non-global scope is currently unsupported

Could you please help me explain this case? Note that I don't have this problem if I use v12.x - Magnesium.

Thuy Nguyen
  • 353
  • 2
  • 10
  • The two typedefs in main are undefined after the if/else and can’t be used in the rest of main. – Jonathan Leffler Feb 05 '20 at 05:53
  • @JonathanLeffler: Could you explain why two typedefs in main are undefined after the if/else? – Thuy Nguyen Feb 05 '20 at 06:05
  • Their scope is limited by the (explicit) braces. They are only valid in that scope. The braces are probably necessary; you can’t put a definition after `if` or `else` because that isn’t a statement, but the braces make a compound statement. – Jonathan Leffler Feb 05 '20 at 06:18
  • And, JFTR, any C compiler should behave the same. This isn’t unique to the Fran C compiler. – Jonathan Leffler Feb 05 '20 at 06:19
  • @JonathanLeffler: Thanks, I got it. You are right that two typedefs in main are undefined after the if/else. However, my main wonder is that Frama-C raises a warning when I redefine typedef in a non-global scope. I modified the example code to make the question more concise and accurate. – Thuy Nguyen Feb 05 '20 at 07:24
  • 1
    Note that the C11 change is that you could write `typedef struct a { int x; char y; } alias;` (the global scope definition) a second time at global scope, or inside a function, and it would be the same type. In C99 or earlier, you could not write the definition a second time at global scope, and inside a function, it would define a new type, not the same type. – Jonathan Leffler Feb 05 '20 at 19:10

1 Answers1

2

It seems frama-c does not support redefining a typedef symbol in a local scope.

The C Standard allows this, and it might be useful to support automatically generated code, but doing this on purpose seems a good way to create confusion for readers and maintainers of the code.

chqrlie
  • 131,814
  • 10
  • 121
  • 189