From e966df071cba720318a3b87d4be38b41edfba7a6 Mon Sep 17 00:00:00 2001 From: Simon Tatham Date: Sat, 13 Oct 2018 17:23:29 +0100 Subject: [PATCH] Avoid Event Log entries with newlines in. When logging an SSH_MSG_DISCONNECT, the log message has newlines in, because it's also displayed in the GUI dialog box or on Plink's standard error, where that makes some sense. But in the Event Log, all messages should be one-liners: anything else makes the GUI list boxes go weird, and also breaks convenient parsability of packet lot files. So we turn newlines into spaces for Event Log purposes, which is conveniently easy now that Event Log entries always go through logging.c first. --- logging.c | 40 +++++++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/logging.c b/logging.c index eb1aeec2..8f46500d 100644 --- a/logging.c +++ b/logging.c @@ -209,14 +209,8 @@ void logtraffic(LogContext *ctx, unsigned char c, int logmode) } } -/* - * Log an Event Log entry. Used in SSH packet logging mode, to copy - * the Event Log entries into the same log file as the packet data. - */ -void logevent(LogContext *ctx, const char *event) +static void logevent_internal(LogContext *ctx, const char *event) { - if (!ctx) - return; if (ctx->logtype == LGTYP_PACKETS || ctx->logtype == LGTYP_SSHRAW) { logprintf(ctx, "Event Log: %s\r\n", event); logflush(ctx); @@ -224,6 +218,38 @@ void logevent(LogContext *ctx, const char *event) lp_eventlog(ctx->lp, event); } +void logevent(LogContext *ctx, const char *event) +{ + if (!ctx) + return; + + /* + * Replace newlines in Event Log messages with spaces. (Sometimes + * the same message string is reused for the Event Log and a GUI + * dialog box; newlines are sometimes appropriate in the latter, + * but never in the former.) + */ + if (strchr(event, '\n') || strchr(event, '\r')) { + char *dup = dupstr(event); + char *p = dup, *q = dup; + while (*p) { + if (*p == '\r' || *p == '\n') { + do { + p++; + } while (*p == '\r' || *p == '\n'); + *q++ = ' '; + } else { + *q++ = *p++; + } + } + *q = '\0'; + logevent_internal(ctx, dup); + sfree(dup); + } else { + logevent_internal(ctx, event); + } +} + void logevent_and_free(LogContext *ctx, char *event) { logevent(ctx, event);