Index: libcxx/include/fstream =================================================================== --- libcxx/include/fstream +++ libcxx/include/fstream @@ -425,19 +425,33 @@ } else { - ptrdiff_t __ln = __extbufnext_ - __extbuf_; - ptrdiff_t __le = __extbufend_ - __extbuf_; - ptrdiff_t __rn = __rhs.__extbufnext_ - __rhs.__extbuf_; - ptrdiff_t __re = __rhs.__extbufend_ - __rhs.__extbuf_; + ptrdiff_t __ln = 0, __le = 0, __rn = 0, __re = 0; + if (__extbufnext_ != nullptr) + __ln = __extbufnext_ - __extbuf_; + if (__extbufend_ != nullptr) + __le = __extbufend_ - __extbuf_; + if (__rhs.__extbufnext_ != nullptr) + __rn = __rhs.__extbufnext_ - __rhs.__extbuf_; + if (__rhs.__extbufend_ != nullptr) + __re = __rhs.__extbufend_ - __rhs.__extbuf_; if (__extbuf_ == __extbuf_min_ && __rhs.__extbuf_ != __rhs.__extbuf_min_) { __extbuf_ = __rhs.__extbuf_; __rhs.__extbuf_ = __rhs.__extbuf_min_; + _VSTD::memmove(__rhs.__extbuf_min_, __extbuf_min_, sizeof(__extbuf_min_)); } else if (__extbuf_ != __extbuf_min_ && __rhs.__extbuf_ == __rhs.__extbuf_min_) { __rhs.__extbuf_ = __extbuf_; __extbuf_ = __extbuf_min_; + _VSTD::memmove(__extbuf_min_, __rhs.__extbuf_min_, sizeof(__extbuf_min_)); + } + else + { + char __tmp[sizeof(__extbuf_min_)]; + _VSTD::memmove(__tmp, __extbuf_min_, sizeof(__extbuf_min_)); + _VSTD::memmove(__extbuf_min_, __rhs.__extbuf_min_, sizeof(__extbuf_min_)); + _VSTD::memmove(__rhs.__extbuf_min_, __tmp, sizeof(__extbuf_min_)); } __extbufnext_ = __extbuf_ + __rn; __extbufend_ = __extbuf_ + __re; Index: libcxx/test/std/input.output/file.streams/fstreams/filebuf.assign/nonmember_swap_min.pass.cpp =================================================================== --- /dev/null +++ libcxx/test/std/input.output/file.streams/fstreams/filebuf.assign/nonmember_swap_min.pass.cpp @@ -0,0 +1,61 @@ +//===----------------------------------------------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// + +// + +// template > +// class basic_filebuf + +// template +// void +// swap(basic_filebuf& x, basic_filebuf& y); + +#include +#include +#include "test_macros.h" +#include "platform_support.h" + +int main(int, char**) +{ + std::string tmp = get_temp_file_name(); + std::string tmpA = get_temp_file_name(); + std::string tmpB = get_temp_file_name(); + + { + std::ofstream sa(tmpA), sb(tmpB); + sa << "AAAA"; + sb << "BBBB"; + } + + std::filebuf f1; + assert(f1.open(tmpA, std::ios_base::in) != 0); + assert(f1.is_open()); + f1.pubsetbuf(0, 0); + + std::filebuf f2; + assert(f2.open(tmpB, std::ios_base::in) != 0); + assert(f2.is_open()); + f2.pubsetbuf(0, 0); + + assert(f1.sgetc() == 'A'); + assert(f2.sgetc() == 'B'); + + swap(f1, f2); + + assert(f1.is_open()); + assert(f2.is_open()); + + assert(f1.sgetc() == 'B'); + assert(f2.sgetc() == 'A'); + + std::remove(tmp.c_str()); + std::remove(tmpA.c_str()); + std::remove(tmpB.c_str()); + + return 0; +}