Skip to content

Commit b25ccca

Browse files
committed
Avoid user heap to override the minimum stack size
Minimum stack size is defined in linker script: _Min_Stack_Size = 0x400;; /* required amount of stack */ If more stack is requested, then user have to ensure that heap and stack can fit in the SRAM. Fix #307 Signed-off-by: Frederic.Pillon <frederic.pillon@st.com>
1 parent 8cef68d commit b25ccca

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

cores/arduino/syscalls.c

+7
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ register char * stack_ptr asm("sp");
2929

3030
__attribute__((weak))
3131
caddr_t _sbrk( int incr ) {
32+
extern char _estack; /* Defined in the linker script */
33+
extern char _Min_Stack_Size; /* Defined in the linker script */
3234
extern char _end; /* Defined by the linker */
3335
static char *heap_end = &_end ;
3436
char *prev_heap_end = heap_end;
@@ -38,6 +40,11 @@ caddr_t _sbrk( int incr ) {
3840
errno = ENOMEM;
3941
return (caddr_t) -1;
4042
}
43+
/* Ensure to keep minimun stack size defined in the linker script */
44+
if (heap_end + incr >= (char*)(&_estack - &_Min_Stack_Size)) {
45+
errno = ENOMEM;
46+
return (caddr_t) -1;
47+
}
4148

4249
heap_end += incr ;
4350
return (caddr_t) prev_heap_end ;

0 commit comments

Comments
 (0)