Skip to content

Commit ffcc552

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 64e9142 commit ffcc552

1 file changed

Lines changed: 3 additions & 4 deletions

File tree

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

@@ -75,16 +76,15 @@ static size_t mbstowcs_nonfatal(wchar_t* restrict dest, const char* restrict src
7576
mbstate_t ps = { 0 };
7677
bool broken = false;
7778

78-
while (n > 0) {
79-
size_t ret = mbrtowc(dest, src, n, &ps);
79+
while (written < n) {
80+
size_t ret = mbrtowc(dest, src, SIZE_MAX, &ps);
8081
if (ret == (size_t)-1 || ret == (size_t)-2) {
8182
if (!broken) {
8283
broken = true;
8384
*dest++ = L'\xFFFD';
8485
written++;
8586
}
8687
src++;
87-
n--;
8888
continue;
8989
}
9090

@@ -97,7 +97,6 @@ static size_t mbstowcs_nonfatal(wchar_t* restrict dest, const char* restrict src
9797
dest++;
9898
written++;
9999
src += ret;
100-
n -= ret;
101100
}
102101

103102
return written;

0 commit comments

Comments
 (0)