Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

<fstream>: basic_filebuf doesn't comply with setbuf(0,0) requirement in the standard #1113

Open
MahmoudGSaleh opened this issue Jul 30, 2020 · 0 comments
Labels
bug Something isn't working vNext Breaks binary compatibility

Comments

@MahmoudGSaleh
Copy link
Member

MahmoudGSaleh commented Jul 30, 2020

[filebuf.virtuals]/12 says:

If setbuf(0, 0) is called on a stream before any I/O has occurred on that stream, the stream becomes unbuffered. Otherwise the results are implementation-defined. "Unbuffered" means that pbase() and pptr() always return null and output to the file should appear as soon as possible.

We don't accept pubsetbuf() before a file has been opened. libstdc++ accepts pubsetbuf() only before a file has been opened. Opening the file should probably be considered I/O. Note that even if you move pubsetbuf() after the open we still don't do the right thing.

Test case

Based on libcxx/test/std/input.output/file.streams/fstreams/filebuf.virtuals/overflow.pass.cpp:

1. >type repro.cpp
#include <fstream>
#include <iostream>

template <class CharT>
struct test_buf
    : public std::basic_filebuf<CharT>
{
    typedef std::basic_filebuf<CharT>  base;
    typedef typename base::char_type   char_type;
    typedef typename base::int_type    int_type;
    typedef typename base::traits_type traits_type;

    char_type* pbase() const {return base::pbase();}
    char_type* pptr()  const {return base::pptr();}
    char_type* epptr() const {return base::epptr();}
    void gbump(int n) {base::gbump(n);}

    virtual int_type overflow(int_type c = traits_type::eof()) {return base::overflow(c);}
};

#define ASSERT_OR_FAIL(x) {num++; if (!(x)) { std::cout << "FAILED at #" << num << std::endl; return -1; } }

int main()
{
    int num = 0;
    test_buf<char> f;
    f.pubsetbuf(0, 0);
    // #1
    ASSERT_OR_FAIL(f.open("overflow.dat", std::ios_base::out) != 0);
    // #2
    ASSERT_OR_FAIL(f.is_open());
    // #3
    ASSERT_OR_FAIL(f.pbase() == 0);
    // #4
    ASSERT_OR_FAIL(f.pptr() == 0);
    // #5
    ASSERT_OR_FAIL(f.epptr() == 0);
    // #6
    ASSERT_OR_FAIL(f.overflow('a') == 'a');
    // #7
    ASSERT_OR_FAIL(f.pbase() == 0);
    // #8
    ASSERT_OR_FAIL(f.pptr() == 0);
    // #9
    ASSERT_OR_FAIL(f.epptr() == 0);
}

2. >cl /EHsc /W4 /WX .\repro.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 19.27.29009.1 for x86
Copyright (C) Microsoft Corporation.  All rights reserved.

repro.cpp
Microsoft (R) Incremental Linker Version 14.27.29009.1
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:repro.exe
repro.obj

3. >.\repro.exe
FAILED at #7

Expected behavior
The tests pass

STL version

Microsoft Visual Studio Community 2019 Preview
Version 16.7.0 Preview 3.0

Additional context

  • This must be an ABI breaking change as it forces us to remember whether that call happened somewhere.

  • Skipped libcxx test

# GH-1113 <fstream>: basic_filebuf doesn't comply with setbuf(0,0) requirement in the standard
std/input.output/file.streams/fstreams/filebuf.virtuals/overflow.pass.cpp FAIL
std/input.output/file.streams/fstreams/filebuf.virtuals/underflow.pass.cpp FAIL

  • Also tracked by Microsoft-internal AB#595631
@MahmoudGSaleh MahmoudGSaleh added the vNext Breaks binary compatibility label Jul 30, 2020
@CaseyCarter CaseyCarter added the bug Something isn't working label Jul 30, 2020
@StephanTLavavej StephanTLavavej changed the title <fstream> basic_filebuf doesn't comply with setbuf(0,0) requirement in the standard <fstream>: basic_filebuf doesn't comply with setbuf(0,0) requirement in the standard Sep 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working vNext Breaks binary compatibility
Projects
None yet
Development

No branches or pull requests

2 participants