Skip to content

<fstream>: ifstream putback is still broken #1171

Description

@fsb4000

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

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