From 7cc5bcfcb2340266a6b42370c9c4c02d8a325d5f Mon Sep 17 00:00:00 2001
From: Karel Zak <kzak@redhat.com>
Date: Thu, 21 Oct 2021 18:47:40 +0200
Subject: [PATCH 67/74] logger: fix --size use for stdin
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
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
Upstream: http://github.com/util-linux/util-linux/commit/58e4ee082bca100034791a4a74481f263bb30a25
Signed-off-by: Karel Zak <kzak@redhat.com>
---
misc-utils/logger.c | 16 ++++------------
1 file changed, 4 insertions(+), 12 deletions(-)
diff --git a/misc-utils/logger.c b/misc-utils/logger.c
index ebdc56ec2..c20ef05f1 100644
--- a/misc-utils/logger.c
+++ b/misc-utils/logger.c
@@ -957,11 +957,9 @@ static void logger_stdin(struct logger_ctl *ctl)
* update header timestamps and to reflect possible priority changes.
* The initial header is generated by logger_open().
*/
- int has_header = 1;
int default_priority = ctl->pri;
int last_pri = default_priority;
- size_t max_usrmsg_size = ctl->max_message_size - strlen(ctl->hdr);
- char *const buf = xmalloc(max_usrmsg_size + 2 + 2);
+ char *buf = xmalloc(ctl->max_message_size + 2 + 2);
int pri;
int c;
size_t i;
@@ -988,27 +986,21 @@ static void logger_stdin(struct logger_ctl *ctl)
ctl->pri = default_priority;
if (ctl->pri != last_pri) {
- has_header = 0;
- max_usrmsg_size =
- ctl->max_message_size - strlen(ctl->hdr);
+ generate_syslog_header(ctl);
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();
}
buf[i] = '\0';
- if (i > 0 || !ctl->skip_empty_lines) {
- if (!has_header)
- generate_syslog_header(ctl);
+ if (i > 0 || !ctl->skip_empty_lines)
write_output(ctl, buf);
- has_header = 0;
- }
if (c == '\n') /* discard line terminator */
c = getchar();
--
2.31.1