From 293b7b0f11052f3b0f5ff78e01299831dfb690cd Mon Sep 17 00:00:00 2001 From: apio Date: Wed, 21 Dec 2022 17:38:19 +0100 Subject: [PATCH] TextConsole: Propagate UTF-8 decoding errors --- kernel/src/Log.cpp | 17 ++++++++++++--- kernel/src/video/TextConsole.cpp | 36 +++++++++++++++++--------------- kernel/src/video/TextConsole.h | 8 +++---- 3 files changed, 37 insertions(+), 24 deletions(-) diff --git a/kernel/src/Log.cpp b/kernel/src/Log.cpp index 7342419c..d13d9e0e 100644 --- a/kernel/src/Log.cpp +++ b/kernel/src/Log.cpp @@ -55,10 +55,21 @@ static void log_text_console(LogLevel level, const char* format, va_list origin) TextConsole::set_foreground(WHITE); // FIXME: Same as above. - pure_cstyle_format( - format, [](char c, void*) { TextConsole::putchar(c); }, nullptr, ap); + auto rc = cstyle_format( + format, [](char c, void*) -> Result { return TextConsole::putchar(c); }, nullptr, ap); - TextConsole::putchar('\n'); + if (rc.has_error()) + { + // FIXME: Find a way to do this in a simpler way (TextConsole::wprint maybe?) + const wchar_t* s = L"Invalid UTF-8 in log message"; + while (*s) + { + TextConsole::putwchar(*s); + s++; + } + } + + TextConsole::putwchar(L'\n'); TextConsole::set_background(original_background); diff --git a/kernel/src/video/TextConsole.cpp b/kernel/src/video/TextConsole.cpp index 7d124a06..abd1c139 100644 --- a/kernel/src/video/TextConsole.cpp +++ b/kernel/src/video/TextConsole.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include @@ -120,18 +121,17 @@ namespace TextConsole } } - // FIXME: Should this function propagate errors? - void putchar(char c) + Result putchar(char c) { - auto rc = utf8_decoder.feed(c); - if (rc.has_error()) - { - utf8_decoder.reset(); - return; - } + auto guard = make_scope_guard([] { utf8_decoder.reset(); }); + + auto maybe_wchar = TRY(utf8_decoder.feed(c)); + + guard.deactivate(); - auto maybe_wchar = rc.value(); if (maybe_wchar.has_value()) putwchar(maybe_wchar.value()); + + return {}; } void set_foreground(u32 color) @@ -166,23 +166,25 @@ namespace TextConsole Framebuffer::rect(0, 0, Framebuffer::width(), Framebuffer::height(), BLACK); } - void print(const char* str) + Result print(const char* str) { - while (*str) putchar(*str++); + while (*str) TRY(putchar(*str++)); + return {}; } - void println(const char* str) + Result println(const char* str) { - print(str); - putchar('\n'); + TRY(print(str)); + putwchar(L'\n'); + return {}; } - usize printf(const char* format, ...) + Result printf(const char* format, ...) { va_list ap; va_start(ap, format); - auto rc = pure_cstyle_format( - format, [](char c, void*) { putchar(c); }, nullptr, ap); + auto rc = TRY(cstyle_format( + format, [](char c, void*) -> Result { return putchar(c); }, nullptr, ap)); va_end(ap); return rc; } diff --git a/kernel/src/video/TextConsole.h b/kernel/src/video/TextConsole.h index 2c884262..464f34d9 100644 --- a/kernel/src/video/TextConsole.h +++ b/kernel/src/video/TextConsole.h @@ -6,14 +6,14 @@ namespace TextConsole { void clear(); - void putchar(char c); + Result putchar(char c); void putwchar(wchar_t c); void set_foreground(u32 color); void set_background(u32 color); u32 foreground(); u32 background(); void move_to(u32 x, u32 y); - void print(const char* str); - void println(const char* str); - usize printf(const char* format, ...) _format(1, 2); + Result print(const char* str); + Result println(const char* str); + Result printf(const char* format, ...) _format(1, 2); } \ No newline at end of file