1

Consider the following code:

typedef struct _sMYSTRUCT_BASE
{
    int b_a;
    int b_b;
    int b_c;
} sMYSTRUCT_BASE;

typedef struct _sMYSTRUCT
{
    sMYSTRUCT_BASE base;
    int            a;
    int            b;
} sMYSTRUCT;

Private const sMYSTRUCT mystruct_init =
{
    0,
    1,
    3,
    4
};

I am looking for a way to generate an error (compile-, or runtime) to indicated that the structure initialization hasn't explicitly 'touched' all structure members. There are 5 integers in the structure, but 'mystruct_init' only have 4 values. I know that last member (mystruct_init.b) will be zero, but I need some kind of warning/error to inform the programmer about the mistake. This has to work on a very old compiler (maybe not even ansi-c compliant).

user2448122
  • 195
  • 12

1 Answers1

1

Modern compilers are capable of producing such a warning...in gcc, it's turned on with -Wmissing-field-initializers (which warns about initializers that exist but do not initialize all members, but not about structs with no initializer expression; these can at least sometimes be caught by turning on -Wuninitialized, which will warn you if it sees you reading a potentially uninitialized value, at least if you read it in the same function the variable was declared in).

If your very old compiler happens to supply such a warning, you could of course just turn it on, but that seems unlikely from your description.

Your best option, I think, if you want to do an exhaustive search for them, would be to see whether you can get the code to compile with some version of gcc -- it wouldn't have to compile well enough to actually run on your target platform in order to get the warnings. I can't guarantee that it will be able to compile your pre-ANSI C code, particularly if it widely uses compiler-specific extensions, but I can at least say that support for the legacy K&R syntax is still present in the modern C standard, so I wouldn't be surprised if your code compiles better than you might think.

If that works, then to consistently produce the warnings in your IDE, you could modify the build script so that it both compiles and links the code with the real compiler you're targeting, and also compiles it (but not necessarily links it) with gcc, just to generate additional warnings that can be picked up and displayed by the IDE.

The other option would be to see if you can find a compatible static analyzer that can perform such a check; I work on a tool called EnSoft Atlas that builds a data-flow graph which, together with a simple script, could be used to enforce initialization more thoroughly than the gcc warnings allow, by checking whether flow of uninitialized values to fields of structs occurs.

However, our support for C is still in beta. Atlas requires that Eclipse CDT (or JDT for Java) be able to parse your code, and the current C beta only fully supports modern strongly-typed struct initializers (i.e. struct foo f = (struct foo) {...} has fully connected data-flow, but support for the older initializer list syntax struct foo f = {...} was not implemented in our first pass), so I'm not sure it would be able to meet your needs at this time.

Theodore Murdock
  • 1,538
  • 1
  • 13
  • 28
  • Thanks for the detailed reply. I am playing around with [-Wmissing-field-initializers] gcc options (as it is possible to compile to code with gcc), but it is giving me some false positive warnings. See [link](http://stackoverflow.com/questions/22194935/wmissing-field-initializer-when-using-designated-initializers). -Wuninitialized is not an option, as these initializations are placed on the top of the sources (global scope). I will look into the Atlas tool (we use Eclipse CDT to develop), as it might be interesting... – user2448122 Aug 17 '15 at 09:29
  • I took a closer look at our tool's current status for struct initializers, and it's a little less complete than I thought at the moment. The default value nodes that I thought would be present for missing fields are not yet implemented. I'll update this when they've been added. – Theodore Murdock Aug 18 '15 at 21:44
  • It is already possible to recognize missing initializers using our tool, though...a GlobalVariable node that is tagged "##tentative-definitions-only" was defined (without `extern`) but never initialized, while a local (stack) variable that was not initialized has an uninitialized value flowing to it. The TypeOf edges can be used to recognize structs or instances of a particular struct as needed. – Theodore Murdock Aug 18 '15 at 21:46