ui/ncurses: Check wcstombs() for error in nc-lang
authorSam Mendoza-Jonas <sam@mendozajonas.com>
Wed, 2 Mar 2016 04:36:36 +0000 (15:36 +1100)
committerSam Mendoza-Jonas <sam@mendozajonas.com>
Tue, 15 Mar 2016 03:23:13 +0000 (14:23 +1100)
If we are unable to correctly parse wide-character strings for display
in the Language screen (eg. due to an incorrect locale) display an
error string instead of continuing to try to display the string.

Signed-off-by: Sam Mendoza-Jonas <sam@mendozajonas.com>
ui/ncurses/nc-lang.c

index 7879c457cfe9eb8daa877f56cbd51f8e87df63d0..a7c9ccc53bb53f24b755a2d2c3109752dc4da05e 100644 (file)
@@ -21,6 +21,7 @@
 
 #include <stdlib.h>
 #include <string.h>
+#include <locale.h>
 
 #include <talloc/talloc.h>
 #include <types/types.h>
@@ -265,8 +266,14 @@ static void lang_screen_setup_widgets(struct lang_screen *screen,
 
                len = wcstombs(NULL, lang->label, 0);
                assert(len >= 0);
-               label = talloc_array(screen, char, len + 1);
-               wcstombs(label, lang->label, len + 1);
+               if (len < 0) {
+                       label = talloc_asprintf(screen,
+                               "Unable to display text in this locale (%s)\n",
+                               setlocale(LC_ALL, NULL));
+               } else {
+                       label = talloc_array(screen, char, len + 1);
+                       wcstombs(label, lang->label, len + 1);
+               }
 
                selected = config->lang && !strcmp(lang->name, config->lang);
                found |= selected;