Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
stephentoub opened this issue Jan 5, 2023 · 4 comments · Fixed by #82620
Closed

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

stephentoub opened this issue Jan 5, 2023 · 4 comments · Fixed by #82620

Comments

@stephentoub
Copy link
Member

stephentoub commented Jan 5, 2023

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

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Jan 5, 2023
@ghost
Copy link

ghost commented Jan 5, 2023

Tagging subscribers to this area: @roji, @ajcvickers
See info in area-owners.md if you want to be subscribed.

Issue Details

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

Author: stephentoub
Assignees: -
Labels:

area-System.Data

Milestone: -

@stephentoub stephentoub changed the title SqlBytes from Stream doesn't work for partial reads SqlBytes/SqlChars from Stream doesn't work for partial reads Jan 5, 2023
@stephentoub
Copy link
Member Author

Related:
#69159

@roji
Copy link
Member

roji commented Jan 5, 2023

/cc @David-Engel

@ghost
Copy link

ghost commented Jan 17, 2023

Tagging subscribers to this area: @cheenamalhotra, @David-Engel
See info in area-owners.md if you want to be subscribed.

Issue Details

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/SqlChars 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

Author: stephentoub
Assignees: -
Labels:

area-System.Data, area-System.Data.SqlClient, untriaged

Milestone: -

@stephentoub stephentoub changed the title SqlBytes/SqlChars from Stream doesn't work for partial reads SqlBytes from Stream doesn't work for partial reads Feb 24, 2023
@ghost ghost added the in-pr There is an active PR which will close this issue when it is merged label Feb 24, 2023
@ghost ghost removed in-pr There is an active PR which will close this issue when it is merged untriaged New issue has not been triaged by the area owner labels Feb 24, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Mar 27, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants