From 5b591a6fb49190c3b1deb80f3b508705a89d206e Mon Sep 17 00:00:00 2001 From: Lephenixnoir Date: Sat, 19 Nov 2022 18:53:43 +0100 Subject: [PATCH] jinput: fix out-of-bounds bug in weird cursor positions --- include/justui/jinput.h | 2 +- src/jinput.c | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/include/justui/jinput.h b/include/justui/jinput.h index 1fd7c45..f58f48c 100644 --- a/include/justui/jinput.h +++ b/include/justui/jinput.h @@ -18,7 +18,7 @@ an indicator displays the status of modifier keys. 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. * When pressing SHIFT or ALPHA in combination with a key (without releasing diff --git a/src/jinput.c b/src/jinput.c index 0f847cb..152c72c 100644 --- a/src/jinput.c +++ b/src/jinput.c @@ -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) { - 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--) 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) { - if(position == 0) + if(position <= 0) return position; while((str[--position] & 0xc0) == 0x80) {} @@ -148,6 +148,7 @@ static int next(char const *str, int position) static void delete(jinput *i) { + if(i->cursor < 0) return; int prev = previous(i->text, i->cursor); int diff = i->cursor - prev; if(!diff) return;