From: Arran Cudbard-Bell Date: Wed, 15 Jul 2015 03:36:16 +0000 (-0400) Subject: Alternate where we write errors in fr_strerror_printf to allow error messages to... X-Git-Tag: release_3_0_10~335 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a6d3793cb93444b4ced344c8c3dfa5cbbcde4f54;p=thirdparty%2Ffreeradius-server.git Alternate where we write errors in fr_strerror_printf to allow error messages to be appended to --- diff --git a/src/lib/log.c b/src/lib/log.c index b0a978e5461..227b3a42d4d 100644 --- a/src/lib/log.c +++ b/src/lib/log.c @@ -65,7 +65,7 @@ void fr_strerror_printf(char const *fmt, ...) /* * malloc is thread safe, talloc is not */ - buffer = malloc(sizeof(char) * (FR_STRERROR_BUFSIZE + 1)); /* One byte extra for status */ + buffer = calloc((FR_STRERROR_BUFSIZE * 2) + 1, sizeof(char)); /* One byte extra for status */ if (!buffer) { fr_perror("Failed allocating memory for libradius error buffer"); return; @@ -83,13 +83,26 @@ void fr_strerror_printf(char const *fmt, ...) * NULL has a special meaning, setting the new byte to false. */ if (!fmt) { - buffer[FR_STRERROR_BUFSIZE] = '\0'; + buffer[FR_STRERROR_BUFSIZE * 2] = '\0'; return; } va_start(ap, fmt); - vsnprintf(buffer, FR_STRERROR_BUFSIZE, fmt, ap); - buffer[FR_STRERROR_BUFSIZE] = '\1'; /* Flip the 'new' byte to true */ + /* + * Alternate where we write the message, so we can do: + * fr_strerror_printf("Additional error: %s", fr_strerror()); + */ + switch (buffer[FR_STRERROR_BUFSIZE * 2]) { + default: + vsnprintf(buffer + FR_STRERROR_BUFSIZE, FR_STRERROR_BUFSIZE, fmt, ap); + buffer[FR_STRERROR_BUFSIZE * 2] = '\2'; /* Flip the 'new' byte to true */ + break; + + case '\2': + vsnprintf(buffer, FR_STRERROR_BUFSIZE, fmt, ap); + buffer[FR_STRERROR_BUFSIZE * 2] = '\1'; /* Flip the 'new' byte to true */ + break; + } va_end(ap); } @@ -104,12 +117,20 @@ char const *fr_strerror(void) char *buffer; buffer = fr_thread_local_get(fr_strerror_buffer); - if (buffer && (buffer[FR_STRERROR_BUFSIZE] != '\0')) { - buffer[FR_STRERROR_BUFSIZE] = '\0'; /* Flip the 'new' byte to false */ + if (!buffer) return ""; + + switch (buffer[FR_STRERROR_BUFSIZE * 2]) { + default: + return ""; + + case '\1': + buffer[FR_STRERROR_BUFSIZE * 2] = '\0'; /* Flip the 'new' byte to false */ return buffer; - } - return ""; + case '\2': + buffer[FR_STRERROR_BUFSIZE * 2] = '\0'; /* Flip the 'new' byte to false */ + return buffer + FR_STRERROR_BUFSIZE; + } } /** Guaranteed to be thread-safe version of strerror