chuck258
chuck258

Reputation: 922

struct member may be uninitialised

I'm currently writing a WebSocket Daemon for the ESP8266. I'm currently struggling with an error I can not explain. I defined a struct to hold all Information of a WebSocket Frame

typedef struct {
    uint8_t flags;
    uint8_t opcode;
    uint8_t isMasked;
    uint64_t payloadLength;
    uint32_t maskingKey;
    char* payloadData;
} WSFrame;

I then parse the message as described in IEEE RFC6455 sec 5.2. I then need to unmask the data, since I'm the server. Again, the IEEE RFC6455 sec 5.3 is very helpful here. However, the compiler for the xtensalx106 gives me the following error:

user/websocketd.c: In function 'wsRecvCb':
user/websocketd.c:104:11: error: 'frame.maskingKey' may be used uninitialized in this function [-Werror=maybe-uninitialized]
   WSFrame frame;
           ^
cc1: all warnings being treated as errors

for the following piece of code:

WSFrame frame;
parseWsFrame(data, &frame);

os_printf("payloadLength=%"PRIu64"\n", frame.payloadLength);
unmaskWsPayload(frame.payloadData, frame.payloadLength, frame.maskingKey);

even though frame.maskingKey is defined in parseWSFrame.

What do I oversee? Why is it moaning about 'frame.maskingKey' but does mention the error is in the initialisation?

If I do frame.maskingKey = 0; right after the definition of WSFrame frame;, the compiler shuts up. However, I can not declare uint32_t maskingKey = 0; in my struct definition.

I don't want to use any heap allocations, so I don't want to malloc anything.

Upvotes: 1

Views: 216

Answers (1)

Milind Dumbare
Milind Dumbare

Reputation: 3244

I think the main thing here is -Werror flag while compiling, which is treating the warning as error. And if you still want to keep that compiler flag, you better initialize the frame.maskingKey to 0, as you did.

Initializing structure members in structure declaration is never allowed. So doing uint32_t maskingKey = 0 is not possible inside structure declaration.

Upvotes: 1

Related Questions