Skip to content

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

Description

[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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingvNextBreaks binary compatibility

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions