driver: ram_console: leave one byte from the defined buffer size

Leave one byte from the CONFIG_RAM_CONSOLE_BUFFER_SIZE to ensure
the NULL-termination.

Signed-off-by: Hou Zhiqiang <Zhiqiang.Hou@nxp.com>
This commit is contained in:
Hou Zhiqiang 2024-03-19 15:28:25 +08:00 committed by David Leach
parent c1765ff690
commit 209568eabe
3 changed files with 8 additions and 6 deletions

View file

@ -108,8 +108,10 @@ config RAM_CONSOLE_BUFFER_SIZE
default 1024
depends on RAM_CONSOLE
help
Size of the RAM console buffer. Messages will wrap around if the
length is exceeded.
Total size of the RAM console buffer, to ensure it's always
NULL-terminated leave one byte unused, the actual length is
one byte less. Messages will wrap around if the actual length
is exceeded.
config RTT_CONSOLE
bool "Use RTT console"

View file

@ -15,14 +15,14 @@
extern void __printk_hook_install(int (*fn)(int));
extern void __stdout_hook_install(int (*fn)(int));
/* Extra byte to ensure we're always NULL-terminated */
char ram_console[CONFIG_RAM_CONSOLE_BUFFER_SIZE + 1];
char ram_console[CONFIG_RAM_CONSOLE_BUFFER_SIZE];
static int pos;
static int ram_console_out(int character)
{
ram_console[pos] = (char)character;
pos = (pos + 1) % CONFIG_RAM_CONSOLE_BUFFER_SIZE;
/* Leave one byte to ensure we're always NULL-terminated */
pos = (pos + 1) % (CONFIG_RAM_CONSOLE_BUFFER_SIZE - 1);
return character;
}

View file

@ -68,7 +68,7 @@ static struct fw_resource_table __resource resource_table = {
#if defined(CONFIG_RAM_CONSOLE)
.cm_trace = {
RSC_TRACE,
(uint32_t)ram_console, CONFIG_RAM_CONSOLE_BUFFER_SIZE + 1, 0,
(uint32_t)ram_console, CONFIG_RAM_CONSOLE_BUFFER_SIZE, 0,
"Zephyr_log",
},
#endif