about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorMichael Vetter <jubalh@iodoru.org>2020-04-19 00:07:31 +0200
committerGitHub <noreply@github.com>2020-04-19 00:07:31 +0200
commit95b75a29485706dd25886f4e077ee0d2aa08cc97 (patch)
tree5b4282a5396e2378d5a627a8aed56c6f80429827 /src
parenta39e44ac1d42f12fa1ac015d80037babd2a3a039 (diff)
parent1f0159de361af5d03a0469570cc6b7ea1ff45e4a (diff)
downloadprofani-tty-95b75a29485706dd25886f4e077ee0d2aa08cc97.tar.gz
Merge pull request #1319 from profanity-im/input
Make _inp_edited() more robust
Diffstat (limited to 'src')
-rw-r--r--src/ui/inputwin.c9
1 files changed, 8 insertions, 1 deletions
diff --git a/src/ui/inputwin.c b/src/ui/inputwin.c
index 40875935..8362ee00 100644
--- a/src/ui/inputwin.c
+++ b/src/ui/inputwin.c
@@ -324,6 +324,10 @@ _inp_write(char *line, int offset)
 static int
 _inp_edited(const wint_t ch)
 {
+    // Use own state to be thread-safe with possible pthreads. C standard
+    // guarantees that initial value of the state will be zeroed buffer.
+    static mbstate_t mbstate;
+
     // backspace
     if (ch == 127) {
         return 1;
@@ -341,7 +345,10 @@ _inp_edited(const wint_t ch)
 
     // printable
     char bytes[MB_CUR_MAX+1];
-    size_t utf_len = wcrtomb(bytes, ch, (mbstate_t*)NULL);
+    size_t utf_len = wcrtomb(bytes, ch, &mbstate);
+    if (utf_len == (size_t)-1) {
+        return 0;
+    }
     bytes[utf_len] = '\0';
     gunichar unichar = g_utf8_get_char(bytes);