Skip to content

Commit 518f449

Browse files
achabenseStephanTLavavejCaseyCarter
authored
Fix fstream.seekp(0, ios::cur) (microsoft#3773)
Co-authored-by: Stephan T. Lavavej <[email protected]> Co-authored-by: Casey Carter <[email protected]>
1 parent 10f26f0 commit 518f449

File tree

4 files changed

+53
-2
lines changed

4 files changed

+53
-2
lines changed

stl/inc/__msvc_filebuf.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -651,8 +651,7 @@ class basic_filebuf : public basic_streambuf<_Elem, _Traits> { // stream buffer
651651
_Off -= static_cast<off_type>(sizeof(_Elem)); // back up over _Elem bytes
652652
}
653653

654-
if (!_Myfile || !_Endwrite()
655-
|| ((_Off != 0 || _Way != ios_base::cur) && _CSTD _fseeki64(_Myfile, _Off, _Way) != 0)
654+
if (!_Myfile || !_Endwrite() || _CSTD _fseeki64(_Myfile, _Off, _Way) != 0
656655
|| _CSTD fgetpos(_Myfile, &_Fileposition) != 0) {
657656
return pos_type{off_type{-1}}; // report failure
658657
}

tests/std/test.lst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ tests\GH_003105_piecewise_densities
223223
tests\GH_003119_error_category_ctor
224224
tests\GH_003246_cmath_narrowing
225225
tests\GH_003570_allocate_at_least
226+
tests\GH_003572_fstream_seekp_0_cur
226227
tests\GH_003617_vectorized_meow_element
227228
tests\GH_003676_format_large_hh_mm_ss_values
228229
tests\GH_003735_char_traits_signatures
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Copyright (c) Microsoft Corporation.
2+
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3+
4+
RUNALL_INCLUDE ..\usual_matrix.lst
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3+
4+
#include <cassert>
5+
#include <cstring>
6+
#include <fstream>
7+
#include <ios>
8+
9+
int main() {
10+
using namespace std;
11+
12+
fstream f("test.txt", ios::in | ios::out | ios::trunc);
13+
14+
f << "123";
15+
16+
auto check = [&f](const char(&content)[4]) {
17+
char buffer[4]{};
18+
f.seekg(0);
19+
f.read(buffer, 3);
20+
assert(f);
21+
assert(memcmp(buffer, content, 4) == 0);
22+
};
23+
24+
f.seekg(0);
25+
(void) f.get();
26+
f.seekp(f.tellp());
27+
f << "*";
28+
check("1*3");
29+
30+
f.seekg(0);
31+
(void) f.get();
32+
f.seekp(0, ios::cur);
33+
f << "!";
34+
check("1!3");
35+
36+
f.seekg(0);
37+
(void) f.get();
38+
f.seekp(1, ios::cur);
39+
f << "!";
40+
check("1!!");
41+
42+
f.seekg(0);
43+
(void) f.get();
44+
f.seekp(-1, ios::cur);
45+
f << "!";
46+
check("!!!");
47+
}

0 commit comments

Comments
 (0)