Skip to content

Commit

Permalink
simplify truncation in echo()
Browse files Browse the repository at this point in the history
  • Loading branch information
falkoschindler committed Oct 25, 2024
1 parent 0780f2e commit c3f06ff
Showing 1 changed file with 2 additions and 6 deletions.
8 changes: 2 additions & 6 deletions main/utils/uart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,11 @@

void echo(const char *format, ...) {
static char buffer[1024];
int pos = 0;

va_list args;
va_start(args, format);
const int num_chars = std::vsnprintf(&buffer[pos], sizeof(buffer) - pos - 1, format, args);
if (num_chars >= (sizeof(buffer) - pos - 1))
pos = sizeof(buffer) - pos - 1;
else
pos += num_chars;
const int num_chars = std::vsnprintf(buffer, sizeof(buffer) - 1, format, args);
int pos = std::min(num_chars, static_cast<int>(sizeof(buffer) - 1));
va_end(args);

pos += std::sprintf(&buffer[pos], "\n");
Expand Down

0 comments on commit c3f06ff

Please sign in to comment.