sysdep.c: cprintf() truncation fix (Phil Slack)
authorArt Cancro <ajc@citadel.org>
Thu, 4 May 2023 22:11:24 +0000 (18:11 -0400)
committerArt Cancro <ajc@citadel.org>
Thu, 4 May 2023 22:11:24 +0000 (18:11 -0400)
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.

citadel/server/sysdep.c

index 702cc2a90299c2fb11cda206697738626e69ccfd..21ba4c7b055d1eefce318cf308d1b8d95be6be2b 100644 (file)
@@ -369,11 +369,21 @@ void cputbuf(const StrBuf *Buf) {
 void cprintf(const char *format, ...) {   
        va_list arg_ptr;   
        char buf[1024];
+       int rc;
    
        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)); 
+       }
        va_end(arg_ptr);
 }