On Fri, 17 Nov 2017 10:20:49 -0700, "Todd C. Miller" wrote:

> I've done a brief survey using the test program at the end of
> this message.  Here are the results:

Here's the missing test program.  It compares how mbrtowc() and
snprintf() treat an invalid UTF-8 sequence.  I chose a simple one.

 - todd

#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>

int main(int argc, char *argv[])
{
    const char *fmt = "\xc0 ";
    char buf[1024];
    wchar_t wc;
    mbstate_t ps;
    size_t len;
    int ret;

    setlocale(LC_ALL, "en_US.UTF-8");

    memset(&ps, 0, sizeof(ps));
    len = mbrtowc(&wc, fmt, MB_CUR_MAX, &ps);
    if (len == (size_t)-1 || len == (size_t)-2)
        printf("mbrtowc fail (expected)\n");
    else
        printf("mbrtowc OK, len %zu (unexpected)\n", len);

    ret = snprintf(buf, sizeof(buf), "\xc0");
    if ((size_t)ret >= sizeof(buf))
        printf("printf fail (unexpected)\n");
    else
        printf("printf OK, ret %d (expected)\n", ret);
}

Reply via email to