Skip to content

<fstream>: Calling tellg() when reading an LF file in text mode causes getline() to loop forever #3840

@StephanTLavavej

Description

@StephanTLavavej

Internal VSO-1841456 / AB#1841456 "[RWC][prod/fe][Regression] ITK failed to run test case itkCSVArray2DFileReaderTest" reported a runtime correctness regression caused by #3773. Here's a fully reduced test case:

D:\GitHub\STL\out\x64>type meow.cpp
#include <fstream>
#include <iostream>
#include <string>
using namespace std;

void test(const ios_base::openmode mode) {
    {
        ofstream out{"delme.txt", mode};
        out << "Line A\n";
        out << "Line B\n";
        out << "Line C\n";
    }

    {
        ifstream in{"delme.txt"}; // Always reading in text mode!
        string line;
        for (int i = 0; i < 5 && getline(in, line); ++i) {
            cout << "Read: \"" << line << "\"; ";
            cout << "in.tellg(): " << in.tellg() << "\n";
        }
    }
}

int main() {
    cout << "Testing a text mode (CRLF) file.\n";
    test(ios_base::out);
    cout << "--------------------------------\n";
    cout << "Testing a binary mode (LF) file.\n";
    test(ios_base::binary);
}

Before

D:\GitHub\STL\out\x64>cl /EHsc /nologo /W4 /MTd /Od meow.cpp && meow
meow.cpp
Testing a text mode (CRLF) file.
Read: "Line A"; in.tellg(): 8
Read: "Line B"; in.tellg(): 16
Read: "Line C"; in.tellg(): 24
--------------------------------
Testing a binary mode (LF) file.
Read: "Line A"; in.tellg(): 5
Read: "Line B"; in.tellg(): 13
Read: "Line C"; in.tellg(): 21

After

D:\GitHub\STL\out\x64>cl /EHsc /nologo /W4 /MTd /Od meow.cpp && meow
meow.cpp
Testing a text mode (CRLF) file.
Read: "Line A"; in.tellg(): 8
Read: "Line B"; in.tellg(): 16
Read: "Line C"; in.tellg(): 24
--------------------------------
Testing a binary mode (LF) file.
Read: "Line A"; in.tellg(): 5
Read: "A"; in.tellg(): 5
Read: "A"; in.tellg(): 5
Read: "A"; in.tellg(): 5
Read: "A"; in.tellg(): 5

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingfixedSomething works now, yay!high priorityImportant!

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions