From 0c677d093eaf349e11fd74fc09cebd16d8909920 Mon Sep 17 00:00:00 2001 From: czapek1337 Date: Fri, 28 Jan 2022 23:51:34 +0100 Subject: [PATCH] console,editor: Filter out non-printable characters --- stage23/lib/libc.h | 4 ++++ stage23/lib/libc.s2.c | 8 ++++++++ stage23/lib/readline.c | 2 +- stage23/menu.c | 12 +++++++----- 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/stage23/lib/libc.h b/stage23/lib/libc.h index 7f7d91bb..7e8add9d 100644 --- a/stage23/lib/libc.h +++ b/stage23/lib/libc.h @@ -2,6 +2,10 @@ #define __LIB__LIBC_H__ #include +#include + +bool isprint(int c); +bool isspace(int c); int toupper(int c); int tolower(int c); diff --git a/stage23/lib/libc.s2.c b/stage23/lib/libc.s2.c index 24af2bec..1b818135 100644 --- a/stage23/lib/libc.s2.c +++ b/stage23/lib/libc.s2.c @@ -4,6 +4,14 @@ #include #include +bool isprint(int c) { + return c >= ' ' && c <= '~'; +} + +bool isspace(int c) { + return (c >= '\t' && c <= 0xD) || c == ' '; +} + int toupper(int c) { if (c >= 'a' && c <= 'z') { return c - 0x20; diff --git a/stage23/lib/readline.c b/stage23/lib/readline.c index c810dab8..22415716 100644 --- a/stage23/lib/readline.c +++ b/stage23/lib/readline.c @@ -317,7 +317,7 @@ void readline(const char *orig_str, char *buf, size_t limit) { term_write((uintptr_t)"\n", 1); goto out; default: { - if (strlen(buf) < limit - 1) { + if (strlen(buf) < limit - 1 && isprint(c)) { for (size_t j = strlen(buf); ; j--) { buf[j+1] = buf[j]; if (j == i) diff --git a/stage23/menu.c b/stage23/menu.c index 6430b0e6..f4663340 100644 --- a/stage23/menu.c +++ b/stage23/menu.c @@ -463,12 +463,14 @@ refresh: return NULL; default: if (strlen(buffer) < EDITOR_MAX_BUFFER_SIZE - 1) { - for (size_t i = strlen(buffer); ; i--) { - buffer[i+1] = buffer[i]; - if (i == cursor_offset) - break; + if (isprint(c) || c == '\n') { + for (size_t i = strlen(buffer); ; i--) { + buffer[i+1] = buffer[i]; + if (i == cursor_offset) + break; + } + buffer[cursor_offset++] = c; } - buffer[cursor_offset++] = c; } else { display_overflow_error = true; }