]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
logger: fix --size use for stdin
authorKarel Zak <kzak@redhat.com>
Thu, 21 Oct 2021 16:47:40 +0000 (18:47 +0200)
committerKarel Zak <kzak@redhat.com>
Thu, 21 Oct 2021 16:47:40 +0000 (18:47 +0200)
The stdin version counts log header into the message size, but
for example when it reads message from argv[] it counts only message
itself.

 $ logger --stderr  --size 3 "abcd"
 <13>Oct 21 18:48:29 kzak: abc

 $ echo "abcd" | logger --stderr  --size 3
 logger: cannot allocate 18446744073709551597 bytes: Cannot allocate memory

Addresses: https://bugzilla.redhat.com/show_bug.cgi?id=2011602
Signed-off-by: Karel Zak <kzak@redhat.com>
misc-utils/logger.c

index 25ff2b9308ee6567a3683b489265c0390f1a5d19..50ae211056a953820384e8e97cfe1f41707dded5 100644 (file)
@@ -976,9 +976,7 @@ static void logger_stdin(struct logger_ctl *ctl)
         */
        int default_priority = ctl->pri;
        int last_pri = default_priority;
-       size_t max_usrmsg_size = ctl->max_message_size - strlen(ctl->hdr);
-       size_t allocated_usrmsg_size = max_usrmsg_size;
-       char *buf = xmalloc(allocated_usrmsg_size + 2 + 2);
+       char *buf = xmalloc(ctl->max_message_size + 2 + 2);
        int pri;
        int c;
        size_t i;
@@ -1006,20 +1004,13 @@ static void logger_stdin(struct logger_ctl *ctl)
 
                        if (ctl->pri != last_pri) {
                                generate_syslog_header(ctl);
-                               max_usrmsg_size = ctl->max_message_size - strlen(ctl->hdr);
-
-                               if (max_usrmsg_size > allocated_usrmsg_size) {
-                                       allocated_usrmsg_size = max_usrmsg_size;
-                                       buf = xrealloc(buf, allocated_usrmsg_size + 2 + 2);
-                               }
-
                                last_pri = ctl->pri;
                        }
                        if (c != EOF && c != '\n')
                                c = getchar();
                }
 
-               while (c != EOF && c != '\n' && i < max_usrmsg_size) {
+               while (c != EOF && c != '\n' && i < ctl->max_message_size) {
                        buf[i++] = c;
                        c = getchar();
                }