input: properly terminate asap when handling multibyte #1188

This commit is contained in:
nick black 2020-12-06 13:09:23 -05:00
parent cc5f9f0cb7
commit 39cc06a5f7
No known key found for this signature in database
GPG Key ID: 5F43400C21CBFACC

View File

@ -238,28 +238,28 @@ handle_getc(ncinputlayer* nc, int kpress, ncinput* ni, int leftmargin, int topma
if(kpress < 0x80){ if(kpress < 0x80){
return kpress; return kpress;
} }
char cpoint[MB_CUR_MAX]; char cpoint[MB_CUR_MAX + 1];
memset(cpoint, 0, sizeof(cpoint));
size_t cpointlen = 0; size_t cpointlen = 0;
cpoint[cpointlen] = kpress; cpoint[cpointlen] = kpress;
// FIXME need to stop as soon as we have a full codepoint urk wchar_t w;
while(++cpointlen < (size_t)MB_CUR_MAX - 1 && nc->inputbuf_occupied){ mbstate_t mbstate;
while(++cpointlen <= (size_t)MB_CUR_MAX && nc->inputbuf_occupied){
int candidate = pop_input_keypress(nc); int candidate = pop_input_keypress(nc);
if(candidate < 0x80){ if(candidate < 0x80){
unpop_keypress(nc, candidate); unpop_keypress(nc, candidate);
} }
cpoint[cpointlen] = candidate; cpoint[cpointlen] = candidate;
} //fprintf(stderr, "CANDIDATE: %d cpointlen: %zu cpoint: %d\n", candidate, cpointlen, cpoint[cpointlen]);
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? // FIXME how the hell does this work with 16-bit wchar_t?
memset(&mbstate, 0, sizeof(mbstate));
size_t r; size_t r;
if((r = mbrtowc(&w, cpoint, cpointlen, &mbstate)) == (size_t)-1 || if((r = mbrtowc(&w, cpoint, cpointlen + 1, &mbstate)) != (size_t)-1 &&
r == (size_t)-2){ r != (size_t)-2){
return (wchar_t)-1;
}
return w; return w;
}
}
return -1;
} }
// blocks up through ts (infinite with NULL ts), returning number of events // blocks up through ts (infinite with NULL ts), returning number of events