diff --git a/src/demo/witherworm.c b/src/demo/witherworm.c index 8a8cb1eed..aab4f9ba2 100644 --- a/src/demo/witherworm.c +++ b/src/demo/witherworm.c @@ -483,7 +483,9 @@ int witherworm_demo(struct notcurses* nc){ break; } wchar_t wcs; - int eaten = mbtowc(&wcs, &(*s)[idx], MB_CUR_MAX + 1); + mbstate_t mbstate; + memset(&mbstate, 0, sizeof(mbstate)); + int eaten = mbrtowc(&wcs, &(*s)[idx], MB_CUR_MAX + 1, &mbstate); if(eaten < 0){ return -1; } diff --git a/src/lib/egcpool.h b/src/lib/egcpool.h index b77b813b9..21969a3a7 100644 --- a/src/lib/egcpool.h +++ b/src/lib/egcpool.h @@ -59,7 +59,7 @@ egcpool_grow(egcpool* pool, size_t len){ } // Eat an EGC from the UTF-8 string input. This consists of extracting a -// multibyte via mbtowc, then continuing to extract any which have zero +// multibyte via mbrtowc, then continuing to extract any which have zero // width until hitting another spacing character or a NUL terminator. Writes // the number of columns occupied to '*colcount'. Returns the number of bytes // consumed, not including any NUL terminator. Note that neither the number @@ -193,9 +193,11 @@ egcpool_check_validity(const egcpool* pool, int offset){ fprintf(stderr, "Bad offset 0x%06x: empty\n", offset); return false; } + mbstate_t mbstate; + memset(&mbstate, 0, sizeof(mbstate)); do{ wchar_t wcs; - int r = mbtowc(&wcs, egc, strlen(egc)); + int r = mbrtowc(&wcs, egc, strlen(egc), &mbstate); if(r < 0){ fprintf(stderr, "Invalid UTF8 at offset 0x%06x, len %zu [%s]\n", offset, strlen(egc), strerror(errno)); diff --git a/src/lib/input.c b/src/lib/input.c index a59c0627c..e247b0b61 100644 --- a/src/lib/input.c +++ b/src/lib/input.c @@ -251,8 +251,12 @@ handle_getc(notcurses* nc, int kpress, ncinput* ni){ } cpoint[cpointlen] = '\0'; wchar_t w; + mbstate_t mbstate; + memset(&mbstate, 0, sizeof(mbstate)); // FIXME how the hell does this work with 16-bit wchar_t? - if(mbtowc(&w, cpoint, cpointlen) < 0){ + size_t r; + if((r = mbrtowc(&w, cpoint, cpointlen, &mbstate)) == (size_t)-1 || + r == (size_t)-2){ return (wchar_t)-1; } return w;