newlib-cygwin/newlib/libc
Joe Seymour 83c39aedac Fix incorrect cast in nano malloc
As described in nano-mallocr.c, chunks of heap are represented in memory
as a size (of type long), followed by some optional padding containing a
negative offset to size, followed by the data area.

get_chunk_from_ptr is responsible for taking a pointer to the data area
(as returned by malloc) and finding the start of the chunk. It does this
by assuming there is no padding and trying to read the size, if the size
is negative then it uses that as an offset to find the true size.
Crucially, it reads the padding area as a long.

nano_malloc is responsible for populating the optional padding area. It
does so by casting a pointer to an (int *) and writing the negative
offset into it.

This means that padding is being written as an int but read as a long.

On msp430 an int is 2 bytes, while a long is 4 bytes. This means that 2
bytes are written to the padding, but 4 bytes are read from it: it has
only been partially initialised.

nano_malloc is the default malloc implementation for msp430.

This patch changes the cast from (int *) to (long *). The change to
nano_malloc has has been observed to fix a TI Energia project that
had been malfunctioning because malloc was returning invalid addresses.
The change to nano_memalign is based entirely on code inspection.

I've built and tested as follows:
  Configured (gcc+newlib) with: --target=msp430-elf --enable-languages=c
  gcc testsuite variations:
    msp430-sim/-mcpu=msp430
    msp430-sim/-mcpu=msp430x
    msp430-sim/-mcpu=msp430x/-mlarge/-mdata-region=either/-mcode-region=either
    msp430-sim/-mhwmult=none
    msp430-sim/-mhwmult=f5series
My testing has shown no regressions, however I don't know if the gcc
testsuite provides sufficient coverage for this patch?

I don't have write access, so if this patch is acceptable after review,
I would appreciate it if someone would commit it for me.

Thanks,

2017-01-XX  Joe Seymour  <joe.s@somniumtech.com>

	newlib/
	* libc/stdlib/nano-mallocr.c (nano_malloc): Fix incorrect cast.
	(nano_memalign): Likewise.
2017-01-09 16:16:12 +01:00
..
argz Regenerate newlib Makefiles 2016-07-04 17:13:55 +01:00
ctype Bump release to 2.5.0 for yearly snapshot. 2016-12-22 21:33:54 -05:00
errno Regenerate newlib Makefiles 2016-07-04 17:13:55 +01:00
iconv Regenerate newlib Makefiles 2016-07-04 17:13:55 +01:00
include Add <devctl.h> per POSIX 1003.26-2003 2016-12-05 21:00:38 -06:00
locale Bump release to 2.5.0 for yearly snapshot. 2016-12-22 21:33:54 -05:00
machine Bump release to 2.5.0 for yearly snapshot. 2016-12-22 21:33:54 -05:00
misc Regenerate newlib Makefiles 2016-07-04 17:13:55 +01:00
posix Regenerate newlib Makefiles 2016-07-04 17:13:55 +01:00
reent Revert "Fix __getreent function for Cygwin" 2016-08-18 15:24:18 +02:00
search Regenerate newlib Makefiles 2016-07-04 17:13:55 +01:00
signal Regenerate newlib Makefiles 2016-07-04 17:13:55 +01:00
stdio 2016-12-15 Giuseppe Musumeci <giuseppe.musumeci@broadcom.com> 2016-12-15 12:12:31 -05:00
stdio64 Regenerate newlib Makefiles 2016-07-04 17:13:55 +01:00
stdlib Fix incorrect cast in nano malloc 2017-01-09 16:16:12 +01:00
string Bump release to 2.5.0 for yearly snapshot. 2016-12-22 21:33:54 -05:00
sys RTEMS: Add user-defined name to thread queues 2017-01-09 16:11:50 +01:00
syscalls Regenerate newlib Makefiles 2016-07-04 17:13:55 +01:00
time get_alt_digits: Fix typo in allocation 2016-10-22 21:29:10 +02:00
unix Regenerate newlib Makefiles 2016-07-04 17:13:55 +01:00
xdr Regenerate newlib Makefiles 2016-07-04 17:13:55 +01:00
Makefile.am Make newlib manpages (v3) 2016-07-04 14:17:10 +01:00
Makefile.in Bump release to 2.5.0 for yearly snapshot. 2016-12-22 21:33:54 -05:00
aclocal.m4 Fix up errors in regenerated files for 2.3.0 release. 2016-01-04 12:57:31 -05:00
configure Bump release to 2.5.0 for yearly snapshot. 2016-12-22 21:33:54 -05:00
configure.in 2014-07-04 Bin Cheng <bin.cheng@arm.com> 2014-07-04 17:21:45 +00:00
libc.in.xml Make newlib manpages (v3) 2016-07-04 14:17:10 +01:00
libc.texinfo Fix pdf build failure with texinfo 6.1.0 2016-10-25 17:45:11 +02:00
saber
sys.tex Don't include stdio64 functions in reentrant syscalls menu when not bulding stdio64. 2015-08-03 09:54:33 +01:00