From d27a23a7d6ad3f5b35967cfecf64a4ebfa4b31c6 Mon Sep 17 00:00:00 2001
From: Přemysl Janouch
Date: Sat, 11 Jul 2015 04:40:48 +0200
Subject: degesch: sanitize terminal output
---
degesch.c | 37 ++++++++++++++++++++++---------------
1 file changed, 22 insertions(+), 15 deletions(-)
diff --git a/degesch.c b/degesch.c
index 5d5108e..a6826d6 100644
--- a/degesch.c
+++ b/degesch.c
@@ -2560,6 +2560,23 @@ formatter_flush_attr
}
}
+static void
+formatter_flush_text (struct app_context *ctx, const char *text, FILE *stream)
+{
+ struct str sanitized;
+ str_init (&sanitized);
+
+ // Throw away any potentially harmful control characters
+ char *term = iconv_xstrdup (ctx->term_from_utf8, (char *) text, -1, NULL);
+ for (char *p = term; *p; p++)
+ if (!strchr ("\a\b\x1b", *p))
+ str_append_c (&sanitized, *p);
+ free (term);
+
+ fputs (sanitized.str, stream);
+ str_free (&sanitized);
+}
+
static void
formatter_flush (struct formatter *self, FILE *stream)
{
@@ -2579,23 +2596,13 @@ formatter_flush (struct formatter *self, FILE *stream)
int attribute_ignore = 0;
LIST_FOR_EACH (struct formatter_item, iter, self->items)
{
- switch (iter->type)
- {
- char *term;
- case FORMATTER_ITEM_TEXT:
- term = iconv_xstrdup
- (self->ctx->term_from_utf8, iter->text, -1, NULL);
- fputs (term, stream);
- free (term);
- break;
- case FORMATTER_ITEM_IGNORE_ATTR:
+ if (iter->type == FORMATTER_ITEM_TEXT)
+ formatter_flush_text (self->ctx, iter->text, stream);
+ else if (iter->type == FORMATTER_ITEM_IGNORE_ATTR)
attribute_ignore += iter->attribute;
- break;
- default:
- if (attribute_ignore <= 0
- && !formatter_flush_attr (&state, iter))
+ else if (attribute_ignore <= 0
+ && !formatter_flush_attr (&state, iter))
hard_assert (!"unhandled formatter item type");
- }
}
attribute_printer_reset (&state);
}
--
cgit v1.2.3-70-g09d2