PATCH: [perl #115808]: POSIX::setlocale returns bizarre value on failure
authorKarl Williamson <public@khwilliamson.com>
Mon, 8 Jul 2013 04:42:43 +0000 (22:42 -0600)
committerKarl Williamson <public@khwilliamson.com>
Mon, 8 Jul 2013 15:13:49 +0000 (09:13 -0600)
It turns out that this bug is caused by a more general one, covered by
[perl #118693].  But in the meantime, this commit fixes the problem for
setlocale(), and makes the code slightly cleaner besides.

ext/POSIX/POSIX.xs
lib/locale.t

index e4c9e88..655fda3 100644 (file)
@@ -1097,7 +1097,10 @@ setlocale(category, locale = 0)
        char *          retval;
     CODE:
        retval = setlocale(category, locale);
-       if (retval) {
+       if (! retval) {
+            XSRETURN_UNDEF;
+        }
+        else {
            /* Save retval since subsequent setlocale() calls
             * may overwrite it. */
            RETVAL = savepv(retval);
@@ -1153,13 +1156,10 @@ setlocale(category, locale = 0)
            }
 #endif /* USE_LOCALE_NUMERIC */
        }
-       else
-           RETVAL = NULL;
     OUTPUT:
        RETVAL
     CLEANUP:
-        if (RETVAL)
-           Safefree(RETVAL);
+        Safefree(RETVAL);
 
 NV
 acos(x)
index 081783b..1126a38 100644 (file)
@@ -1519,6 +1519,16 @@ if ($didwarn) {
 
 $test_num = $final_locales_test_number;
 
+{   # perl #115808
+    use warnings;
+    my $warned = 0;
+    local $SIG{__WARN__} = sub {
+        $warned = $_[0] =~ /uninitialized/;
+    };
+    my $z = "y" . setlocale(&POSIX::LC_ALL, "xyzzy");
+    ok($warned, "variable set to setlocale(BAD LOCALE) is considered uninitialized");
+}
+
 # Test that tainting and case changing works on utf8 strings.  These tests are
 # placed last to avoid disturbing the hard-coded test numbers that existed at
 # the time these were added above this in this file.