Buffer overflow while using CONFIG_NEWLIB_LIBC and not able to use math.h in my application

My application requires mathematical calculation based on double and float. For this I have included <math.h> in the header files.

FYI : I am using NCS 2.2.0

Also included -

CONFIG_NEWLIB_LIBC=y
CONFIG_MAIN_STACK_SIZE=8192
CONFIG_BT_HCI_TX_STACK_SIZE=4096
CONFIG_BT_RX_STACK_SIZE=4096
CONFIG_IDLE_STACK_SIZE=4096
CONFIG_MPU_ALLOW_FLASH_WRITE=n
CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=8192
CONFIG_MPSL_WORK_STACK_SIZE=8192
CONFIG_ISR_STACK_SIZE=8192
CONFIG_CBPRINTF_FP_SUPPORT=y
CONFIG_FPU=y
CONFIG_USERSPACE=y
CONFIG_NEWLIB_LIBC_ALIGNED_HEAP_SIZE=8192

But I getting consistent reset because of the buffer flow and seeing Faulting instruction address (r15/pc): 0x00025eee , which I can trace back using addr2line to

zephyr/lib/libc/newlib/libc-hooks.c:471 which is below part :

/* This function gets called if static buffer overflow detection is enabled
 * on stdlib side (Newlib here), in case such an overflow is detected. Newlib
 * provides an implementation not suitable for us, so we override it here.
 */
__weak FUNC_NORETURN void __chk_fail(void)
{
	static const char chk_fail_msg[] = "* buffer overflow detected *\n";
	_write(2, chk_fail_msg, sizeof(chk_fail_msg) - 1);
	k_oops();
	CODE_UNREACHABLE;
}

Please help in resolving this so that my algo can work properly.

double res = 9724/1024 is resulting in 0 , but if I change my definition to -
int32_t res = 9724/1024 is resulting in 9, which is correct result.

Related