Skip to content

Commit af1b331

Browse files
committed
Add nano-malloc-clear-freed option
With this option enabled, all memory released in free or realloc gets cleared to zero. Signed-off-by: Keith Packard <[email protected]>
1 parent 01e8f20 commit af1b331

File tree

4 files changed

+13
-0
lines changed

4 files changed

+13
-0
lines changed

meson.build

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,7 @@ newlib_iconv_external_ccs = get_option('newlib-iconv-external-ccs')
185185

186186
newlib_atexit_dynamic_alloc = get_option('newlib-atexit-dynamic-alloc')
187187
newlib_nano_malloc = get_option('newlib-nano-malloc')
188+
nano_malloc_clear_freed = get_option('nano-malloc-clear-freed')
188189
lite_exit = get_option('lite-exit')
189190

190191
newlib_elix_level = get_option('newlib-elix-level')
@@ -1365,6 +1366,7 @@ conf_data.set('_FSEEK_OPTIMIZATION', newlib_fseek_optimization)
13651366
conf_data.set('_WIDE_ORIENT', newlib_wide_orient)
13661367
conf_data.set('_HAVE_FCNTL', newlib_have_fcntl)
13671368
conf_data.set('_NANO_MALLOC', newlib_nano_malloc)
1369+
conf_data.set('_NANO_MALLOC_CLEAR_FREED', nano_malloc_clear_freed and newlib_nano_malloc)
13681370
conf_data.set('_UNBUF_STREAM_OPT', get_option('newlib-unbuf-stream-opt'))
13691371
conf_data.set('_LITE_EXIT', lite_exit)
13701372
conf_data.set('_PICO_EXIT', picoexit)

meson_options.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,8 @@ option('atomic-signal', type: 'boolean', value: true,
263263
#
264264
option('newlib-nano-malloc', type: 'boolean', value: true,
265265
description: 'use small-footprint nano-malloc implementation')
266+
option('nano-malloc-clear-freed', type: 'boolean', value: false,
267+
description: 'nano version erases memory on free/realloc')
266268

267269
#
268270
# Locking support

newlib/libc/stdlib/nano-free.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,12 @@ free (void * free_p)
4646
if (free_p == NULL) return;
4747

4848
p_to_free = ptr_to_chunk(free_p);
49+
50+
#ifdef _NANO_MALLOC_CLEAR_FREED
51+
memset(p_to_free, 0, chunk_usable(p_to_free));
52+
#else
4953
p_to_free->next = NULL;
54+
#endif
5055

5156
#if MALLOC_DEBUG
5257
__malloc_validate_block(p_to_free);

newlib/libc/stdlib/nano-realloc.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ realloc(void * ptr, size_t size)
113113
{
114114
size_t extra = old_size - new_size;
115115

116+
#ifdef _NANO_MALLOC_CLEAR_FREED
117+
if (extra > MALLOC_HEAD)
118+
memset((char *) ptr + new_size, 0, extra - MALLOC_HEAD);
119+
#endif
116120
/* If there's enough space left over, split it out
117121
* and free it
118122
*/

0 commit comments

Comments
 (0)