Skip to content

Commit

Permalink
Merge pull request #950 from dorssel/devskim_unsafe
Browse files Browse the repository at this point in the history
Suppress DevSkim warning for unsafe
  • Loading branch information
dorssel committed May 12, 2024
2 parents 8ed1f21 + dccc104 commit d064ea1
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 23 deletions.
6 changes: 3 additions & 3 deletions Usbipd/CommandHandlersInstaller.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Task<ExitCode> ICommandHandlers.Install(IConsole console, CancellationToken canc
{
ConsoleTools.ReportInfo(console, $"Installing VBoxUSB");
// See: https://learn.microsoft.com/en-us/windows-hardware/drivers/install/preinstalling-driver-packages
unsafe
unsafe // DevSkim: ignore DS172412
{
fixed (char* inf = Path.Combine(path, "Drivers", "VBoxUSB.inf"))
{
Expand All @@ -44,7 +44,7 @@ Task<ExitCode> ICommandHandlers.Install(IConsole console, CancellationToken canc
console.ReportError($"OpenSCManager -> {Marshal.GetLastWin32Error()}");
return Task.FromResult(ExitCode.Failure);
}
unsafe
unsafe // DevSkim: ignore DS172412
{
using var service = PInvoke.CreateService(manager, "VBoxUSBMon", "VirtualBox USB Monitor Service",
(uint)GENERIC_ACCESS_RIGHTS.GENERIC_ALL, ENUM_SERVICE_TYPE.SERVICE_KERNEL_DRIVER, SERVICE_START_TYPE.SERVICE_DEMAND_START,
Expand All @@ -70,7 +70,7 @@ Task<ExitCode> ICommandHandlers.Uninstall(IConsole console, CancellationToken ca

{
ConsoleTools.ReportInfo(console, $"Uninstalling VBoxUSB");
unsafe
unsafe // DevSkim: ignore DS172412
{
BOOL needReboot;
if (!PInvoke.DiUninstallDriver(HWND.Null, Path.Combine(path, "Drivers", "VBoxUSB.inf"), 0, &needReboot))
Expand Down
14 changes: 7 additions & 7 deletions Usbipd/ConfigurationManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public static void ThrowOnError(this CONFIGRET configRet, string function)

public static uint Locate_DevNode(string instanceId, bool present)
{
unsafe
unsafe // DevSkim: ignore DS172412
{
fixed (char* pInstanceId = instanceId)
{
Expand All @@ -39,7 +39,7 @@ public static uint Locate_DevNode(string instanceId, bool present)

static string[] Get_Device_Interface_List(in Guid interfaceClassGuid, string? deviceId, CM_GET_DEVICE_INTERFACE_LIST_FLAGS flags)
{
unsafe
unsafe // DevSkim: ignore DS172412
{
fixed (char* pDeviceId = deviceId)
{
Expand All @@ -54,7 +54,7 @@ static string[] Get_Device_Interface_List(in Guid interfaceClassGuid, string? de
}
}

static unsafe object ConvertProperty(DEVPROPTYPE propertyType, byte* pBuffer, int propertyBufferSize)
static unsafe object ConvertProperty(DEVPROPTYPE propertyType, byte* pBuffer, int propertyBufferSize) // DevSkim: ignore DS172412
{
return propertyType switch
{
Expand All @@ -66,7 +66,7 @@ static unsafe object ConvertProperty(DEVPROPTYPE propertyType, byte* pBuffer, in

static object Get_Device_Interface_Property(string deviceInterface, in DEVPROPKEY devPropKey)
{
unsafe
unsafe // DevSkim: ignore DS172412
{
var propertyBufferSize = 0u;
var cr = PInvoke.CM_Get_Device_Interface_Property(deviceInterface, devPropKey, out var propertyType, null, ref propertyBufferSize, 0);
Expand All @@ -85,7 +85,7 @@ static object Get_Device_Interface_Property(string deviceInterface, in DEVPROPKE

static object Get_DevNode_Property(uint deviceNode, in DEVPROPKEY devPropKey)
{
unsafe
unsafe // DevSkim: ignore DS172412
{
var propertyBufferSize = 0u;
var cr = PInvoke.CM_Get_DevNode_Property(deviceNode, devPropKey, out var propertyType, null, ref propertyBufferSize, 0);
Expand Down Expand Up @@ -361,7 +361,7 @@ public static IEnumerable<string> GetOriginalDeviceIdsWithVBoxDriver()

public static void SetDeviceFriendlyName(uint deviceNode)
{
unsafe
unsafe // DevSkim: ignore DS172412
{
var friendlyName = "USBIP Shared Device";
fixed (char* pValue = friendlyName)
Expand All @@ -388,7 +388,7 @@ public RestartingDevice(string instanceId)
public RestartingDevice(uint deviceNode)
{
DeviceNode = deviceNode;
unsafe
unsafe // DevSkim: ignore DS172412
{
PNP_VETO_TYPE vetoType;
var vetoName = new string('\0', (int)PInvoke.MAX_PATH);
Expand Down
2 changes: 1 addition & 1 deletion Usbipd/ConnectedClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ async Task HandleRequestImportAsync(CancellationToken cancellationToken)
}, null, Timeout.Infinite, true);

// Detect unplug.
unsafe
unsafe // DevSkim: ignore DS172412
{
CM_NOTIFY_FILTER filter = new()
{
Expand Down
2 changes: 1 addition & 1 deletion Usbipd/DeviceFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public Task<uint> IoControlAsync(uint ioControlCode, byte[]? input, byte[]? outp
{
var taskCompletionSource = new TaskCompletionSource<uint>();

unsafe
unsafe // DevSkim: ignore DS172412
{
void OnCompletion(uint errorCode, uint numBytes, NativeOverlapped* nativeOverlapped)
{
Expand Down
29 changes: 21 additions & 8 deletions Usbipd/NewDev.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ static class NewDev
public static bool ForceVBoxDriver(string originalInstanceId)
{
BOOL reboot = false;
unsafe
{
// First, we must set a NULL driver to clear any existing Device Setup Class.
using var deviceInfoSet = PInvoke.SetupDiCreateDeviceInfoList((Guid?)null, default);
Expand All @@ -27,10 +26,16 @@ public static bool ForceVBoxDriver(string originalInstanceId)
{
cbSize = (uint)Marshal.SizeOf<SP_DEVINFO_DATA>(),
};
PInvoke.SetupDiOpenDeviceInfo(deviceInfoSet, originalInstanceId, default, 0, &deviceInfoData).ThrowOnError(nameof(PInvoke.SetupDiOpenDeviceInfo));
unsafe // DevSkim: ignore DS172412
{
PInvoke.SetupDiOpenDeviceInfo(deviceInfoSet, originalInstanceId, default, 0, &deviceInfoData).ThrowOnError(nameof(PInvoke.SetupDiOpenDeviceInfo));
}
BOOL tmpReboot;
PInvoke.DiInstallDevice(default, deviceInfoSet, deviceInfoData, null, DIINSTALLDEVICE_FLAGS.DIIDFLAG_INSTALLNULLDRIVER, &tmpReboot)
.ThrowOnError(nameof(DIINSTALLDEVICE_FLAGS.DIIDFLAG_INSTALLNULLDRIVER));
unsafe // DevSkim: ignore DS172412
{
PInvoke.DiInstallDevice(default, deviceInfoSet, deviceInfoData, null, DIINSTALLDEVICE_FLAGS.DIIDFLAG_INSTALLNULLDRIVER, &tmpReboot)
.ThrowOnError(nameof(DIINSTALLDEVICE_FLAGS.DIIDFLAG_INSTALLNULLDRIVER));
}
if (tmpReboot)
{
reboot = true;
Expand All @@ -41,7 +46,6 @@ public static bool ForceVBoxDriver(string originalInstanceId)
// 200 ms seems to work, so delay for 500 ms for good measure...
Thread.Sleep(TimeSpan.FromMilliseconds(500));

unsafe
{
// Now we can update the driver.
using var deviceInfoSet = PInvoke.SetupDiCreateDeviceInfoList((Guid?)null, default);
Expand All @@ -53,7 +57,10 @@ public static bool ForceVBoxDriver(string originalInstanceId)
{
cbSize = (uint)Marshal.SizeOf<SP_DEVINFO_DATA>(),
};
PInvoke.SetupDiOpenDeviceInfo(deviceInfoSet, originalInstanceId, default, 0, &deviceInfoData).ThrowOnError(nameof(PInvoke.SetupDiOpenDeviceInfo));
unsafe // DevSkim: ignore DS172412
{
PInvoke.SetupDiOpenDeviceInfo(deviceInfoSet, originalInstanceId, default, 0, &deviceInfoData).ThrowOnError(nameof(PInvoke.SetupDiOpenDeviceInfo));
}
var deviceInstallParams = new SP_DEVINSTALL_PARAMS_W()
{
cbSize = (uint)Marshal.SizeOf<SP_DEVINSTALL_PARAMS_W>(),
Expand All @@ -62,14 +69,20 @@ public static bool ForceVBoxDriver(string originalInstanceId)
DriverPath = @$"{RegistryUtils.InstallationFolder ?? throw new UnexpectedResultException("not installed")}\Drivers\VBoxUSB.inf",
};
PInvoke.SetupDiSetDeviceInstallParams(deviceInfoSet, deviceInfoData, deviceInstallParams).ThrowOnError(nameof(PInvoke.SetupDiSetDeviceInstallParams));
PInvoke.SetupDiBuildDriverInfoList(deviceInfoSet, &deviceInfoData, SETUP_DI_DRIVER_TYPE.SPDIT_CLASSDRIVER).ThrowOnError(nameof(PInvoke.SetupDiBuildDriverInfoList));
unsafe // DevSkim: ignore DS172412
{
PInvoke.SetupDiBuildDriverInfoList(deviceInfoSet, &deviceInfoData, SETUP_DI_DRIVER_TYPE.SPDIT_CLASSDRIVER).ThrowOnError(nameof(PInvoke.SetupDiBuildDriverInfoList));
}
var driverInfoData = new SP_DRVINFO_DATA_V2_W()
{
cbSize = (uint)Marshal.SizeOf<SP_DRVINFO_DATA_V2_W>(),
};
PInvoke.SetupDiEnumDriverInfo(deviceInfoSet, deviceInfoData, SETUP_DI_DRIVER_TYPE.SPDIT_CLASSDRIVER, 0, ref driverInfoData).ThrowOnError(nameof(PInvoke.SetupDiEnumDriverInfo));
BOOL tmpReboot;
PInvoke.DiInstallDevice(default, deviceInfoSet, deviceInfoData, driverInfoData, 0, &tmpReboot).ThrowOnError(nameof(PInvoke.DiInstallDevice));
unsafe // DevSkim: ignore DS172412
{
PInvoke.DiInstallDevice(default, deviceInfoSet, deviceInfoData, driverInfoData, 0, &tmpReboot).ThrowOnError(nameof(PInvoke.DiInstallDevice));
}
if (tmpReboot)
{
reboot = true;
Expand Down
2 changes: 1 addition & 1 deletion Usbipd/Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ static void EnablePrivilege(string name)
PInvoke.OpenProcessToken(currentProcess, TOKEN_ACCESS_MASK.TOKEN_ADJUST_PRIVILEGES, out var token).ThrowOnError(nameof(PInvoke.OpenProcessToken));
using (token)
{
unsafe
unsafe // DevSkim: ignore DS172412
{
PInvoke.AdjustTokenPrivileges(token, false, &tokenPrivileges, 0, null, null).ThrowOnError(nameof(PInvoke.AdjustTokenPrivileges));
}
Expand Down
4 changes: 2 additions & 2 deletions Usbipd/Tools.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public static async Task ReadMessageAsync(this Stream stream, Memory<byte> buf,
{
throw new ArgumentException($"buffer too small for structure: {bytes.Length} < {required}", nameof(bytes));
}
unsafe
unsafe // DevSkim: ignore DS172412
{
fixed (byte* dst = bytes)
{
Expand All @@ -75,7 +75,7 @@ public static async Task ReadMessageAsync(this Stream stream, Memory<byte> buf,
{
throw new ArgumentException($"buffer too small for structure: {bytes.Length} < {required}", nameof(bytes));
}
unsafe
unsafe // DevSkim: ignore DS172412
{
fixed (byte* src = bytes)
{
Expand Down

0 comments on commit d064ea1

Please sign in to comment.