microsoft / microsoft/STL

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

Open
#1,113 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug vNext
Dominant language
C++
Stars
11.2k
Forks
1.7k
Avg merge
4d 15h
Merged PRs (30d)
22

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

https://github.com/microsoft/STL/blob/faccf0084ed9b8b58df103358174537233b178c7/tests/libcxx/expected_results.txt#L407-L409

  • Also tracked by Microsoft-internal AB#595631

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the standalone repro.cpp and compare its assertions with the referenced libcxx overflow.pass.cpp test. Review tests/libcxx/expected_results.txt around the skipped entry, then locate the basic_filebuf implementation and its ABI constraints. Done means the unbuffered setbuf(0, 0) behavior satisfies all listed assertions and the skipped test can be enabled.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
backend, testing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.