From 143f09331b22869e48ff63dd2a4cdeb23084abca Mon Sep 17 00:00:00 2001 From: "Stephan T. Lavavej" Date: Thu, 29 Jun 2023 15:59:59 -0700 Subject: [PATCH 1/2] Revert "Fix `fstream.seekp(0, ios::cur)` (#3773)" This reverts commit 518f4495ff585fe6052eaf0402e5da04701bd621. --- stl/inc/__msvc_filebuf.hpp | 3 +- tests/std/test.lst | 1 - .../GH_003572_fstream_seekp_0_cur/env.lst | 4 -- .../GH_003572_fstream_seekp_0_cur/test.cpp | 47 ------------------- 4 files changed, 2 insertions(+), 53 deletions(-) delete mode 100644 tests/std/tests/GH_003572_fstream_seekp_0_cur/env.lst delete mode 100644 tests/std/tests/GH_003572_fstream_seekp_0_cur/test.cpp diff --git a/stl/inc/__msvc_filebuf.hpp b/stl/inc/__msvc_filebuf.hpp index f06907a888c..7e420846cfa 100644 --- a/stl/inc/__msvc_filebuf.hpp +++ b/stl/inc/__msvc_filebuf.hpp @@ -651,7 +651,8 @@ class basic_filebuf : public basic_streambuf<_Elem, _Traits> { // stream buffer _Off -= static_cast(sizeof(_Elem)); // back up over _Elem bytes } - if (!_Myfile || !_Endwrite() || _CSTD _fseeki64(_Myfile, _Off, _Way) != 0 + if (!_Myfile || !_Endwrite() + || ((_Off != 0 || _Way != ios_base::cur) && _CSTD _fseeki64(_Myfile, _Off, _Way) != 0) || _CSTD fgetpos(_Myfile, &_Fileposition) != 0) { return pos_type{off_type{-1}}; // report failure } diff --git a/tests/std/test.lst b/tests/std/test.lst index 3059eef1402..512bd1d6e70 100644 --- a/tests/std/test.lst +++ b/tests/std/test.lst @@ -223,7 +223,6 @@ tests\GH_003022_substr_allocator tests\GH_003105_piecewise_densities tests\GH_003119_error_category_ctor tests\GH_003246_cmath_narrowing -tests\GH_003572_fstream_seekp_0_cur tests\GH_003617_vectorized_meow_element tests\GH_003676_format_large_hh_mm_ss_values tests\GH_003735_char_traits_signatures diff --git a/tests/std/tests/GH_003572_fstream_seekp_0_cur/env.lst b/tests/std/tests/GH_003572_fstream_seekp_0_cur/env.lst deleted file mode 100644 index 19f025bd0e6..00000000000 --- a/tests/std/tests/GH_003572_fstream_seekp_0_cur/env.lst +++ /dev/null @@ -1,4 +0,0 @@ -# Copyright (c) Microsoft Corporation. -# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - -RUNALL_INCLUDE ..\usual_matrix.lst diff --git a/tests/std/tests/GH_003572_fstream_seekp_0_cur/test.cpp b/tests/std/tests/GH_003572_fstream_seekp_0_cur/test.cpp deleted file mode 100644 index 72d15f50bcb..00000000000 --- a/tests/std/tests/GH_003572_fstream_seekp_0_cur/test.cpp +++ /dev/null @@ -1,47 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception - -#include -#include -#include -#include - -int main() { - using namespace std; - - fstream f("test.txt", ios::in | ios::out | ios::trunc); - - f << "123"; - - auto check = [&f](const char(&content)[4]) { - char buffer[4]{}; - f.seekg(0); - f.read(buffer, 3); - assert(f); - assert(memcmp(buffer, content, 4) == 0); - }; - - f.seekg(0); - (void) f.get(); - f.seekp(f.tellp()); - f << "*"; - check("1*3"); - - f.seekg(0); - (void) f.get(); - f.seekp(0, ios::cur); - f << "!"; - check("1!3"); - - f.seekg(0); - (void) f.get(); - f.seekp(1, ios::cur); - f << "!"; - check("1!!"); - - f.seekg(0); - (void) f.get(); - f.seekp(-1, ios::cur); - f << "!"; - check("!!!"); -} From be204741943fca362e0caec6476bcae2eb161286 Mon Sep 17 00:00:00 2001 From: "Stephan T. Lavavej" Date: Thu, 29 Jun 2023 16:24:05 -0700 Subject: [PATCH 2/2] Add GH_003840_tellg_when_reading_lf_file_in_text_mode. --- tests/std/test.lst | 1 + .../env.lst | 4 ++ .../test.cpp | 51 +++++++++++++++++++ 3 files changed, 56 insertions(+) create mode 100644 tests/std/tests/GH_003840_tellg_when_reading_lf_file_in_text_mode/env.lst create mode 100644 tests/std/tests/GH_003840_tellg_when_reading_lf_file_in_text_mode/test.cpp diff --git a/tests/std/test.lst b/tests/std/test.lst index 512bd1d6e70..5c0b3ca9533 100644 --- a/tests/std/test.lst +++ b/tests/std/test.lst @@ -226,6 +226,7 @@ tests\GH_003246_cmath_narrowing tests\GH_003617_vectorized_meow_element tests\GH_003676_format_large_hh_mm_ss_values tests\GH_003735_char_traits_signatures +tests\GH_003840_tellg_when_reading_lf_file_in_text_mode tests\LWG2381_num_get_floating_point tests\LWG2597_complex_branch_cut tests\LWG3018_shared_ptr_function diff --git a/tests/std/tests/GH_003840_tellg_when_reading_lf_file_in_text_mode/env.lst b/tests/std/tests/GH_003840_tellg_when_reading_lf_file_in_text_mode/env.lst new file mode 100644 index 00000000000..2de7aab2959 --- /dev/null +++ b/tests/std/tests/GH_003840_tellg_when_reading_lf_file_in_text_mode/env.lst @@ -0,0 +1,4 @@ +# Copyright (c) Microsoft Corporation. +# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +RUNALL_INCLUDE ..\usual_17_matrix.lst diff --git a/tests/std/tests/GH_003840_tellg_when_reading_lf_file_in_text_mode/test.cpp b/tests/std/tests/GH_003840_tellg_when_reading_lf_file_in_text_mode/test.cpp new file mode 100644 index 00000000000..2482c9c6a1f --- /dev/null +++ b/tests/std/tests/GH_003840_tellg_when_reading_lf_file_in_text_mode/test.cpp @@ -0,0 +1,51 @@ +// Copyright (c) Microsoft Corporation. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception + +#include +#include +#include +#include +#include + +#include "temp_file_name.hpp" + +using namespace std; + +void test(const string& temp_file_name_str, const ios_base::openmode mode) { + filesystem::remove(temp_file_name_str); + + { + ofstream out{temp_file_name_str, mode}; + out << "Line A\n"; + out << "Line B\n"; + out << "Line C\n"; + } + + { + ifstream in{temp_file_name_str}; + string line; + + assert(getline(in, line)); + assert(line == "Line A"); + (void) in.tellg(); + + assert(getline(in, line)); + assert(line == "Line B"); + (void) in.tellg(); + + assert(getline(in, line)); + assert(line == "Line C"); + (void) in.tellg(); + + assert(!getline(in, line)); + } + + filesystem::remove(temp_file_name_str); +} + +int main() { + const string temp_file_name_str = temp_file_name(); + + test(temp_file_name_str, ios_base::out); + test(temp_file_name_str, ios_base::binary); +}