Original code (sysdep.c) assumed a return code of -1 from vsnprintf()
was a truncation. Actually, it is an output error and the code still
tried to output it. A return of the buffer size or larger means it was
truncated. Changed the processing to handle return values properly.
void cprintf(const char *format, ...) {
va_list arg_ptr;
char buf[1024];
void cprintf(const char *format, ...) {
va_list arg_ptr;
char buf[1024];
va_start(arg_ptr, format);
va_start(arg_ptr, format);
- if (vsnprintf(buf, sizeof buf, format, arg_ptr) == -1)
- buf[sizeof buf - 2] = '\n';
- client_write(buf, strlen(buf));
+ rc = vsnprintf(buf, sizeof buf, format, arg_ptr);
+ if (rc < 0) {
+ syslog(LOG_ERR,"Console output error occured. Format: %s",format);
+ } else {
+ if (rc >= sizeof buf) {
+ // Output was truncated.
+ // Chop at the end of the buffer
+ buf[sizeof buf - 2] = '\n';
+ buf[sizeof buf - 1] = 0;
+ }
+ client_write(buf, strlen(buf));
+ }