diff --git a/CHANGELOG.md b/CHANGELOG.md index 242f3bab..f9c665fa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -74,6 +74,8 @@ * Use `utf8proc_charwidth()` instead of `wcwidth()`+`wcswidth()` when calculating character width, when foot has been built with utf8proc support ([#1865][1865]). +* Run-time changes to the window title, and the app ID now require the + new value to consist of printable characters only. [1865]: https://codeberg.org/dnkl/foot/issues/1865 diff --git a/misc.c b/misc.c index 1e5b9328..c7abe03b 100644 --- a/misc.c +++ b/misc.c @@ -44,8 +44,19 @@ timespec_sub(const struct timespec *a, const struct timespec *b, } bool -is_valid_utf8(const char *value) +is_valid_utf8_and_printable(const char *value) { - return value != NULL && - mbsntoc32(NULL, value, strlen(value), 0) != (size_t)-1; + char32_t *wide = ambstoc32(value); + if (wide == NULL) + return false; + + for (const char32_t *c = wide; *c != U'\0'; c++) { + if (!isc32print(*c)) { + free(wide); + return false; + } + } + + free(wide); + return true; } diff --git a/misc.h b/misc.h index cce8d2c1..6c77c484 100644 --- a/misc.h +++ b/misc.h @@ -9,4 +9,4 @@ bool isword(char32_t wc, bool spaces_only, const char32_t *delimiters); void timespec_add(const struct timespec *a, const struct timespec *b, struct timespec *res); void timespec_sub(const struct timespec *a, const struct timespec *b, struct timespec *res); -bool is_valid_utf8(const char *value); +bool is_valid_utf8_and_printable(const char *value); diff --git a/terminal.c b/terminal.c index 5532972e..a4963bc8 100644 --- a/terminal.c +++ b/terminal.c @@ -3570,7 +3570,7 @@ term_set_window_title(struct terminal *term, const char *title) if (term->window_title != NULL && streq(term->window_title, title)) return; - if (!is_valid_utf8(title)) { + if (!is_valid_utf8_and_printable(title)) { /* It's an xdg_toplevel::set_title() protocol violation to set a title with an invalid UTF-8 sequence */ LOG_WARN("%s: title is not valid UTF-8, ignoring", title); @@ -3593,7 +3593,7 @@ term_set_app_id(struct terminal *term, const char *app_id) if (term->app_id != NULL && app_id != NULL && streq(term->app_id, app_id)) return; - if (app_id != NULL && !is_valid_utf8(app_id)) { + if (app_id != NULL && !is_valid_utf8_and_printable(app_id)) { LOG_WARN("%s: app-id is not valid UTF-8, ignoring", app_id); return; }