From f8606591f8ee2f688b0196b577fdfa9d73a69c43 Mon Sep 17 00:00:00 2001 From: Danil Korotenko Date: Fri, 2 Apr 2021 17:59:24 +0300 Subject: [PATCH] When log messages with 'write' function it could be an inconsistency between the log message string and the string length. To avoid this situation, added 'backendMessage' function that will count the log message string length automatically. --- backend/backend-private.h | 6 ++++++ backend/ipp.c | 4 ++-- backend/snmp.c | 2 +- backend/testbackend.c | 5 +++-- backend/usb-darwin.c | 2 +- 5 files changed, 13 insertions(+), 6 deletions(-) diff --git a/backend/backend-private.h b/backend/backend-private.h index 85fb8aef8a..9b656ebd4b 100644 --- a/backend/backend-private.h +++ b/backend/backend-private.h @@ -320,6 +320,12 @@ extern int backendSNMPSupplies(int snmp_fd, http_addr_t *addr, extern int backendWaitLoop(int snmp_fd, http_addr_t *addr, int use_bc, _cups_sccb_t side_cb); +/* + * Used to log messages in backend. + * This was done to avoid a mismatch between the message string and the length of the string to write. + */ +#define CUPS_BACKEND_LOG_MESSAGE_SIZE 2048 +#define backendMessage(msg) {const char *s=msg; write(2, s, strnlen(s, CUPS_BACKEND_LOG_MESSAGE_SIZE));} # ifdef __cplusplus } diff --git a/backend/ipp.c b/backend/ipp.c index 3a84d14e41..63353a66df 100644 --- a/backend/ipp.c +++ b/backend/ipp.c @@ -3418,7 +3418,7 @@ sigterm_handler(int sig) /* I - Signal */ { (void)sig; /* remove compiler warnings... */ - write(2, "DEBUG: Got SIGTERM.\n", 20); + backendMessage("DEBUG: Got SIGTERM.\n"); #if defined(HAVE_GSSAPI) && defined(HAVE_XPC) if (child_pid) @@ -3434,7 +3434,7 @@ sigterm_handler(int sig) /* I - Signal */ * Flag that the job should be canceled... */ - write(2, "DEBUG: sigterm_handler: job_canceled = 1.\n", 42); + backendMessage("DEBUG: sigterm_handler: job_canceled = 1.\n"); job_canceled = 1; return; diff --git a/backend/snmp.c b/backend/snmp.c index 9572822a8e..084c6f5118 100644 --- a/backend/snmp.c +++ b/backend/snmp.c @@ -424,7 +424,7 @@ alarm_handler(int sig) /* I - Signal number */ #endif /* !HAVE_SIGSET && !HAVE_SIGACTION */ if (DebugLevel) - write(2, "DEBUG: ALARM!\n", 14); + backendMessage("DEBUG: ALARM!\n"); } diff --git a/backend/testbackend.c b/backend/testbackend.c index 6dbd0abee6..6412a022b7 100644 --- a/backend/testbackend.c +++ b/backend/testbackend.c @@ -19,6 +19,7 @@ #include #include #include +#include "backend-private.h" /* @@ -395,7 +396,7 @@ main(int argc, /* I - Number of command-line args */ data = ps_data; write(1, data, strlen(data)); - write(2, "DEBUG: START\n", 13); + backendMessage("DEBUG: START\n"); timeout = 60.0; while ((bytes = cupsBackChannelRead(buffer, sizeof(buffer), timeout)) > 0) @@ -403,7 +404,7 @@ main(int argc, /* I - Number of command-line args */ write(2, buffer, (size_t)bytes); timeout = 5.0; } - write(2, "\nDEBUG: END\n", 12); + backendMessage("\nDEBUG: END\n"); } exit(0); diff --git a/backend/usb-darwin.c b/backend/usb-darwin.c index 0b69967621..25e58cdda0 100644 --- a/backend/usb-darwin.c +++ b/backend/usb-darwin.c @@ -2231,7 +2231,7 @@ sigterm_handler(int sig) /* I - Signal */ _exit(0); else { - write(2, "DEBUG: Child crashed.\n", 22); + backendMessage("DEBUG: Child crashed.\n"); _exit(CUPS_BACKEND_STOP); } } -- 2.47.2