http://llvm.org/bugs/show_bug.cgi?id=9326

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@126504 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/locale b/include/locale
index 13bac7a..c024e65 100644
--- a/include/locale
+++ b/include/locale
@@ -733,22 +733,27 @@
 {
     if (__a != __a_end)
     {
+        int __save_errno = errno;
+        errno = 0;
         char *__p2;
         long long __ll = strtoll_l(__a, &__p2, __base, 0);
+        int __current_errno = errno;
+        if (__current_errno == 0)
+            errno = __save_errno;
         if (__p2 != __a_end)
         {
             __err = ios_base::failbit;
             return 0;
         }
-        else if (__ll > numeric_limits<_Tp>::max())
+        else if (__current_errno == ERANGE         ||
+                 __ll < numeric_limits<_Tp>::min() ||
+                 numeric_limits<_Tp>::max() < __ll)
         {
             __err = ios_base::failbit;
-            return numeric_limits<_Tp>::max();
-        }
-        else if (__ll < numeric_limits<_Tp>::min())
-        {
-            __err = ios_base::failbit;
-            return numeric_limits<_Tp>::min();
+            if (__ll > 0)
+                return numeric_limits<_Tp>::max();
+            else
+                return numeric_limits<_Tp>::min();
         }
         return static_cast<_Tp>(__ll);
     }
@@ -763,14 +768,25 @@
 {
     if (__a != __a_end)
     {
+        if (*__a == '-')
+        {
+            __err = ios_base::failbit;
+            return 0;
+        }
+        int __save_errno = errno;
+        errno = 0;
         char *__p2;
         unsigned long long __ll = strtoull_l(__a, &__p2, __base, 0);
+        int __current_errno = errno;
+        if (__current_errno == 0)
+            errno = __save_errno;
         if (__p2 != __a_end)
         {
             __err = ios_base::failbit;
             return 0;
         }
-        else if (__ll > numeric_limits<_Tp>::max())
+        else if (__current_errno == ERANGE ||
+                 numeric_limits<_Tp>::max() < __ll)
         {
             __err = ios_base::failbit;
             return numeric_limits<_Tp>::max();