Skip to content

Commit 5f05d65

Browse files
pfalconcarlescufi
authored andcommitted
libc: newlib: libc-hooks: Provide our own implementation of __chk_fail()
The version as shipped in Newlib itself is coded a bit sloppily for an embedded environment. We thus want to override it (and make it weak, to allow user apps to override it in turn, if needed). The desired properties of the implementation are: 1. It should call _write() (Newlib implementation calls write()). 2. It should be minimal (Newlib implementation allocates message on the stack, i.e. misses "static const"). Signed-off-by: Paul Sokolovsky <[email protected]>
1 parent 88f25df commit 5f05d65

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

lib/libc/newlib/libc-hooks.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,18 @@ __weak int *__errno(void)
275275
return z_errno();
276276
}
277277

278+
/* This function gets called if static buffer overflow detection is enabled
279+
* on stdlib side (Newlib here), in case such an overflow is detected. Newlib
280+
* provides an implementation not suitable for us, so we override it here.
281+
*/
282+
__weak FUNC_NORETURN void __chk_fail(void)
283+
{
284+
static const char chk_fail_msg[] = "* buffer overflow detected *\n";
285+
_write(2, chk_fail_msg, sizeof(chk_fail_msg) - 1);
286+
k_oops();
287+
CODE_UNREACHABLE;
288+
}
289+
278290
#if CONFIG_XTENSA
279291
extern int _read(int fd, char *buf, int nbytes);
280292
extern int _open(const char *name, int mode);

0 commit comments

Comments
 (0)