Constuct a sentry object in istream::readsome, and handle failures appropriately. Fixes PR#28217.

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@275280 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/include/istream b/include/istream
index 0bcc7ee..ee69400 100644
--- a/include/istream
+++ b/include/istream
@@ -1251,18 +1251,35 @@
 basic_istream<_CharT, _Traits>::readsome(char_type* __s, streamsize __n)
 {
     __gc_ = 0;
-    streamsize __c = this->rdbuf()->in_avail();
-    switch (__c)
+#ifndef _LIBCPP_NO_EXCEPTIONS
+    try
     {
-    case -1:
-        this->setstate(ios_base::eofbit);
-        break;
-    case 0:
-        break;
-    default:
-        read(__s, _VSTD::min(__c, __n));
-        break;
+#endif  // _LIBCPP_NO_EXCEPTIONS
+        sentry __sen(*this, true);
+        if (__sen)
+        {
+            streamsize __c = this->rdbuf()->in_avail();
+            switch (__c)
+            {
+            case -1:
+                this->setstate(ios_base::eofbit);
+                break;
+            case 0:
+                break;
+            default:
+                read(__s, _VSTD::min(__c, __n));
+                break;
+            }
+        }
+        else
+            this->setstate(ios_base::failbit);
+#ifndef _LIBCPP_NO_EXCEPTIONS
     }
+    catch (...)
+    {
+        this->__set_badbit_and_consider_rethrow();
+    }
+#endif  // _LIBCPP_NO_EXCEPTIONS
     return __gc_;
 }