Skip to content

Commit

Permalink
bugfix: Handle zero-len packets from ADB to avoid BSOD
Browse files Browse the repository at this point in the history
Older versions of ADB send zero-makers, but other tools also do so.
Handling these properly.

Related issues: #461 #410 #248

Co-authored-by: Dimitar Stankov <[email protected]>
Signed-off-by: Ivaylo Tsenkov <[email protected]>
  • Loading branch information
itsenkov and slonmron committed May 24, 2023
1 parent 81e89fb commit 60d205d
Showing 1 changed file with 22 additions and 13 deletions.
35 changes: 22 additions & 13 deletions Usbipd/AttachedClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -348,23 +348,32 @@ async Task HandleSubmitAsync(UsbIpHeaderBasic basic, UsbIpHeaderCmdSubmit submit
}
pending = true;

// Input or output, exceptions or not, this buffer must be locked until after the ioctl has completed.
var gcHandle = GCHandle.Alloc(buf, GCHandleType.Pinned);
try
if (buf.Length > 0)
{
urb.buf = gcHandle.AddrOfPinnedObject();
StructToBytes(urb, bytes);
ioctl = Device.IoControlAsync(SUPUSB_IOCTL.SEND_URB, bytes, bytes);
// Input or output, exceptions or not, this buffer must be locked until after the ioctl has completed.
var gcHandle = GCHandle.Alloc(buf, GCHandleType.Pinned);
try
{
urb.buf = gcHandle.AddrOfPinnedObject();
StructToBytes(urb, bytes);
ioctl = Device.IoControlAsync(SUPUSB_IOCTL.SEND_URB, bytes, bytes);
}
catch
{
gcHandle.Free();
throw;
}
_ = ioctl.ContinueWith((task) =>
{
gcHandle.Free();
}, CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default);
}
catch
else
{
gcHandle.Free();
throw;
urb.buf = IntPtr.Zero;
StructToBytes(urb, bytes);
ioctl = Device.IoControlAsync(SUPUSB_IOCTL.SEND_URB, bytes, bytes);
}
_ = ioctl.ContinueWith((task) =>
{
gcHandle.Free();
}, CancellationToken.None, TaskContinuationOptions.ExecuteSynchronously, TaskScheduler.Default);
}

// At this point we have initiated the ioctl (and possibly awaited it for special cases).
Expand Down

0 comments on commit 60d205d

Please sign in to comment.