microsoft / microsoft/STL

<fstream>: ifstream putback is still broken

Open
#1,171 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Describe the bug
The behaviour of std::basic_ifstream::putback is incorrect when putback is used over buffer boundaries.
In particular, it appears that if one does a get() – peek() – putback() sequence, where the peek() causes a new buffer to be read from the file, the following putback() overwrites the first character in the buffer (i.e., the character that peek() returned) with the character you put back. Moreover, the pointers managing the buffer appear to be put into in an inconsistent state.

Command-line test case

C:\Temp>type main.cpp
#include <iostream>
#include <fstream>

int main()
{
  std::ofstream out("temp.bin", std::ios_base::binary);
  for (char i = 0; i < 16; i++)
  {
    out.put(i);
  }
  out.close();

  std::ifstream in("temp.bin", std::ios_base::binary);
  char buffer[8];
  in.rdbuf()->pubsetbuf(buffer, sizeof(buffer));

  for (char i = 0; i < 8; i++)
  {
    if (i != in.get())
    {
      std::cout << "incorrect character" << std::endl;
    }
  }
  std::cout << "pos: " << in.tellg() << std::endl;
  std::cout << "peek: " << in.peek() << std::endl;
  std::cout << "pos: " << in.tellg() << std::endl;
  in.putback(7);
  std::cout << "putback(7) " << std::endl;
  std::cout << "good?: " << in.good() << std::endl;
  std::cout << "pos: " << in.tellg() << std::endl;
  std::cout << "peek: " << in.peek() << std::endl;
  std::cout << "pos: " << in.tellg() << std::endl;
  std::cout << "get: " << in.get() << std::endl;
  std::cout << "pos: " << in.tellg() << std::endl;
  std::cout << "get: " << in.get() << std::endl;
  std::cout << "pos: " << in.tellg() << std::endl;
}
C:\Temp>cl /EHsc /W4 /WX main.cpp
Оптимизирующий компилятор Microsoft (R) C/C++ версии 19.28.29115 для x64
(C) Корпорация Майкрософт (Microsoft Corporation).  Все права защищены.

main.cpp
Microsoft (R) Incremental Linker Version 14.28.29115.0
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:main.exe
main.obj

C:\Temp>main.exe
pos: 8
peek: 8
pos: 8
putback(7)
good?: 1
pos: 14
peek: 8
pos: 6
get: 8
pos: 7
get: 9
pos: 8

Expected behavior
if the stream is not able to achieve the putback is should report a failure. Valid outputs are therefore either:

pos: 8
peek: 8
pos: 8
putback(7) 
good?: 0
pos: -1
peek: -1
pos: -1
get: -1
pos: -1
get: -1
pos: -1

Or if putback succeeds:

pos: 8
peek: 8
pos: 8
putback(7) 
good?: 1
pos: 7
peek: 7
pos: 7
get: 7
pos: 8
get: 8
pos: 9

STL version
Microsoft Visual Studio Community 2019 Preview Version 16.8.0 Preview 1.0

Additional context
DevCom-858136 | DevCom-246367
Microsoft-internal: VSO-275515 / AB#275515 | VSO-425342 / AB#425342

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 by reproducing the command-line test case with the shown buffer size and get–peek–putback sequence. Inspect the std::basic_ifstream and stream-buffer putback behavior across buffer boundaries; done means putback either fails cleanly or restores position 7 and yields 7, then 8, with consistent positions.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
backend
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.