Skip to content

Commit a547c87

Browse files
committed
Fix mbstowcs_nonfatal() that might convert string shorter than desired
The "n" parameter of mbstowcs_nonfatal() refers to number of wide characters. But the logic seemed to be confused with "n" parameter of mbrtowc() (the number of bytes). Signed-off-by: Kang-Che Sung <explorer09@gmail.com>
1 parent 4e6a781 commit a547c87

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

RichString.c

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ in the source distribution for its full text.
1212
#include <assert.h>
1313
#include <ctype.h>
1414
#include <limits.h> // IWYU pragma: keep
15+
#include <stdint.h>
1516
#include <stdlib.h>
1617
#include <string.h>
1718

@@ -69,16 +70,15 @@ static size_t mbstowcs_nonfatal(wchar_t* restrict dest, const char* restrict src
6970
mbstate_t ps = { 0 };
7071
bool broken = false;
7172

72-
while (n > 0) {
73-
size_t ret = mbrtowc(dest, src, n, &ps);
73+
while (written < n) {
74+
size_t ret = mbrtowc(dest, src, SIZE_MAX, &ps);
7475
if (ret == (size_t)-1 || ret == (size_t)-2) {
7576
if (!broken) {
7677
broken = true;
7778
*dest++ = L'\xFFFD';
7879
written++;
7980
}
8081
src++;
81-
n--;
8282
continue;
8383
}
8484

@@ -91,7 +91,6 @@ static size_t mbstowcs_nonfatal(wchar_t* restrict dest, const char* restrict src
9191
dest++;
9292
written++;
9393
src += ret;
94-
n -= ret;
9594
}
9695

9796
return written;

0 commit comments

Comments
 (0)