mbed-os/libraries
Adam Green e0fb0a8f9b Don't dereference NULL ipaddr in netif_set_ipaddr()
The code in netif_set_ipaddr would read the memory pointed to by its
ipaddr parameter, even if it was NULL on this line:
  if ((ip_addr_cmp(ipaddr, &(netif->ip_addr))) == 0) {
On the Cortex-M3, it is typically OK to read from address 0 so this
code will actually compare the reset stack pointer value to the
current value in netif->ip_addr.

Later in the code, this same pointer will be used for a second read:
  ip_addr_set(&(netif->ip_addr), ipaddr);

The ip_addr_set call will first check to see if the ipaddr is NULL and
if so, treats it like IP_ADDR_ANY (4 bytes of 0).
    /** Safely copy one IP address to another (src may be NULL) */
    #define ip_addr_set(dest, src) ((dest)->addr = \
                                        ((src) == NULL ? 0 : \
                                        (src)->addr))

The issue here is that when GCC optimizes this code, it assumes that
the first dereference of ipaddr would have thrown an invalid memory
access exception and execution would never make it to this second
dereference.  Therefore it optimizes out the NULL check in ip_addr_set.

The -fno-delete-null-pointer-checks will disable this optimization and
is a good thing to use with GCC in general on Cortex-M parts.  I will
let the mbed guys make that change to their build system.

I have however corrected the code so that the intent of how to handle a
NULL ipaddr is more obvious and gets rid of the potential NULL
dereference.

By the way, this bug caused connect() to fail in obtaining an
address from DHCP.  If I recall correctly from when I first debugged
this issue (late last year), I actually saw the initial value of the
stack pointer being used in the DHCP request as an IP address which
caused it to be rejected.
2013-08-15 19:02:51 -07:00
..
USBDevice Updated pin mapping and CAN HAL for LPC4088 target 2013-08-08 13:57:02 +02:00
USBHost usbhost: fixed skip bits/max size conf descriptor 2013-03-18 14:54:08 +00:00
doc Initial commit of the mbed libraries and tools 2013-02-18 15:32:11 +00:00
dsp Initial commit of the mbed libraries and tools 2013-02-18 15:32:11 +00:00
fs Initial commit of the mbed libraries and tools 2013-02-18 15:32:11 +00:00
mbed Fixups to network code after recent merges. 2013-08-15 04:40:53 -07:00
net Don't dereference NULL ipaddr in netif_set_ipaddr() 2013-08-15 19:02:51 -07:00
rpc Updated pin mapping and CAN HAL for LPC4088 target 2013-08-08 13:57:02 +02:00
rtos Merge remote-tracking branch 'upstream/master' 2013-07-26 15:50:59 +01:00
tests Compile network and RTOS with GCC_ARM 2013-08-14 22:34:33 +02:00