[gnome-desktop] gnome-languages: Avoid passing NULL to newlocale



commit 0638f32dc93180f31d3f1d8e440e8dbd809667f6
Author: Pablo Correa Gómez <ablocorrea hotmail com>
Date:   Wed Oct 6 15:28:40 2021 +0200

    gnome-languages: Avoid passing NULL to newlocale
    
    Passing NULL to newlocale is not warrantied to return an
    error, but only "may" according to POSIX.1-2008[1]. For this
    function glibc returns an error instead of treating the NULL
    pointer as undefined behaviour. However, that is not the case
    in muslc, which generates a segmentation fault.
    
    This commit makes sure that any call to newlocale is preceded
    by a NULL pointer check. It makes the code more conformant to
    POSIX.1-2008 and gives an intepretation of what a NULL locale
    means in this piece of code.
    
    [1] https://pubs.opengroup.org/onlinepubs/9699919799/functions/newlocale.html

 libgnome-desktop/gnome-languages.c | 6 ++++++
 1 file changed, 6 insertions(+)
---
diff --git a/libgnome-desktop/gnome-languages.c b/libgnome-desktop/gnome-languages.c
index 11d6ab27..a9b1a197 100644
--- a/libgnome-desktop/gnome-languages.c
+++ b/libgnome-desktop/gnome-languages.c
@@ -289,6 +289,9 @@ language_name_is_valid (const char *language_name)
 {
         locale_t locale;
 
+        if (language_name == NULL)
+                return FALSE;
+
         locale = newlocale (LC_MESSAGES_MASK, language_name, (locale_t) 0);
         if (locale != (locale_t) 0) {
                 freelocale (locale);
@@ -306,6 +309,9 @@ language_name_get_codeset_details (const char  *language_name,
         locale_t locale;
         const char *codeset = NULL;
 
+        if (language_name == NULL) {
+                language_name = setlocale (LC_MESSAGES, NULL);
+        }
         locale = newlocale (LC_CTYPE_MASK, language_name, (locale_t) 0);
         if (locale == (locale_t) 0)
                 return;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]