Skip to content
This repository has been archived by the owner on Dec 12, 2023. It is now read-only.

Commit

Permalink
Fix crash when the buffer for ResourceHandler is smaller than the res…
Browse files Browse the repository at this point in the history
…ource
  • Loading branch information
chylex committed Feb 28, 2022
1 parent 712bcd5 commit 61cd632
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
17 changes: 8 additions & 9 deletions lib/TweetLib.Browser.CEF/Logic/ByteArrayResourceHandlerLogic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ public abstract class ByteArrayResourceHandlerLogic {

[SuppressMessage("ReSharper", "MemberCanBePrivate.Global")]
public sealed class ByteArrayResourceHandlerLogic<TResponse> : ByteArrayResourceHandlerLogic {
public int RemainingBytes => resource.Length - position;

private readonly ByteArrayResource resource;
private readonly IResponseAdapter<TResponse> responseAdapter;

Expand Down Expand Up @@ -46,20 +44,21 @@ public sealed class ByteArrayResourceHandlerLogic<TResponse> : ByteArrayResource
return true;
}

public bool Read<T>(WriteToOut<T> write, T dataOut, int bytesToRead, out int bytesRead, IDisposable callback) {
public bool Read<T>(WriteToOut<T> write, T dataOut, long maxBytesToRead, out int bytesRead, IDisposable callback) {
callback.Dispose();

if (bytesToRead > 0) {
if (maxBytesToRead == 0) {
bytesRead = 0;
}
else {
int bytesToRead = (int) Math.Min(maxBytesToRead, resource.Length - position);

write(dataOut, resource.Contents, position, bytesToRead);
position += bytesToRead;
bytesRead = bytesToRead;
}

bytesRead = bytesToRead;
return bytesRead > 0;
}

public bool Read<T>(WriteToOut<T> write, T dataOut, out int bytesRead, IDisposable callback) {
return Read(write, dataOut, RemainingBytes, out bytesRead, callback);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ sealed class ByteArrayResourceHandler : CefResourceHandler {
}

protected override bool Read(IntPtr dataOut, int bytesToRead, out int bytesRead, CefResourceReadCallback callback) {
return logic!.Read(WriteToOut, dataOut, Math.Min(bytesToRead, logic.RemainingBytes), out bytesRead, callback);
return logic!.Read(WriteToOut, dataOut, bytesToRead, out bytesRead, callback);
}

protected override void Cancel() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public sealed class CefByteArrayResourceHandler : IResourceHandler {
}

bool IResourceHandler.Read(Stream dataOut, out int bytesRead, IResourceReadCallback callback) {
return logic.Read(WriteToOut, dataOut, out bytesRead, callback);
return logic.Read(WriteToOut, dataOut, dataOut.Length, out bytesRead, callback);
}

bool IResourceHandler.ProcessRequest(IRequest request, ICallback callback) {
Expand Down

0 comments on commit 61cd632

Please sign in to comment.