Skip to content

Commit

Permalink
Fallback on printing invalid wide string
Browse files Browse the repository at this point in the history
In case the string to be printed contains invalid wide characters, fall
back to print as ASCII string.

Closes: htop-dev#1373
  • Loading branch information
cgzones committed Jan 15, 2024
1 parent 62a39e0 commit 3c7dacb
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions RichString.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,19 @@ static inline int RichString_writeFromAscii(RichString* this, int attrs, const c

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);
if (len <= 0)
size_t ret = mbstowcs(data, data_c, len);
if (ret == 0)
return 0;
if (ret == (size_t)-1)
return RichString_writeFromAscii(this, attrs, data_c, from, len);

int newLen = from + len;
int newLen = from + ret;
RichString_setLen(this, newLen);
for (int i = from, j = 0; i < newLen; i++, j++) {
this->chptr[i] = (CharType) { .attr = attrs & 0xffffff, .chars = { (iswprint(data[j]) ? data[j] : L'\xFFFD') } };
}

return len;
return ret;
}

int RichString_appendnWideColumns(RichString* this, int attrs, const char* data_c, int len, int* columns) {
Expand Down

0 comments on commit 3c7dacb

Please sign in to comment.