e377cc
From 7cc5bcfcb2340266a6b42370c9c4c02d8a325d5f Mon Sep 17 00:00:00 2001
e377cc
From: Karel Zak <kzak@redhat.com>
e377cc
Date: Thu, 21 Oct 2021 18:47:40 +0200
e377cc
Subject: [PATCH 67/74] logger: fix --size use for stdin
e377cc
MIME-Version: 1.0
e377cc
Content-Type: text/plain; charset=UTF-8
e377cc
Content-Transfer-Encoding: 8bit
e377cc
e377cc
The stdin version counts log header into the message size, but
e377cc
for example when it reads message from argv[] it counts only message
e377cc
itself.
e377cc
e377cc
 $ logger --stderr  --size 3 "abcd"
e377cc
 <13>Oct 21 18:48:29 kzak: abc
e377cc
e377cc
 $ echo "abcd" | logger --stderr  --size 3
e377cc
 logger: cannot allocate 18446744073709551597 bytes: Cannot allocate memory
e377cc
e377cc
Addresses: https://bugzilla.redhat.com/show_bug.cgi?id=2011602
e377cc
Upstream: http://github.com/util-linux/util-linux/commit/58e4ee082bca100034791a4a74481f263bb30a25
e377cc
Signed-off-by: Karel Zak <kzak@redhat.com>
e377cc
---
e377cc
 misc-utils/logger.c | 16 ++++------------
e377cc
 1 file changed, 4 insertions(+), 12 deletions(-)
e377cc
e377cc
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
e377cc
index ebdc56ec2..c20ef05f1 100644
e377cc
--- a/misc-utils/logger.c
e377cc
+++ b/misc-utils/logger.c
e377cc
@@ -957,11 +957,9 @@ static void logger_stdin(struct logger_ctl *ctl)
e377cc
 	 * update header timestamps and to reflect possible priority changes.
e377cc
 	 * The initial header is generated by logger_open().
e377cc
 	 */
e377cc
-	int has_header = 1;
e377cc
 	int default_priority = ctl->pri;
e377cc
 	int last_pri = default_priority;
e377cc
-	size_t max_usrmsg_size = ctl->max_message_size - strlen(ctl->hdr);
e377cc
-	char *const buf = xmalloc(max_usrmsg_size + 2 + 2);
e377cc
+	char *buf = xmalloc(ctl->max_message_size + 2 + 2);
e377cc
 	int pri;
e377cc
 	int c;
e377cc
 	size_t i;
e377cc
@@ -988,27 +986,21 @@ static void logger_stdin(struct logger_ctl *ctl)
e377cc
 				ctl->pri = default_priority;
e377cc
 
e377cc
 			if (ctl->pri != last_pri) {
e377cc
-				has_header = 0;
e377cc
-				max_usrmsg_size =
e377cc
-				    ctl->max_message_size - strlen(ctl->hdr);
e377cc
+				generate_syslog_header(ctl);
e377cc
 				last_pri = ctl->pri;
e377cc
 			}
e377cc
 			if (c != EOF && c != '\n')
e377cc
 				c = getchar();
e377cc
 		}
e377cc
 
e377cc
-		while (c != EOF && c != '\n' && i < max_usrmsg_size) {
e377cc
+		while (c != EOF && c != '\n' && i < ctl->max_message_size) {
e377cc
 			buf[i++] = c;
e377cc
 			c = getchar();
e377cc
 		}
e377cc
 		buf[i] = '\0';
e377cc
 
e377cc
-		if (i > 0 || !ctl->skip_empty_lines) {
e377cc
-			if (!has_header)
e377cc
-				generate_syslog_header(ctl);
e377cc
+		if (i > 0 || !ctl->skip_empty_lines)
e377cc
 			write_output(ctl, buf);
e377cc
-			has_header = 0;
e377cc
-		}
e377cc
 
e377cc
 		if (c == '\n')	/* discard line terminator */
e377cc
 			c = getchar();
e377cc
-- 
e377cc
2.31.1
e377cc