SmingHub / Sming

Sming - powerful open source framework simplifying the creation of embedded C++ applications.
https://sming.readthedocs.io
GNU Lesser General Public License v3.0
1.47k stars 348 forks source link

Fix UBSAN bugs #2845

Closed mikee47 closed 3 months ago

mikee47 commented 3 months ago

This PR fixes some potential/actual bugs identified by the undefined behaviour sanitizer. Several of these became apparent with -Wextra, mainly through 'unused parameter' warnings, of which there are many.

String::replace: Passing nullptr to memcpy is undefined behaviour

axtls sha1: Undefined behaviour

axtls-8266/crypto/sha1.c:124:42: runtime error: left shift of 128 by 24 places cannot be represented in type 'int' SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior axtls-8266/crypto/sha1.c:124:42

Because: implicit conversion of uint8_t to int

Don't force queueCallback with no parameter

Sanitizer reports undefined behaviour passing parameter to function which doesn't expect one. Works fine in practice but safer to add a lamba for dealing with it.

Stream::parseFloat() not handling timeout

String(unsigned char, ...) ignoring width, pad parameters

char could be signed, check for > 127 would never succeed

HostTests datetime check should use parameter

Fix unimplemented host RtcClass::setRtcNanoseconds()

Potential array bounds violation in esp-open-lwip dhcp.c

Check for index in range should be first.

lwip/core/dhcp.c: In function 'dhcp_recv':
lwip/core/dhcp.c:133:69: warning: array subscript 10 is above array bounds of 'u8_t[10]' {aka 'unsigned char[10]'} [-Warray-bounds]
  133 | #define dhcp_option_given(dhcp, idx)          (dhcp_rx_options_given[idx] != 0)
      |                                                ~~~~~~~~~~~~~~~~~~~~~^~~~~
lwip/core/dhcp.c:592:9: note: in expansion of macro 'dhcp_option_given'
  592 |   while(dhcp_option_given(dhcp, DHCP_OPTION_IDX_DNS_SERVER + n) && (n < DNS_MAX_SERVERS)) {
      |         ^~~~~~~~~~~~~~~~~
lwip/core/dhcp.c:131:7: note: while referencing 'dhcp_rx_options_given'
  131 | u8_t  dhcp_rx_options_given[DHCP_OPTION_IDX_MAX];
      |       ^~~~~~~~~~~~~~~~~~~~~