Skip to content

Commit 644de3f

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 <[email protected]>
1 parent ab6c11c commit 644de3f

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 = NULL ;
3436
char *prev_heap_end ;
@@ -43,6 +45,11 @@ caddr_t _sbrk( int incr ) {
4345
errno = ENOMEM;
4446
return (caddr_t) -1;
4547
}
48+
/* Ensure to keep minimun stack size defined in the linker script */
49+
if (heap_end + incr > (char*)(&_estack - &_Min_Stack_Size)) {
50+
errno = ENOMEM;
51+
return (caddr_t) -1;
52+
}
4653

4754
heap_end += incr ;
4855
return (caddr_t) prev_heap_end ;

0 commit comments

Comments
 (0)