citrus_gbk2k's mbrtowc(3) wrongly checked mbstate_t, so can't convert partial character.

Issue #53 closed
Takehiko NOZAKI repo owner created an issue

this works fine:

#include <assert.h>
#include <limits.h>
#include <locale.h>
#include <string.h>
#include <wchar.h>

int
main(void) {
    mbstate_t st;
    char *s = "\x81\x30\x81\x31";
    size_t ret;

    setlocale(LC_CTYPE, "zh_CN.GB18030");

    memset(&st, 0, sizeof(st));
    ret = mbrtowc(NULL, s, sizeof(s), &st);
    assert(ret == (size_t)4);
}

but this doesn't work:

#include <assert.h>
#include <limits.h>
#include <locale.h>
#include <string.h>
#include <wchar.h>

int
main(void) {
    mbstate_t st;
    char *s = "\x81\x30\x81\x31";
    size_t ret, len, i;

    setlocale(LC_CTYPE, "zh_CN.GB18030");
    memset(&st, 0, sizeof(st));

    len = strlen(s);
    for (i = 0; i < len - 1; ++i) {
        ret = mbrtowc(NULL, s, 1, &st);
        assert(ret == (size_t)-2);
        ++s;
    }
    ret = mbrtowc(NULL, s, 1, &st);
    assert(ret == (size_t)1);
}

Comments (2)

  1. Log in to comment