Skip to content

Commit 2b39e2a

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 stm32duino#307 Signed-off-by: Frederic.Pillon <[email protected]>
1 parent cf4561c commit 2b39e2a

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

cores/arduino/syscalls.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ extern size_t uart_debug_write(uint8_t *data, uint32_t size);
2727

2828
register char * stack_ptr asm("sp");
2929

30-
__attribute__((weak))
3130
caddr_t _sbrk( int incr ) {
31+
extern char _estack; /* Defined in the linker script */
32+
extern char _Min_Stack_Size; /* Defined in the linker script */
3233
extern char _end; /* Defined by the linker */
3334
static char *heap_end = NULL ;
3435
char *prev_heap_end ;
@@ -43,6 +44,11 @@ caddr_t _sbrk( int incr ) {
4344
errno = ENOMEM;
4445
return (caddr_t) -1;
4546
}
47+
/* Ensure to keep minimun stack size defined in the linker script */
48+
if (heap_end + incr > (&_estack - &_Min_Stack_Size)) {
49+
errno = ENOMEM;
50+
return (caddr_t) -1;
51+
}
4652

4753
heap_end += incr ;
4854
return (caddr_t) prev_heap_end ;

0 commit comments

Comments
 (0)