BUG/MINOR: logs: prevent double line returns in some events.
authorEmeric Brun <ebrun@haproxy.com>
Tue, 12 May 2020 17:33:15 +0000 (19:33 +0200)
committerWilly Tarreau <w@1wt.eu>
Thu, 28 May 2020 06:43:10 +0000 (08:43 +0200)
Historically some messages used to already contain the trailing LF but
not all, and __do_send_log adds a new one in needed cases. It also does
trim a trailing LF in certain cases while computing the max message
length, as a result of subtracting 1 to the available room in the
destination buffer. But the way it's done is wrong since some messages
still contain it.

So the code was fixed to always trim the trailing LF from messages if
present, and then only subtract 1 from the destination buffer room
instead of the size..

Note: new sink API is not designed to receive a trailing LF on
event messages

This could be backported to relevant stable versions with particular
care since the logic of the code changed a bit since 1.6 and there
may be other locations that need to be adjusted.

(cherry picked from commit 9e8ea0ae6f63fe1a8119d99b1218ccc095b390ac)
Signed-off-by: Willy Tarreau <w@1wt.eu>

src/log.c

index 23e6c47..cf5d87e 100644 (file)
--- a/src/log.c
+++ b/src/log.c
@@ -1540,6 +1540,10 @@ static inline void __do_send_log(struct logsrv *logsrv, int nblogger, char *pid_
 
        dataptr = message;
 
+       /* historically some messages used to already contain the trailing LF */
+       if (size && (dataptr[size-1] == '\n'))
+               size--;
+
        if (logsrv->type == LOG_TARGET_FD) {
                /* the socket's address is a file descriptor */
                plogfd = (int *)&((struct sockaddr_in *)&logsrv->addr)->sin_addr.s_addr;
@@ -1594,7 +1598,7 @@ static inline void __do_send_log(struct logsrv *logsrv, int nblogger, char *pid_
                hdr_ptr = hdr;
                hdr_max = 3;
                maxlen = logsrv->maxlen - hdr_max;
-               max = MIN(size, maxlen) - 1;
+               max = MIN(size, maxlen - 1);
                goto send;
 
        case LOG_FORMAT_RAW:
@@ -1602,7 +1606,7 @@ static inline void __do_send_log(struct logsrv *logsrv, int nblogger, char *pid_
                hdr_ptr = hdr = "";
                hdr_max = 0;
                maxlen = logsrv->maxlen;
-               max = MIN(size, maxlen) - 1;
+               max = MIN(size, maxlen - 1);
                goto send;
 
        default:
@@ -1686,7 +1690,7 @@ static inline void __do_send_log(struct logsrv *logsrv, int nblogger, char *pid_
                goto send;
        }
 
-       max = MIN(size, maxlen - sd_max) - 1;
+       max = MIN(size, maxlen - sd_max - 1);
 send:
        if (logsrv->addr.ss_family == AF_UNSPEC) {
                /* the target is a file descriptor or a ring buffer */