jinput: fix out-of-bounds bug in weird cursor positions

This commit is contained in:
Lephenixnoir 2022-11-19 18:53:43 +01:00
parent 7c0e8257f7
commit 5b591a6fb4
No known key found for this signature in database
GPG key ID: 1BBA026E13FC0495
2 changed files with 5 additions and 4 deletions

View file

@ -18,7 +18,7 @@
an indicator displays the status of modifier keys. an indicator displays the status of modifier keys.
The edition rules support both the OS' native one-key-at-time input system, The edition rules support both the OS' native one-key-at-time input system,
and the usual computer modifer-keys-held method. and the usual computer modifier-keys-held method.
* The normal insertion mode is used by default. * The normal insertion mode is used by default.
* When pressing SHIFT or ALPHA in combination with a key (without releasing * When pressing SHIFT or ALPHA in combination with a key (without releasing

View file

@ -85,9 +85,9 @@ void jinput_set_prompt(jinput *i, char const *prompt)
static void insert_str(jinput *i, char const *str, size_t n) static void insert_str(jinput *i, char const *str, size_t n)
{ {
if(i->size + n > i->max) return; if(i->size + n > i->max || i->cursor < 0) return;
/* Insert at cursor_pos, shift everything else right n places */ /* Insert at i->cursor, shift everything else right n places */
for(int k = i->size - 1; k >= i->cursor; k--) for(int k = i->size - 1; k >= i->cursor; k--)
i->text[k + n] = i->text[k]; i->text[k + n] = i->text[k];
@ -131,7 +131,7 @@ static void insert_code_point(jinput *i, uint32_t p)
static int previous(char const *str, int position) static int previous(char const *str, int position)
{ {
if(position == 0) if(position <= 0)
return position; return position;
while((str[--position] & 0xc0) == 0x80) {} while((str[--position] & 0xc0) == 0x80) {}
@ -148,6 +148,7 @@ static int next(char const *str, int position)
static void delete(jinput *i) static void delete(jinput *i)
{ {
if(i->cursor < 0) return;
int prev = previous(i->text, i->cursor); int prev = previous(i->text, i->cursor);
int diff = i->cursor - prev; int diff = i->cursor - prev;
if(!diff) return; if(!diff) return;