From 4c924ab14a032a6fcb695059463ee1cd7734b77a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Thu, 19 Dec 2019 07:25:05 +0100 Subject: [PATCH] main: verify locale is UTF-8 --- main.c | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/main.c b/main.c index b9aa14d3..a8776606 100644 --- a/main.c +++ b/main.c @@ -47,6 +47,21 @@ print_usage(const char *prog_name) " -v,--version show the version number and quit\n"); } +bool +locale_is_utf8(void) +{ + assert(strlen(u8"ö") == 2); + + wchar_t w; + if (mbtowc(&w, u8"ö", 2) != 2) + return false; + + if (w != U'ö') + return false; + + return true; +} + struct shutdown_context { struct terminal **term; int exit_code; @@ -167,12 +182,16 @@ main(int argc, char *const *argv) argc -= optind; argv += optind; - setlocale(LC_ALL, ""); - struct config conf = {NULL}; if (!config_load(&conf, conf_path)) return ret; + setlocale(LC_ALL, ""); + if (!locale_is_utf8()) { + LOG_ERR("locale is not UTF-8"); + return ret; + } + if (conf_term != NULL) { free(conf.term); conf.term = strdup(conf_term); @@ -242,6 +261,8 @@ out: fdm_destroy(fdm); config_free(conf); + + LOG_INFO("goodbye"); log_deinit(); return ret == EXIT_SUCCESS && !as_server ? shutdown_ctx.exit_code : ret; }