From 2a4c6d8942d2ec90fe5ca555d2af71434bbc001b Mon Sep 17 00:00:00 2001 From: Jim Jagielski Date: Mon, 9 Apr 2018 15:37:44 +0000 Subject: [PATCH] Merge r1534895, r1534896, r1534914 from trunk: rotatelogs: Use apr_psprintf() with %pm instead of a constant length buffer for errors. * support/rotatelogs.c (post_rotate, doRotate): Switch to using apr_psprintf() with %pm. Suggested by: rpluem rotatelogs: Remove another use of a consant length buffer for errors. * support/rotatelogs.c (doRotate): Use apr_psprintf() and %pm. Move the destruction of the pool after we're done with the error message so the error string stays allocated long enough. rotatelogs: Remove last constant length error buffer. * support/rotatelogs.c (ERRMSGSZ): Remove. (rotate_status): Remove errbuff member. (truncate_and_write_error): Accept the error message as an argument. (doRotate): Shift the pool destruction slightly later and use it to generate the error message to pass truncate_and_write_error(). (main): In case of write errors create a pool to generate the error message, since the other pools available may never been freed. Adjust to pass message directly to truncate_and_write_error(). Submitted by: breser Reviewed by: rjung, ylavic, jim git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/branches/2.4.x@1828739 13f79535-47bb-0310-9956-ffa450edef68 --- STATUS | 8 -------- support/rotatelogs.c | 46 ++++++++++++++++++++++---------------------- 2 files changed, 23 insertions(+), 31 deletions(-) diff --git a/STATUS b/STATUS index 69a4dd9172..4b7076f80e 100644 --- a/STATUS +++ b/STATUS @@ -122,14 +122,6 @@ RELEASE SHOWSTOPPERS: PATCHES ACCEPTED TO BACKPORT FROM TRUNK: [ start all new proposals below, under PATCHES PROPOSED. ] - *) rotatelogs: Remove multiple uses of constant length error buffers. - trunk patches: http://svn.apache.org/r1534895 - http://svn.apache.org/r1534896 - http://svn.apache.org/r1534914 - 2.4.x patch: svn merge -c 1534895,1534896,1534914 ^/httpd/httpd/trunk . - plus CHANGES - +1: rjung, ylavic,, jim - *) mod_md: Fix compilation with OpenSSL before version 1.0.2. Symbol ASN1_TIME_diff is only available for 1.0.2+, but luckily alternative code we can use is already diff --git a/support/rotatelogs.c b/support/rotatelogs.c index 26e8b47fe1..2526f56953 100644 --- a/support/rotatelogs.c +++ b/support/rotatelogs.c @@ -36,7 +36,6 @@ #include "apr_want.h" #define BUFSIZE 65536 -#define ERRMSGSZ 256 #define ROTATE_NONE 0 #define ROTATE_NEW 1 @@ -94,7 +93,6 @@ struct logfile { struct rotate_status { struct logfile current; /* current logfile. */ apr_pool_t *pool; /* top-level pool */ - char errbuf[ERRMSGSZ]; int rotateReason; adjusted_time_t tLogEnd; int nMessCount; @@ -347,19 +345,19 @@ static void post_rotate(apr_pool_t *pool, struct logfile *newlog, } /* After a error, truncate the current file and write out an error - * message, which must be contained in status->errbuf. The process is + * message, which must be contained in message. The process is * terminated on failure. */ -static void truncate_and_write_error(rotate_status_t *status) +static void truncate_and_write_error(rotate_status_t *status, const char *message) { - apr_size_t buflen = strlen(status->errbuf); + apr_size_t buflen = strlen(message); if (apr_file_trunc(status->current.fd, 0) != APR_SUCCESS) { fprintf(stderr, "Error truncating the file %s\n", status->current.name); exit(2); } - if (apr_file_write_full(status->current.fd, status->errbuf, buflen, NULL) != APR_SUCCESS) { + if (apr_file_write_full(status->current.fd, message, buflen, NULL) != APR_SUCCESS) { fprintf(stderr, "Error writing error (%s) to the file %s\n", - status->errbuf, status->current.name); + message, status->current.name); exit(2); } } @@ -478,9 +476,8 @@ static void doRotate(rotate_config_t *config, rotate_status_t *status) status->current = newlog; } else { - char error[120]; - - apr_strerror(rv, error, sizeof error); + char *error = apr_psprintf(newlog.pool, "%pm", &rv); + char *message; /* Uh-oh. Failed to open the new log file. Try to clear * the previous log file, note the lost log entries, @@ -490,17 +487,17 @@ static void doRotate(rotate_config_t *config, rotate_status_t *status) exit(2); } - /* Throw away new state; it isn't going to be used. */ - apr_pool_destroy(newlog.pool); - /* Try to keep this error message constant length * in case it occurs several times. */ - apr_snprintf(status->errbuf, sizeof status->errbuf, - "Resetting log file due to error opening " - "new log file, %10d messages lost: %-25.25s\n", - status->nMessCount, error); + message = apr_psprintf(newlog.pool, + "Resetting log file due to error opening " + "new log file, %10d messages lost: %-25.25s\n", + status->nMessCount, error); - truncate_and_write_error(status); + truncate_and_write_error(status, message); + + /* Throw away new state; it isn't going to be used. */ + apr_pool_destroy(newlog.pool); } status->nMessCount = 0; @@ -753,18 +750,21 @@ int main (int argc, const char * const argv[]) rv = apr_file_write_full(status.current.fd, buf, nWrite, &nWrite); if (nWrite != nRead) { apr_off_t cur_offset; + apr_pool_t *pool; + char *error; cur_offset = 0; if (apr_file_seek(status.current.fd, APR_CUR, &cur_offset) != APR_SUCCESS) { cur_offset = -1; } status.nMessCount++; - apr_snprintf(status.errbuf, sizeof status.errbuf, - "Error %d writing to log file at offset %" APR_OFF_T_FMT ". " - "%10d messages lost (%pm)\n", - rv, cur_offset, status.nMessCount, &rv); + apr_pool_create(&pool, status.pool); + error = apr_psprintf(pool, "Error %d writing to log file at offset %" + APR_OFF_T_FMT ". %10d messages lost (%pm)\n", + rv, cur_offset, status.nMessCount, &rv); - truncate_and_write_error(&status); + truncate_and_write_error(&status, error); + apr_pool_destroy(pool); } else { status.nMessCount++; -- 2.40.0