Skip to content

Commit

Permalink
driver: ram_console: leave one byte from the defined buffer size
Browse files Browse the repository at this point in the history
Leave one byte from the CONFIG_RAM_CONSOLE_BUFFER_SIZE to ensure
the NULL-termination.

Signed-off-by: Hou Zhiqiang <[email protected]>
  • Loading branch information
Zhiqiang-Hou authored and dleach02 committed Jun 12, 2024
1 parent c1765ff commit 209568e
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
6 changes: 4 additions & 2 deletions drivers/console/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
6 changes: 3 additions & 3 deletions drivers/console/ram_console.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/open-amp/resource_table.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 209568e

Please sign in to comment.