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