Skip to content

SqlBytes from Stream doesn't work for partial reads #80259

@stephentoub

Description

@stephentoub

Repro:

using System.Data.SqlTypes;

var stream = new TrickleStream(new byte[] { 1, 2, 3, 4, 5 });
var bytes = new SqlBytes(stream);
Console.WriteLine(BitConverter.ToString(bytes.Buffer));

class TrickleStream : MemoryStream
{
    public TrickleStream(byte[] bytes) : base(bytes) { }

    public override int Read(byte[] buffer, int offset, int count) =>
        base.Read(buffer, offset, Math.Min(count, 1));
    public override int Read(Span<byte> buffer) =>
        base.Read(buffer.IsEmpty ? buffer : buffer.Slice(0, 1));
}

That should output:

01-02-03-04-05

but ends up outputting:

01-00-00-00-00

The Stream.Read operations SqlBytes performs assumes Read will always read as much as was requested, which isn't guaranteed:

_stream.Read(buffer, 0, checked((int)_stream.Length));

_stream.Read(buffer, offsetInBuffer, count);

_stream.Read(_rgbBuf, 0, (int)lStreamLen);

cc: @roji, @eerhardt

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions