Skip to content

Commit 1898625

Browse files
author
Hubert Miś
committed
libc: newlib: assert: Add handler for C std assert
Libraries using standard C assert from <assert.h> did not give useful information on assertion fault, as default newlib implementation tries to print assert info to stderr. It causes another assertion in Zephyr. With this patch, the default handler is overridden to use native Zephyr assert macros and give useful info when assertion fails. Signed-off-by: Hubert Miś <[email protected]>
1 parent 0931046 commit 1898625

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

lib/libc/newlib/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
zephyr_library()
44
zephyr_library_sources(libc-hooks.c)
55

6+
zephyr_library_sources_ifdef(CONFIG_ASSERT assert.c)
7+
68
# Zephyr normally uses -ffreestanding, which with current GNU toolchains
79
# means that the flag macros used by newlib 3.x <inttypes.h> to signal
810
# support for PRI.64 macros are not present. To make them available we
@@ -47,4 +49,3 @@ if(CONFIG_NEWLIB_LIBC_NANO)
4749
-specs=nano.specs
4850
)
4951
endif()
50-

lib/libc/newlib/assert.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* Copyright (c) 2020 Nordic Semiconductor ASA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <sys/__assert.h>
8+
9+
/**
10+
*
11+
* @brief Handler of C standard library assert.
12+
*/
13+
void __assert_func(const char *file,
14+
int line,
15+
const char *func,
16+
const char *failedexpr)
17+
{
18+
__ASSERT_LOC(...);
19+
__ASSERT_MSG_INFO("Assertion fail: (%s) at %s:%d",
20+
failedexpr, file, line);
21+
__ASSERT_POST_ACTION();
22+
}

0 commit comments

Comments
 (0)