256dpi / lwmqtt

a light weight MQTT implementation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Uninitialised value

user115122 opened this issue · comments

Ran the example on a MIPS little endian target, crash during startup. Does not crash on x86, but valgrind unveils what's wrong:

$ valgrind ./example1
==3336== Memcheck, a memory error detector
==3336== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==3336== Using Valgrind-3.12.0.SVN and LibVEX; rerun with -h for copyright info
==3336== Command: ./example1
==3336==
==3336== Conditional jump or move depends on uninitialised value(s)
==3336==    at 0x804BBB4: lwmqtt_unix_network_disconnect (unix.c:107)
==3336==    by 0x804BA6D: lwmqtt_unix_network_connect (unix.c:44)
==3336==    by 0x80492BA: main (main.c:29)
==3336==
==3336== Syscall param close(fd) contains uninitialised byte(s)
==3336==    at 0x4128A5C: __close_nocancel (syscall-template.S:84)
==3336==    by 0x804BBC3: lwmqtt_unix_network_disconnect (unix.c:108)
==3336==    by 0x804BA6D: lwmqtt_unix_network_connect (unix.c:44)
==3336==    by 0x80492BA: main (main.c:29)
==3336==
==3336== Warning: invalid file descriptor 134513736 in syscall close()

As a workaround, you can trick lwmqtt_unix_network_disconnect() into not reading that uninitialized memory by doing something like this before lwmqtt_unix_network_connect():

...
lwmqtt_unix_network_t lwmqtt_network;
lwmqtt_network.socket = 0;
lwmqtt_err_t err = lwmqtt_unix_network_connect(&network, "broker.shiftr.io", 1883);
...

But it would be better if lwmqtt_unix_network_disconnect() takes care of the problem.

I think you only need to properly initialize the struct:

lwmqtt_unix_network_t network = {0};

That way the FD should be properly initialized to zero on the first lwmqtt_unix_network_connect().

I updated the example to reflect this.

Yes that fixes it