Skip to content

Error System.IO.EndOfStreamException when reading text by function NpgsqlDataReader.GetChars in version 8.0.0 #5616

Description

@MLLangad

The error only occurs in version 8.0.0 or higher. In version 7.0.6 function NpgsqlDataReader.GetChars works correctly

The error occurs only when the first buffer is read not from position 0, but from another position.
The error occurs only during the third reading. First and second reading work correctly

CODE:

const string connectionString = "Host=localhost;Port=5432;Database=[DB];Username=[USERNAME];Password=[PASSWORD];";
const string tableName = "TEST";
char[] buffer = new char[2];
using (var connection = new Npgsql.NpgsqlConnection(connectionString)) {
    connection.Open();
    using(var command = connection.CreateCommand()) {
        command.CommandText = $"DROP TABLE IF EXISTS {tableName}";
        command.ExecuteNonQuery();
        command.CommandText = $"CREATE TABLE {tableName}(DATA text)";
        command.ExecuteNonQuery();
        command.CommandText = $"INSERT INTO {tableName} (DATA) VALUES ('01234567')";
        command.ExecuteNonQuery();
        command.CommandText = $"SELECT DATA FROM {tableName}";
        using(var reader = command.ExecuteReader(System.Data.CommandBehavior.SequentialAccess)) {
            reader.Read();
            long count = -1;
            //count = reader.GetChars(0, 0, buffer, 0, 2); //the error does not occur if reading starts from position 0
            count = reader.GetChars(0, 2, buffer, 0, 2);
            count = reader.GetChars(0, 4, buffer, 0, 2);
            count = reader.GetChars(0, 6, buffer, 0, 2); //System.IO.EndOfStreamException: 'Attempted to read past the end of the stream.'

        }
    }
}

Exception message:
System.IO.EndOfStreamException
HResult=0x80070026
Message=Attempted to read past the end of the stream.

Stack trace:
at Npgsql.Internal.Converters.GetCharsTextConverter.g__ConsumeChars|7_0(TextReader reader, Nullable`1 count)
at Npgsql.Internal.Converters.GetCharsTextConverter.ResumableRead(PgReader reader)
at Npgsql.Internal.Converters.GetCharsTextConverter.Read(PgReader reader)
at Npgsql.NpgsqlDataReader.GetChars(Int32 ordinal, Int64 dataOffset, Char[] buffer, Int32 bufferOffset, Int32 length)

Further technical details

Npgsql version: 8.0.2
PostgreSQL version: 16.0
Operating system: Windows 11 x64

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions