From 0e5e68d82eac894483bb1b67a1b23b1972a9f045 Mon Sep 17 00:00:00 2001 From: Craig Barnes Date: Wed, 10 Nov 2021 17:29:57 +0000 Subject: [PATCH] main: improve handling of setlocale(3) return value There's no need to call setlocale() twice, since the first call returns the same value as the second. We also handle a NULL return value explicitly and show a distinct error message for that case, since it typically indicates a misconfigured locale and not just a non-UTF8 locale. --- main.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/main.c b/main.c index 851a4856..4f2dc7b6 100644 --- a/main.c +++ b/main.c @@ -417,8 +417,12 @@ main(int argc, char *const *argv) srand(time(NULL)); - setlocale(LC_CTYPE, ""); - LOG_INFO("locale: %s", setlocale(LC_CTYPE, NULL)); + const char *locale = setlocale(LC_CTYPE, ""); + if (locale == NULL) { + LOG_ERR("setlocale() failed"); + return ret; + } + LOG_INFO("locale: %s", locale); if (!locale_is_utf8()) { LOG_ERR("locale is not UTF-8"); return ret;