summaryrefslogtreecommitdiffstats
path: root/RichString.c
diff options
context:
space:
mode:
authorChristian Göttsche <cgzones@googlemail.com>2024-01-15 18:48:49 +0100
committercgzones <cgzones@googlemail.com>2024-01-20 11:53:25 +0100
commitdb65f175ab44320911ec5da01ab9dcd858dda9c0 (patch)
tree7a678343c61f61f16339f36de67f8c53f48de55c /RichString.c
parente401c8fed95a212fde43b295ea08c2ae81429507 (diff)
Handle invalid multi-byte strings gracefully
In case the string to be printed contains invalid wide characters, continue to translate after the invalid character instead of aborting. Closes: #1373
Diffstat (limited to 'RichString.c')
-rw-r--r--RichString.c41
1 files changed, 37 insertions, 4 deletions
diff --git a/RichString.c b/RichString.c
index ed852ada..ea904817 100644
--- a/RichString.c
+++ b/RichString.c
@@ -54,9 +54,42 @@ void RichString_rewind(RichString* this, int count) {
#ifdef HAVE_LIBNCURSESW
+static size_t mbstowcs_nonfatal(wchar_t* dest, const char* src, size_t n) {
+ size_t written = 0;
+ mbstate_t ps = { 0 };
+ bool broken = false;
+
+ while (n > 0) {
+ size_t ret = mbrtowc(dest, src, n, &ps);
+ if (ret == (size_t)-1 || ret == (size_t)-2) {
+ if (!broken) {
+ broken = true;
+ *dest++ = L'\xFFFD';
+ written++;
+ }
+ src++;
+ n--;
+ continue;
+ }
+
+ broken = false;
+
+ if (ret == 0) {
+ break;
+ }
+
+ dest++;
+ written++;
+ src += ret;
+ n -= ret;
+ }
+
+ return written;
+}
+
static inline int RichString_writeFromWide(RichString* this, int attrs, const char* data_c, int from, int len) {
- wchar_t data[len + 1];
- len = mbstowcs(data, data_c, len);
+ wchar_t data[len];
+ len = mbstowcs_nonfatal(data, data_c, len);
if (len <= 0)
return 0;
@@ -70,8 +103,8 @@ static inline int RichString_writeFromWide(RichString* this, int attrs, const ch
}
int RichString_appendnWideColumns(RichString* this, int attrs, const char* data_c, int len, int* columns) {
- wchar_t data[len + 1];
- len = mbstowcs(data, data_c, len);
+ wchar_t data[len];
+ len = mbstowcs_nonfatal(data, data_c, len);
if (len <= 0)
return 0;

© 2014-2024 Faster IT GmbH | imprint | privacy policy