Hi,

We have the same limit for syslog lines in libc, kernel, syslogd
now.  Do we want a common constant?  Is the name LOG_MAXLINE suitable
as a global name?  Most constants in sys/syslog.h start with LOG_.

While there I renamed TBUF_LEN and FMT_LEN to _SIZE as they contain
a NUL byte.  Change FMT_SIZE to 1024+1 for consistency.  And it
does not make sense to loop over the format string if there is no
output space left.

ok?

bluhm

Index: sys/kern/subr_log.c
===================================================================
RCS file: /data/mirror/openbsd/cvs/src/sys/kern/subr_log.c,v
retrieving revision 1.51
diff -u -p -r1.51 subr_log.c
--- sys/kern/subr_log.c 18 Jul 2017 22:22:19 -0000      1.51
+++ sys/kern/subr_log.c 19 Jul 2017 00:08:25 -0000
@@ -418,8 +418,8 @@ dosendsyslog(struct proc *p, const char 
        size_t i, len;
        int error;
 
-       if (nbyte > 8192)
-               nbyte = 8192;
+       if (nbyte > LOG_MAXLINE)
+               nbyte = LOG_MAXLINE;
 
        /* Global variable syslogf may change during sleep, use local copy. */
        fp = syslogf;
Index: sys/sys/syslog.h
===================================================================
RCS file: /data/mirror/openbsd/cvs/src/sys/sys/syslog.h,v
retrieving revision 1.15
diff -u -p -r1.15 syslog.h
--- sys/sys/syslog.h    14 Jul 2014 03:52:04 -0000      1.15
+++ sys/sys/syslog.h    19 Jul 2017 00:05:05 -0000
@@ -39,6 +39,8 @@
 
 #define        LIOCSFD         _IOW('l', 127, int)     /* set sendsyslog() fd 
*/
 
+#define LOG_MAXLINE    8192                    /* maximum line length */
+
 /*
  * priorities/facilities are encoded into a single 32-bit quantity, where the
  * bottom 3 bits are the priority (0-7) and the top 28 bits are the facility
Index: lib/libc/gen/syslog_r.c
===================================================================
RCS file: /data/mirror/openbsd/cvs/src/lib/libc/gen/syslog_r.c,v
retrieving revision 1.16
diff -u -p -r1.16 syslog_r.c
--- lib/libc/gen/syslog_r.c     19 Oct 2016 16:09:24 -0000      1.16
+++ lib/libc/gen/syslog_r.c     18 Jul 2017 23:50:13 -0000
@@ -73,9 +73,9 @@ __vsyslog_r(int pri, struct syslog_data 
        int cnt;
        char ch, *p, *t;
        int saved_errno;
-#define        TBUF_LEN        (8192+1)
-#define        FMT_LEN         1024
-       char *conp = NULL, *stdp = NULL, tbuf[TBUF_LEN], fmt_cpy[FMT_LEN];
+#define        TBUF_SIZE       (LOG_MAXLINE+1)
+#define        FMT_SIZE        (1024+1)
+       char *conp = NULL, *stdp = NULL, tbuf[TBUF_SIZE], fmt_cpy[FMT_SIZE];
        int tbuf_left, fmt_left, prlen;
 
 #define        INTERNALLOG     LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID
@@ -98,7 +98,7 @@ __vsyslog_r(int pri, struct syslog_data 
                pri |= data->log_fac;
 
        p = tbuf;
-       tbuf_left = TBUF_LEN;
+       tbuf_left = TBUF_SIZE;
 
 #define        DEC()   \
        do {                                    \
@@ -138,7 +138,9 @@ __vsyslog_r(int pri, struct syslog_data 
                }
        }
 
-       for (t = fmt_cpy, fmt_left = FMT_LEN; (ch = *fmt); ++fmt) {
+       for (t = fmt_cpy, fmt_left = FMT_SIZE;
+           (ch = *fmt) != '\0' && fmt_left > 1;
+           ++fmt) {
                if (ch == '%' && fmt[1] == 'm') {
                        char ebuf[NL_TEXTMAX];
 
@@ -152,15 +154,13 @@ __vsyslog_r(int pri, struct syslog_data 
                        t += prlen;
                        fmt_left -= prlen;
                } else if (ch == '%' && fmt[1] == '%' && fmt_left > 2) {
+                       ++fmt;
                        *t++ = '%';
                        *t++ = '%';
-                       fmt++;
                        fmt_left -= 2;
                } else {
-                       if (fmt_left > 1) {
-                               *t++ = ch;
-                               fmt_left--;
-                       }
+                       *t++ = ch;
+                       fmt_left--;
                }
        }
        *t = '\0';
Index: usr.sbin/syslogd/syslogd.c
===================================================================
RCS file: /data/mirror/openbsd/cvs/src/usr.sbin/syslogd/syslogd.c,v
retrieving revision 1.244
diff -u -p -r1.244 syslogd.c
--- usr.sbin/syslogd/syslogd.c  28 Apr 2017 14:52:13 -0000      1.244
+++ usr.sbin/syslogd/syslogd.c  19 Jul 2017 00:03:27 -0000
@@ -54,7 +54,7 @@
  */
 
 #define MAX_UDPMSG     1180            /* maximum UDP send size */
-#define MIN_MEMBUF     (MAXLINE * 4)   /* Minimum memory buffer size */
+#define MIN_MEMBUF     (LOG_MAXLINE * 4) /* Minimum memory buffer size */
 #define MAX_MEMBUF     (256 * 1024)    /* Maximum memory buffer size */
 #define MAX_MEMBUF_NAME        64              /* Max length of membuf log 
name */
 #define MAX_TCPBUF     (256 * 1024)    /* Maximum tcp event buffer size */
@@ -492,8 +492,8 @@ main(int argc, char *argv[])
 
        /* Reserve space for kernel message buffer plus buffer full message. */
        linesize = getmsgbufsize() + 64;
-       if (linesize < MAXLINE)
-               linesize = MAXLINE;
+       if (linesize < LOG_MAXLINE)
+               linesize = LOG_MAXLINE;
        linesize++;
        if ((linebuf = malloc(linesize)) == NULL)
                fatal("allocate line buffer");
@@ -1011,7 +1011,8 @@ udp_readcb(int fd, short event, void *ar
        ssize_t                  n;
 
        salen = sizeof(sa);
-       n = recvfrom(fd, linebuf, MAXLINE, 0, (struct sockaddr *)&sa, &salen);
+       n = recvfrom(fd, linebuf, LOG_MAXLINE, 0, (struct sockaddr *)&sa,
+           &salen);
        if (n > 0) {
                char     resolve[NI_MAXHOST];
 
@@ -1031,7 +1032,8 @@ unix_readcb(int fd, short event, void *a
        ssize_t                  n;
 
        salen = sizeof(sa);
-       n = recvfrom(fd, linebuf, MAXLINE, 0, (struct sockaddr *)&sa, &salen);
+       n = recvfrom(fd, linebuf, LOG_MAXLINE, 0, (struct sockaddr *)&sa,
+           &salen);
        if (n > 0) {
                linebuf[n] = '\0';
                printline(LocalHostName, linebuf);
@@ -1265,15 +1267,15 @@ tcp_readcb(struct bufferevent *bufev, vo
                if (len > 0 && msg[len-1] == '\n')
                        msg[len-1] = '\0';
                if (len == 0 || msg[len-1] != '\0') {
-                       memcpy(linebuf, msg, MINIMUM(len, MAXLINE));
-                       linebuf[MINIMUM(len, MAXLINE)] = '\0';
+                       memcpy(linebuf, msg, MINIMUM(len, LOG_MAXLINE));
+                       linebuf[MINIMUM(len, LOG_MAXLINE)] = '\0';
                        msg = linebuf;
                }
                printline(p->p_hostname, msg);
                evbuffer_drain(bufev->input, len);
        }
        /* Maximum frame has 5 digits, 1 space, MAXLINE chars, 1 new line. */
-       if (EVBUFFER_LENGTH(bufev->input) >= 5 + 1 + MAXLINE + 1) {
+       if (EVBUFFER_LENGTH(bufev->input) >= 5 + 1 + LOG_MAXLINE + 1) {
                log_debug(", use %zu bytes", EVBUFFER_LENGTH(bufev->input));
                printline(p->p_hostname, EVBUFFER_DATA(bufev->input));
                evbuffer_drain(bufev->input, -1);
@@ -1558,7 +1560,7 @@ void
 printline(char *hname, char *msg)
 {
        int pri;
-       char *p, *q, line[MAXLINE + 4 + 1];  /* message, encoding, NUL */
+       char *p, *q, line[LOG_MAXLINE + 4 + 1];  /* message, encoding, NUL */
 
        /* test for special codes */
        pri = DEFUPRI;
@@ -1575,13 +1577,13 @@ printline(char *hname, char *msg)
        if (LOG_FAC(pri) == LOG_KERN)
                pri = LOG_USER | LOG_PRI(pri);
 
-       for (q = line; *p && q < &line[MAXLINE]; p++) {
+       for (q = line; *p && q < &line[LOG_MAXLINE]; p++) {
                if (*p == '\n')
                        *q++ = ' ';
                else
                        q = vis(q, *p, 0, 0);
        }
-       line[MAXLINE] = *q = '\0';
+       line[LOG_MAXLINE] = *q = '\0';
 
        logline(pri, 0, hname, line);
 }
@@ -1593,7 +1595,7 @@ void
 printsys(char *msg)
 {
        int c, pri, flags;
-       char *lp, *p, *q, line[MAXLINE + 1];
+       char *lp, *p, *q, line[LOG_MAXLINE + 1];
        size_t prilen;
 
        (void)snprintf(line, sizeof line, "%s: ", _PATH_UNIX);
@@ -1855,7 +1857,7 @@ fprintlog(struct filed *f, int flags, ch
        struct iovec iov[6];
        struct iovec *v;
        int l, retryonce;
-       char line[MAXLINE + 1], repbuf[80], greetings[500];
+       char line[LOG_MAXLINE + 1], repbuf[80], greetings[500];
 
        v = iov;
        if (f->f_type == F_WALL) {
@@ -2496,7 +2498,7 @@ cfline(char *line, char *progblock, char
        int i, pri;
        size_t rb_len;
        char *bp, *p, *q, *proto, *host, *port, *ipproto;
-       char buf[MAXLINE];
+       char buf[LOG_MAXLINE];
        struct filed *xf, *f, *d;
        struct timeval to;
 
@@ -2946,7 +2948,7 @@ unix_socket(char *path, int type, mode_t
                return (-1);
        }
 
-       optval = MAXLINE + PATH_MAX;
+       optval = LOG_MAXLINE + PATH_MAX;
        if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &optval, sizeof(optval))
            == -1)
                log_warn("setsockopt unix \"%s\"", path);
@@ -2964,7 +2966,7 @@ double_sockbuf(int fd, int optname)
        if (getsockopt(fd, SOL_SOCKET, optname, &oldsize, &len) == -1)
                log_warn("getsockopt bufsize");
        len = sizeof(newsize);
-       newsize =  MAXLINE + 128;  /* data + control */
+       newsize =  LOG_MAXLINE + 128;  /* data + control */
        /* allow 8 full length messages */
        for (i = 0; i < 4; i++, newsize *= 2) {
                if (newsize <= oldsize)
Index: usr.sbin/syslogd/syslogd.h
===================================================================
RCS file: /data/mirror/openbsd/cvs/src/usr.sbin/syslogd/syslogd.h,v
retrieving revision 1.30
diff -u -p -r1.30 syslogd.h
--- usr.sbin/syslogd/syslogd.h  5 Apr 2017 22:15:35 -0000       1.30
+++ usr.sbin/syslogd/syslogd.h  19 Jul 2017 00:03:27 -0000
@@ -48,7 +48,6 @@ extern int nunix;
 extern char **path_unix;
 extern char *path_ctlsock;
 
-#define MAXLINE                8192            /* maximum line length */
 #define ERRBUFSIZE     256
 void vlogmsg(int pri, const char *, const char *, va_list);
 __dead void die(int);
Index: usr.sbin/syslogd/ttymsg.c
===================================================================
RCS file: /data/mirror/openbsd/cvs/src/usr.sbin/syslogd/ttymsg.c,v
retrieving revision 1.15
diff -u -p -r1.15 ttymsg.c
--- usr.sbin/syslogd/ttymsg.c   5 Apr 2017 21:55:31 -0000       1.15
+++ usr.sbin/syslogd/ttymsg.c   19 Jul 2017 00:03:27 -0000
@@ -31,6 +31,7 @@
  */
 
 #include <sys/stat.h>
+#include <sys/syslog.h>
 
 #include <dirent.h>
 #include <errno.h>
@@ -52,7 +53,7 @@
 struct tty_delay {
        struct event     td_event;
        size_t           td_length;
-       char             td_line[MAXLINE];
+       char             td_line[LOG_MAXLINE];
 };
 int tty_delayed = 0;
 void ttycb(int, short, void *);
@@ -148,8 +149,8 @@ ttymsg(struct iovec *iov, int iovcnt, ch
                                break;
                        }
                        td->td_length = 0;
-                       if (left > MAXLINE)
-                               left = MAXLINE;
+                       if (left > LOG_MAXLINE)
+                               left = LOG_MAXLINE;
                        while (iovcnt && left) {
                                if (iov->iov_len > left)
                                        iov->iov_len = left;

Reply via email to