From 8bd1de8259c33ab62b673ea4c26d3a33dacf8afe Mon Sep 17 00:00:00 2001 From: Teufelchen1 Date: Thu, 18 Apr 2024 17:02:31 +0200 Subject: [PATCH] shell/vfs: Handle print failure in genfile cmd gracefully --- sys/shell/cmds/vfs.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/sys/shell/cmds/vfs.c b/sys/shell/cmds/vfs.c index 5279e117c8a0..5fe7375f6ef8 100644 --- a/sys/shell/cmds/vfs.c +++ b/sys/shell/cmds/vfs.c @@ -742,11 +742,19 @@ static char _get_char(unsigned i) static void _write_block(int fd, unsigned bs, unsigned i) { char block[bs]; - char *buf = block; - buf += snprintf(buf, bs, "|%03u|", i); + int size_wanted = snprintf(block, bs, "|%03u|", i); + + if (size_wanted < 0) { + assert(0); + return; + } + + /* Only memset the buffer, if there is space left in the buffer */ + if ((unsigned) size_wanted < bs) { + memset(&block[size_wanted], _get_char(i), bs - size_wanted); + } - memset(buf, _get_char(i), &block[bs] - buf); block[bs - 1] = '\n'; vfs_write(fd, block, bs);