From d6d90c82c119c363da27bfbee3ba95cd0c9b145c Mon Sep 17 00:00:00 2001 From: lolicon0930 <14966910+lolicon0930@users.noreply.github.com> Date: Fri, 11 Aug 2023 12:41:25 +0800 Subject: [PATCH] Properly check the length of UTF-8 strings.(closes fairyglade#527) --- src/draw.c | 19 +++++++++++++------ src/draw.h | 3 +++ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/draw.c b/src/draw.c index 0679244..373dd72 100644 --- a/src/draw.c +++ b/src/draw.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -373,10 +374,16 @@ void draw_labels(struct term_buf* buf) // throws } } +size_t utf8_strlen(char* s) { + size_t len = 0; + while (*s) len += (*s++ & 0xc0) != 0x80; + return len; +} + void draw_key_hints() { struct tb_cell* shutdown_key = str_cell(config.shutdown_key); - int len = strlen(config.shutdown_key); + uint16_t len = utf8_strlen(config.shutdown_key); if (dgn_catch()) { dgn_reset(); @@ -395,31 +402,31 @@ void draw_key_hints() } else { - tb_blit(len, 0, strlen(lang.shutdown), 1, shutdown); + tb_blit(len, 0, utf8_strlen(lang.shutdown), 1, shutdown); free(shutdown); } struct tb_cell* restart_key = str_cell(config.restart_key); - len += strlen(lang.shutdown) + 1; + len += utf8_strlen(lang.shutdown) + 1; if (dgn_catch()) { dgn_reset(); } else { - tb_blit(len, 0, strlen(config.restart_key), 1, restart_key); + tb_blit(len, 0, utf8_strlen(config.restart_key), 1, restart_key); free(restart_key); } struct tb_cell* restart = str_cell(lang.restart); - len += strlen(config.restart_key) + 1; + len += utf8_strlen(config.restart_key) + 1; if (dgn_catch()) { dgn_reset(); } else { - tb_blit(len, 0, strlen(lang.restart), 1, restart); + tb_blit(len, 0, utf8_strlen(lang.restart), 1, restart); free(restart); } } diff --git a/src/draw.h b/src/draw.h index b8a64c4..9d59fdd 100644 --- a/src/draw.h +++ b/src/draw.h @@ -5,6 +5,7 @@ #include "inputs.h" #include +#include #include struct box @@ -89,4 +90,6 @@ bool cascade(struct term_buf* buf, uint8_t* fails); void draw_bigclock(struct term_buf *buf); void draw_clock(struct term_buf *buf); +size_t utf8_strlen(char* s); + #endif