Skip to content

TextFieldParser does not correctly handle input from streams that don't fill out the full array provided. #58857

Closed

Description

@nhunt-zf

Description

Microsoft.VisualBasic.FileIO.TextFieldParser does not cope properly with stream reads that return fewer characters than requested. Note that the documentation for Stream.Read() is clear that this is possible:

"The total number of bytes read into the buffer. This can be less than the number of bytes requested if that many bytes are not currently available, or zero (0) if the end of the stream has been reached.
...
An implementation is free to return fewer bytes than requested even if the end of the stream has not been reached."

– https://docs.microsoft.com/en-us/dotnet/api/system.io.stream.read

The source code for TextFieldParser is available:
referencesource/TextFieldParser.vb at master · https://github.com/microsoft/referencesource/blob/master/Microsoft.VisualBasic/runtime/msvbalib/FileIO/TextFieldParser.vb

runtime/TextFieldParser.vb at main ·
https://github.com/dotnet/runtime/blob/main/src/libraries/Microsoft.VisualBasic.Core/src/Microsoft/VisualBasic/FileIO/TextFieldParser.vb

Notice that on line 828 / 735 within IncreaseBufferSize():

Dim CharsRead As Integer = m_Reader.Read(TempArray, m_Buffer.Length, DEFAULT_BUFFER_LENGTH)

This assumes that m_Buffer is full at the time the buffer is expanded, but it may not be, due to the fact that Stream.Read() may have previously returned fewer bytes than requested. The true length of m_Buffer can be found in m_CharsRead.

This behaviour has caused issues for us when parsing a file that has been provided to a web controller, as the stream does not fully fill the buffer provided.

Activity

  1. ghost added
    untriaged
    on Sep 9, 2021
  2. added this to the 7.0.0 milestone on Sep 13, 2021
  3. cston commented on Sep 13, 2021

    @cston
    Contributor

    It looks like the current behavior is unchanged from .NET Framework. Moving to next milestone since this is not a regression.

  4. removed
    untriaged
    on Sep 13, 2021
  5. changed the title [-]TextFieldParser does correctly handle input from streams that don't fill out the full array provided.[/-] [+]TextFieldParser does not correctly handle input from streams that don't fill out the full array provided.[/+] on Sep 14, 2021
  6. self-assigned this
    on Sep 22, 2021
  7. self-assigned this
    on Sep 22, 2021
  8. stephentoub commented on Sep 22, 2021

    @stephentoub
    Member

    It looks like the current behavior is unchanged from .NET Framework.

    It actually does appear to be a regression. The cited function is the same, but SlideCursorToStartOfBuffer is different (and wrong) in core. The code in core appears to have come from reference source (https://github.com/microsoft/referencesource/blob/5697c29004a34d80acdaf5742d7e699022c64ecd/Microsoft.VisualBasic/runtime/msvbalib/FileIO/TextFieldParser.vb#L776-L799), which is for some reason not in sync with what's actually in the .NET Framework 4.8 binaries.

  9. ghost added
    in-pr
    on Sep 22, 2021
  10. removed their assignment
    on Sep 22, 2021
  11. ghost removed
    in-pr
    on Sep 22, 2021
  12. cston commented on Sep 24, 2021

    @cston
    Contributor

    @nhunt-zf, thanks for logging this issue!

  13. ghost locked as resolved and limited conversation to collaborators on Nov 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions