0

Coverity detects an undefined identifier while trying to calculate offset of a member in the structure.

typedef struct A
{
    uint8_t mem[10];
} A;

size_t offset = offsetof(A, mem); // This line raises the error.

The exact coverity description is "identifier mem is undefined".

I've tried replacing mem with mem[0], mem[], removed typedef from struct A - but nothing seems to solve it. It'll be great if someone has pointers to what could be upsetting coverity.

[Update#1] : I'm using ubuntu 16.04 (kernel - 4.4.0-38-generic) , gcc/g++ 5.4.0, coverity 7.5.1

[Update#2]: A standalone test.c with this structure and following command line options keeps coverity happy:

cov-build --dir . cc -c test.c -o test -Werror -march=corei7 -g -fstrict-aliasing -fstack-protector -fstrict-overflow -Wall -Wextra -Wshadow -Wmissing-prototypes -Wstrict-prototypes -O3 -fomit-frame-pointer -fPIE.

Interestingly, when I compile the whole project, it leads to a bunch of errors for the same lines : UNDEFINED_IDENTIFIER, ROUTINE_NOT_EMITTED, TYPE_IDENTIFIER_NOT_ALLOWED.

0sn1s
  • 1
  • 1

1 Answers1

0

Using Coverity 8.0.0.9 and 8.5.0.5 and GCC 4.6.3 and 5.2.0, I'm unable to reproduce this issue. My reproducer is:

#include <stdint.h>
#include <stddef.h>

typedef struct A
{
  uint8_t mem[10];
} A;

size_t offset = offsetof(A, mem); // This line raises the error.

since yours didn't outright compile with GCC. I used gcc -c test.c as my command line.

Since offsetof is a macro defined by the system headers, it seems most likely that whatever your macro expands to is what's giving Coverity difficulty (and it's not the same as what mine expands to) - or something on your command line is putting Coverity into a mode that makes it reject this. A good next step would be to preprocess your source file to see what offsetof expands to and post the preprocessed snippet here, along with the command line of your compiler.

Caleb
  • 1,143
  • 5
  • 13
  • You're right. A standalone test file keeps Coverity happy. So its not the macro definition. I've updated my original post with the complete command line options. – 0sn1s Oct 31 '16 at 20:44
  • Could this be a namespace or conditional compilation issue? Given that a standalone test case works and Coverity is complaining about not being able to find the identifier `mem`, this suggests that it's not seeing that symbol. Without a reproducer for the issue, I can only speculate, but the most likely culprit is that there's a macro definition that your compiler has that Coverity doesn't. Is the struct wrapped in an #if block or a namespace? – Caleb Nov 01 '16 at 18:00
  • Even I've that feeling. Thanks for the pointer about #ifdef blocks. Let me check it. – 0sn1s Nov 02 '16 at 17:23