Skip to content

Commit

Permalink
Add 0/null checks for various cases in the COM/dynamic VARIANT extens…
Browse files Browse the repository at this point in the history
…ions (#99631)
  • Loading branch information
jkoritzinsky authored and pull[bot] committed Oct 8, 2024
1 parent 2492df1 commit 2883584
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ private static unsafe ref T GetByRefDataRef<T>(this ref ComVariant variant)
return ref Unsafe.AsRef<T>((void*)variant.GetRawDataRef<nint>());
}

public static unsafe void CopyFromIndirect(this ref ComVariant variant, object value)
public static unsafe void CopyFromIndirect(this ref ComVariant variant, object? value)
{
VarEnum vt = (VarEnum)(((int)variant.VarType) & ~((int)VarEnum.VT_BYREF));

Expand Down Expand Up @@ -154,9 +154,8 @@ public static unsafe void CopyFromIndirect(this ref ComVariant variant, object v
VarEnum.VT_DECIMAL => variant.As<decimal>(),
VarEnum.VT_CY => decimal.FromOACurrency(variant.GetRawDataRef<long>()),
VarEnum.VT_DATE => variant.As<DateTime>(),
VarEnum.VT_BSTR => Marshal.PtrToStringBSTR(variant.GetRawDataRef<nint>()),
VarEnum.VT_UNKNOWN => Marshal.GetObjectForIUnknown(variant.GetRawDataRef<nint>()),
VarEnum.VT_DISPATCH => Marshal.GetObjectForIUnknown(variant.GetRawDataRef<nint>()),
VarEnum.VT_BSTR => variant.GetRawDataRef<nint>() is 0 ? null : Marshal.PtrToStringBSTR(variant.GetRawDataRef<nint>()),
VarEnum.VT_UNKNOWN or VarEnum.VT_DISPATCH => variant.GetRawDataRef<nint>() is 0 ? null : Marshal.GetObjectForIUnknown(variant.GetRawDataRef<nint>()),
_ => GetObjectFromNativeVariant(ref variant),
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -379,14 +379,14 @@ public static void SetBstr(this ref ComVariant variant, string value)
variant = ComVariant.Create(new BStrWrapper(value));
}

public static void SetUnknown(this ref ComVariant variant, object value)
public static void SetUnknown(this ref ComVariant variant, object? value)
{
variant = ComVariant.CreateRaw(VarEnum.VT_UNKNOWN, Marshal.GetIUnknownForObject(value));
variant = ComVariant.CreateRaw(VarEnum.VT_UNKNOWN, value is null ? IntPtr.Zero : Marshal.GetIUnknownForObject(value));
}

public static void SetDispatch(this ref ComVariant variant, object value)
public static void SetDispatch(this ref ComVariant variant, object? value)
{
variant = ComVariant.CreateRaw(VarEnum.VT_DISPATCH, Marshal.GetIDispatchForObject(value));
variant = ComVariant.CreateRaw(VarEnum.VT_DISPATCH, value is null ? IntPtr.Zero : Marshal.GetIDispatchForObject(value));
}

public static void SetError(this ref ComVariant variant, int value)
Expand Down
3 changes: 3 additions & 0 deletions src/tests/Interop/COM/Dynamic/BasicTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,9 @@ private void Null()

obj.String_Property = null;
Assert.Equal(string.Empty, obj.String_Property);

obj.Dispatch_Property = new DispatchWrapper(null);
Assert.Null(obj.Dispatch_Property);
}

private void StringWrapper(string toWrap, string expected)
Expand Down

0 comments on commit 2883584

Please sign in to comment.