802f23
2017-07-18  Jonathan Wakely  <jwakely@redhat.com>
802f23
802f23
	PR libstdc++/81395
802f23
	* include/bits/fstream.tcc (basic_filebuf::xsgetn): Don't set buffer
802f23
	pointers for write mode after reading.
802f23
	* testsuite/27_io/basic_filebuf/sgetn/char/81395.cc: New.
802f23
802f23
--- libstdc++-v3/include/bits/fstream.tcc	(revision 254017)
802f23
+++ libstdc++-v3/include/bits/fstream.tcc	(revision 254018)
802f23
@@ -699,7 +699,7 @@
802f23
  
802f23
  	   if (__n == 0)
802f23
  	     {
802f23
- 	       _M_set_buffer(0);
802f23
+	       // Set _M_reading. Buffer is already in initial 'read' mode.
802f23
  	       _M_reading = true;
802f23
  	     }
802f23
  	   else if (__len == 0)
802f23
--- libstdc++-v3/testsuite/27_io/basic_filebuf/sgetn/char/81395.cc	(nonexistent)
802f23
+++ libstdc++-v3/testsuite/27_io/basic_filebuf/sgetn/char/81395.cc	(revision 254018)
802f23
@@ -0,0 +1,46 @@
802f23
+// Copyright (C) 2017 Free Software Foundation, Inc.
802f23
+//
802f23
+// This file is part of the GNU ISO C++ Library.  This library is free
802f23
+// software; you can redistribute it and/or modify it under the
802f23
+// terms of the GNU General Public License as published by the
802f23
+// Free Software Foundation; either version 3, or (at your option)
802f23
+// any later version.
802f23
+
802f23
+// This library is distributed in the hope that it will be useful,
802f23
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
802f23
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
802f23
+// GNU General Public License for more details.
802f23
+
802f23
+// You should have received a copy of the GNU General Public License along
802f23
+// with this library; see the file COPYING3.  If not see
802f23
+// <http://www.gnu.org/licenses/>.
802f23
+
802f23
+// { dg-require-fileio "" }
802f23
+
802f23
+// PR libstdc++/81395
802f23
+
802f23
+#include <fstream>
802f23
+#include <cstring>	// for std::memset
802f23
+#include <cstdio>	// For BUFSIZ
802f23
+
802f23
+using std::memset;
802f23
+
802f23
+int main()
802f23
+{
802f23
+  {
802f23
+    std::filebuf fb;
802f23
+    fb.open("test.txt", std::ios::out);
802f23
+    char data[BUFSIZ];
802f23
+    memset(data, 'A', sizeof(data));
802f23
+    fb.sputn(data, sizeof(data));
802f23
+  }
802f23
+
802f23
+  std::filebuf fb;
802f23
+  fb.open("test.txt", std::ios::in|std::ios::out);
802f23
+  char buf[BUFSIZ];
802f23
+  memset(buf, 0, sizeof(buf));
802f23
+  fb.sgetn(buf, sizeof(buf));
802f23
+  // Switch from reading to writing without seeking first:
802f23
+  fb.sputn("B", 1);
802f23
+  fb.pubsync();
802f23
+}