From cf37047a7c77e4ce7d76bbbfd93a042d59ac2771 Mon Sep 17 00:00:00 2001 From: Bruno da Silva Belo Date: Mon, 23 Dec 2019 22:15:50 +0000 Subject: [PATCH] c#: Checking null for parameters. Checking for non-generated code. ref T8399 Reviewed-by: Felipe Magno de Almeida Differential Revision: https://phab.enlightenment.org/D10959 --- src/bindings/mono/eina_mono/eina_array.cs | 2 ++ src/bindings/mono/eina_mono/eina_binbuf.cs | 4 +++ src/bindings/mono/eina_mono/eina_inarray.cs | 2 ++ src/bindings/mono/eina_mono/eina_inlist.cs | 2 ++ src/bindings/mono/eina_mono/eina_list.cs | 2 ++ src/bindings/mono/eina_mono/eina_promises.cs | 4 ++- src/bindings/mono/eina_mono/eina_slice.cs | 2 ++ src/bindings/mono/eina_mono/eina_value.cs | 28 ++++++++++++++++++- .../mono/eldbus_mono/eldbus_common.cs | 23 +++++++++++++++ .../mono/eldbus_mono/eldbus_message.cs | 2 ++ src/bindings/mono/eldbus_mono/eldbus_proxy.cs | 2 ++ 11 files changed, 71 insertions(+), 2 deletions(-) diff --git a/src/bindings/mono/eina_mono/eina_array.cs b/src/bindings/mono/eina_mono/eina_array.cs index 37251be6cd..ba3809bf81 100644 --- a/src/bindings/mono/eina_mono/eina_array.cs +++ b/src/bindings/mono/eina_mono/eina_array.cs @@ -20,6 +20,7 @@ using System.Runtime.InteropServices; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics.CodeAnalysis; +using System.Diagnostics.Contracts; using static Eina.TraitFunctions; using static Eina.ArrayNativeFunctions; @@ -468,6 +469,7 @@ public class Array : IList, IEnumerable, IDisposable /// public bool Append(T[] values) { + Contract.Requires(values != null, nameof(values)); foreach (T v in values) { if (!Push(v)) diff --git a/src/bindings/mono/eina_mono/eina_binbuf.cs b/src/bindings/mono/eina_mono/eina_binbuf.cs index 24e93e8d9d..6cc79f9625 100644 --- a/src/bindings/mono/eina_mono/eina_binbuf.cs +++ b/src/bindings/mono/eina_mono/eina_binbuf.cs @@ -18,6 +18,7 @@ using System; using System.Runtime.InteropServices; using System.ComponentModel; +using System.Diagnostics.Contracts; namespace Eina { @@ -214,6 +215,7 @@ public class Binbuf : IDisposable /// true on success, false if data could not be appended. public bool Append(byte[] str) { + Contract.Requires(str != null, nameof(str)); return 0 != eina_binbuf_append_length(Handle, str, (UIntPtr)(str.Length)); } @@ -238,6 +240,7 @@ public class Binbuf : IDisposable /// true on success, false if data could not be appended. public bool Append(Binbuf bb) { + Contract.Requires(bb != null, nameof(bb)); return 0 != eina_binbuf_append_buffer(Handle, bb.Handle); } @@ -273,6 +276,7 @@ public class Binbuf : IDisposable /// true on success, false if data could not be appended. public bool Insert(byte[] str, uint pos) { + Contract.Requires(str != null, nameof(str)); return 0 != eina_binbuf_insert_length(Handle, str, (UIntPtr)(str.Length), (UIntPtr)pos); } diff --git a/src/bindings/mono/eina_mono/eina_inarray.cs b/src/bindings/mono/eina_mono/eina_inarray.cs index b88570833d..6bb8626e8a 100644 --- a/src/bindings/mono/eina_mono/eina_inarray.cs +++ b/src/bindings/mono/eina_mono/eina_inarray.cs @@ -20,6 +20,7 @@ using System.Runtime.InteropServices; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics.CodeAnalysis; +using System.Diagnostics.Contracts; using static Eina.TraitFunctions; using static Eina.InarrayNativeFunctions; @@ -485,6 +486,7 @@ public class Inarray : IEnumerable, IDisposable /// true on success, false otherwise. public bool Append(T[] values) { + Contract.Requires(values != null, nameof(values)); foreach (T v in values) { if (Push(v) == -1) diff --git a/src/bindings/mono/eina_mono/eina_inlist.cs b/src/bindings/mono/eina_mono/eina_inlist.cs index cef686b469..3d2670e169 100644 --- a/src/bindings/mono/eina_mono/eina_inlist.cs +++ b/src/bindings/mono/eina_mono/eina_inlist.cs @@ -20,6 +20,7 @@ using System.Runtime.InteropServices; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics.CodeAnalysis; +using System.Diagnostics.Contracts; using static Eina.TraitFunctions; using static Eina.InlistNativeFunctions; @@ -422,6 +423,7 @@ public class Inlist : IEnumerable, IDisposable /// The values to be added. public void AppendArray(T[] values) { + Contract.Requires(values != null, nameof(values)); foreach (T v in values) { Append(v); diff --git a/src/bindings/mono/eina_mono/eina_list.cs b/src/bindings/mono/eina_mono/eina_list.cs index 667b80834f..f4882d0afc 100644 --- a/src/bindings/mono/eina_mono/eina_list.cs +++ b/src/bindings/mono/eina_mono/eina_list.cs @@ -20,6 +20,7 @@ using System.Runtime.InteropServices; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics.CodeAnalysis; +using System.Diagnostics.Contracts; using static Eina.TraitFunctions; using static Eina.ListNativeFunctions; @@ -533,6 +534,7 @@ public class List : IList, IEnumerable, IDisposable /// The values to be appended. public void Append(T[] values) { + Contract.Requires(values != null, nameof(values)); RequireWritable(); foreach (T v in values) diff --git a/src/bindings/mono/eina_mono/eina_promises.cs b/src/bindings/mono/eina_mono/eina_promises.cs index 4b14a0d894..9f725add3c 100644 --- a/src/bindings/mono/eina_mono/eina_promises.cs +++ b/src/bindings/mono/eina_mono/eina_promises.cs @@ -18,7 +18,7 @@ using System.Runtime.InteropServices; using System.Collections.Generic; using System.Linq; using System.ComponentModel; - +using System.Diagnostics.Contracts; using static Eina.EinaNative.PromiseNativeMethods; @@ -207,6 +207,7 @@ public class Promise : IDisposable /// public void Resolve(Eina.Value value) { + Contract.Requires(value != null, nameof(value)); SanityChecks(); eina_promise_resolve(this.Handle, value); // Promise will take care of releasing this value correctly. @@ -280,6 +281,7 @@ public class Future /// The callback to be called when the attached promise resolves. public Future(Promise promise, ResolvedCb cb = null) { + Contract.Requires(promise != null, nameof(promise)); IntPtr intermediate = eina_future_new(promise.Handle); Handle = ThenRaw(intermediate, (Eina.Value value) => { diff --git a/src/bindings/mono/eina_mono/eina_slice.cs b/src/bindings/mono/eina_mono/eina_slice.cs index e47da1071c..912ddea3bd 100644 --- a/src/bindings/mono/eina_mono/eina_slice.cs +++ b/src/bindings/mono/eina_mono/eina_slice.cs @@ -18,6 +18,7 @@ using System; using System.Runtime.InteropServices; using System.ComponentModel; +using System.Diagnostics.Contracts; namespace Eina { @@ -235,6 +236,7 @@ public static class Eina_SliceUtils { public static byte[] GetBytes(this Eina.ISliceBase slc) { + Contract.Requires(slc != null, nameof(slc)); var size = (int)(slc.Len); byte[] mArray = new byte[size]; Marshal.Copy(slc.Mem, mArray, 0, size); diff --git a/src/bindings/mono/eina_mono/eina_value.cs b/src/bindings/mono/eina_mono/eina_value.cs index 5b203812da..46d1ff02b5 100644 --- a/src/bindings/mono/eina_mono/eina_value.cs +++ b/src/bindings/mono/eina_mono/eina_value.cs @@ -27,6 +27,7 @@ using System.Security; using System.Diagnostics.CodeAnalysis; using System.Runtime.Serialization; using System.Globalization; +using System.Diagnostics.Contracts; using static Eina.EinaNative.UnsafeNativeMethods; using static Eina.TraitFunctions; @@ -1268,8 +1269,10 @@ public class Value : IDisposable, IComparable, IEquatable /// Since EFL 1.23. /// /// The object to be wrapped. + [SuppressMessage("Microsoft.Design", "CA1062:ValidateArgumentsOfPublicMethods", Justification="Could not remove warning!")] public Value(object obj) : this() { + Contract.Requires(obj != null, nameof(obj)); var objType = obj.GetType(); if (objType == typeof(sbyte)) @@ -1424,6 +1427,7 @@ public class Value : IDisposable, IComparable, IEquatable /// The value to be copied. public Value(Value v) { + Contract.Requires(v != null, nameof(v)); Handle = Alloc(); if (!eina_value_copy(v.Handle, this.Handle)) { @@ -1615,7 +1619,11 @@ public class Value : IDisposable, IComparable, IEquatable public static implicit operator ValueNative(Value v) => ToValueNative(v); [EditorBrowsable(EditorBrowsableState.Never)] - public static ValueNative ToValueNative(Value v) => v.GetNative(); + public static ValueNative ToValueNative(Value v) + { + Contract.Requires(v != null, nameof(v)); + return v.GetNative(); + } /// Implicit conversion from native struct representation to managed wrapper. /// Since EFL 1.23. @@ -1655,6 +1663,7 @@ public class Value : IDisposable, IComparable, IEquatable /// The to be converted. public static byte ToByte(Value v) { + Contract.Requires(v != null, nameof(v)); byte b; v.Get(out b); @@ -1691,6 +1700,7 @@ public class Value : IDisposable, IComparable, IEquatable /// The to be converted. public static sbyte ToSByte(Value v) { + Contract.Requires(v != null, nameof(v)); sbyte b; v.Get(out b); @@ -1727,6 +1737,7 @@ public class Value : IDisposable, IComparable, IEquatable /// The to be converted. public static short ToInt16(Value v) { + Contract.Requires(v != null, nameof(v)); short b; v.Get(out b); @@ -1763,6 +1774,7 @@ public class Value : IDisposable, IComparable, IEquatable /// The to be converted. public static ushort ToUInt16(Value v) { + Contract.Requires(v != null, nameof(v)); ushort b; v.Get(out b); @@ -1800,6 +1812,7 @@ public class Value : IDisposable, IComparable, IEquatable /// The to be converted. public static int ToInt32(Value v) { + Contract.Requires(v != null, nameof(v)); int b; v.Get(out b); @@ -1836,6 +1849,7 @@ public class Value : IDisposable, IComparable, IEquatable /// The to be converted. public static uint ToUInt32(Value v) { + Contract.Requires(v != null, nameof(v)); uint b; v.Get(out b); @@ -1872,6 +1886,7 @@ public class Value : IDisposable, IComparable, IEquatable /// The to be converted. public static long ToInt64(Value v) { + Contract.Requires(v != null, nameof(v)); long b; v.Get(out b); @@ -1908,6 +1923,7 @@ public class Value : IDisposable, IComparable, IEquatable /// The to be converted. public static ulong ToUInt64(Value v) { + Contract.Requires(v != null, nameof(v)); ulong b; v.Get(out b); @@ -1944,6 +1960,7 @@ public class Value : IDisposable, IComparable, IEquatable /// The to be converted. public static float ToSingle(Value v) { + Contract.Requires(v != null, nameof(v)); float b; v.Get(out b); @@ -1980,6 +1997,7 @@ public class Value : IDisposable, IComparable, IEquatable /// The to be converted. public static double ToDouble(Value v) { + Contract.Requires(v != null, nameof(v)); double b; v.Get(out b); @@ -2016,6 +2034,7 @@ public class Value : IDisposable, IComparable, IEquatable /// The to be converted. public static string ToString(Value v) { + Contract.Requires(v != null, nameof(v)); string b; v.Get(out b); @@ -2171,6 +2190,7 @@ public class Value : IDisposable, IComparable, IEquatable /// The to be converted. public static Efl.Object ToObject(Value v) { + Contract.Requires(v != null, nameof(v)); Efl.Object obj; v.Get(out obj); @@ -2714,6 +2734,7 @@ public class Value : IDisposable, IComparable, IEquatable /// true if the value was successfully stored. public bool Set(Efl.Object value) { + Contract.Requires(value != null, nameof(value)); SanityChecks(); if (this.Optional) @@ -2735,6 +2756,7 @@ public class Value : IDisposable, IComparable, IEquatable /// true if the value was successfully stored. public bool Set(Value value) { + Contract.Requires(value != null, nameof(value)); OptionalSanityChecks(); ValueType subtype = value.GetValueType(); @@ -3321,6 +3343,7 @@ public class Value : IDisposable, IComparable, IEquatable /// true if the value was successfully appended. public bool Append(object o) { + Contract.Requires(o != null, nameof(o)); ContainerSanityChecks(); switch (GetValueSubType()) @@ -3508,6 +3531,7 @@ public class Value : IDisposable, IComparable, IEquatable } set { + Contract.Requires(value != null, nameof(value)); ContainerSanityChecks(i); switch (GetValueSubType()) @@ -3650,6 +3674,7 @@ public class ValueMarshaler : ICustomMarshaler /// keeping the managed ownership. public IntPtr MarshalManagedToNative(object managedObj) { + Contract.Requires(managedObj != null, nameof(managedObj)); try { Value v = (Value)managedObj; @@ -3703,6 +3728,7 @@ public class ValueMarshalerOwn : ICustomMarshaler /// when not needed. public IntPtr MarshalManagedToNative(object managedObj) { + Contract.Requires(managedObj != null, nameof(managedObj)); try { Value v = (Value)managedObj; diff --git a/src/bindings/mono/eldbus_mono/eldbus_common.cs b/src/bindings/mono/eldbus_mono/eldbus_common.cs index 2606fe92c8..6ca2de007f 100644 --- a/src/bindings/mono/eldbus_mono/eldbus_common.cs +++ b/src/bindings/mono/eldbus_mono/eldbus_common.cs @@ -19,6 +19,7 @@ using System; using System.Runtime.InteropServices; using System.ComponentModel; using System.Diagnostics.CodeAnalysis; +using System.Diagnostics.Contracts; using static eldbus.EldbusMessageNativeFunctions; @@ -948,12 +949,14 @@ public class ByteMessageArgument : BasicMessageArgument [EditorBrowsable(EditorBrowsableState.Never)] protected override bool InternalAppendTo(eldbus.Message msg) { + Contract.Requires(msg != null, nameof(msg)); return eldbus_message_arguments_append(msg.Handle, Signature, value); } [EditorBrowsable(EditorBrowsableState.Never)] protected override bool InternalAppendTo(eldbus.MessageIterator iter) { + Contract.Requires(iter != null, nameof(iter)); return eldbus_message_iter_basic_append(iter.Handle, TypeCode, value); } } @@ -997,12 +1000,14 @@ public class BoolMessageArgument : BasicMessageArgument [EditorBrowsable(EditorBrowsableState.Never)] protected override bool InternalAppendTo(eldbus.Message msg) { + Contract.Requires(msg != null, nameof(msg)); return eldbus_message_arguments_append(msg.Handle, Signature, value); } [EditorBrowsable(EditorBrowsableState.Never)] protected override bool InternalAppendTo(eldbus.MessageIterator iter) { + Contract.Requires(iter != null, nameof(iter)); return eldbus_message_iter_basic_append(iter.Handle, TypeCode, value); } } @@ -1046,12 +1051,14 @@ public class Int16MessageArgument : BasicMessageArgument [EditorBrowsable(EditorBrowsableState.Never)] protected override bool InternalAppendTo(eldbus.Message msg) { + Contract.Requires(msg != null, nameof(msg)); return eldbus_message_arguments_append(msg.Handle, Signature, value); } [EditorBrowsable(EditorBrowsableState.Never)] protected override bool InternalAppendTo(eldbus.MessageIterator iter) { + Contract.Requires(iter != null, nameof(iter)); return eldbus_message_iter_basic_append(iter.Handle, TypeCode, value); } } @@ -1095,12 +1102,14 @@ public class UInt16MessageArgument : BasicMessageArgument [EditorBrowsable(EditorBrowsableState.Never)] protected override bool InternalAppendTo(eldbus.Message msg) { + Contract.Requires(msg != null, nameof(msg)); return eldbus_message_arguments_append(msg.Handle, Signature, value); } [EditorBrowsable(EditorBrowsableState.Never)] protected override bool InternalAppendTo(eldbus.MessageIterator iter) { + Contract.Requires(iter != null, nameof(iter)); return eldbus_message_iter_basic_append(iter.Handle, TypeCode, value); } } @@ -1144,12 +1153,14 @@ public class Int32MessageArgument : BasicMessageArgument [EditorBrowsable(EditorBrowsableState.Never)] protected override bool InternalAppendTo(eldbus.Message msg) { + Contract.Requires(msg != null, nameof(msg)); return eldbus_message_arguments_append(msg.Handle, Signature, value); } [EditorBrowsable(EditorBrowsableState.Never)] protected override bool InternalAppendTo(eldbus.MessageIterator iter) { + Contract.Requires(iter != null, nameof(iter)); return eldbus_message_iter_basic_append(iter.Handle, TypeCode, value); } } @@ -1193,12 +1204,14 @@ public class UInt32MessageArgument : BasicMessageArgument [EditorBrowsable(EditorBrowsableState.Never)] protected override bool InternalAppendTo(eldbus.Message msg) { + Contract.Requires(msg != null, nameof(msg)); return eldbus_message_arguments_append(msg.Handle, Signature, value); } [EditorBrowsable(EditorBrowsableState.Never)] protected override bool InternalAppendTo(eldbus.MessageIterator iter) { + Contract.Requires(iter != null, nameof(iter)); return eldbus_message_iter_basic_append(iter.Handle, TypeCode, value); } } @@ -1242,12 +1255,14 @@ public class Int64MessageArgument : BasicMessageArgument [EditorBrowsable(EditorBrowsableState.Never)] protected override bool InternalAppendTo(eldbus.Message msg) { + Contract.Requires(msg != null, nameof(msg)); return eldbus_message_arguments_append(msg.Handle, Signature, value); } [EditorBrowsable(EditorBrowsableState.Never)] protected override bool InternalAppendTo(eldbus.MessageIterator iter) { + Contract.Requires(iter != null, nameof(iter)); return eldbus_message_iter_basic_append(iter.Handle, TypeCode, value); } } @@ -1291,12 +1306,14 @@ public class UInt64MessageArgument : BasicMessageArgument [EditorBrowsable(EditorBrowsableState.Never)] protected override bool InternalAppendTo(eldbus.Message msg) { + Contract.Requires(msg != null, nameof(msg)); return eldbus_message_arguments_append(msg.Handle, Signature, value); } [EditorBrowsable(EditorBrowsableState.Never)] protected override bool InternalAppendTo(eldbus.MessageIterator iter) { + Contract.Requires(iter != null, nameof(iter)); return eldbus_message_iter_basic_append(iter.Handle, TypeCode, value); } } @@ -1340,12 +1357,14 @@ public class DoubleMessageArgument : BasicMessageArgument [EditorBrowsable(EditorBrowsableState.Never)] protected override bool InternalAppendTo(eldbus.Message msg) { + Contract.Requires(msg != null, nameof(msg)); return eldbus_message_arguments_append(msg.Handle, Signature, value); } [EditorBrowsable(EditorBrowsableState.Never)] protected override bool InternalAppendTo(eldbus.MessageIterator iter) { + Contract.Requires(iter != null, nameof(iter)); return eldbus_message_iter_basic_append(iter.Handle, TypeCode, value); } } @@ -1371,12 +1390,14 @@ public abstract class StringLikeMessageArgument : BasicMessageArgument [EditorBrowsable(EditorBrowsableState.Never)] protected override bool InternalAppendTo(eldbus.Message msg) { + Contract.Requires(msg != null, nameof(msg)); return eldbus_message_arguments_append(msg.Handle, Signature, value); } [EditorBrowsable(EditorBrowsableState.Never)] protected override bool InternalAppendTo(eldbus.MessageIterator iter) { + Contract.Requires(iter != null, nameof(iter)); return eldbus_message_iter_basic_append(iter.Handle, TypeCode, value); } } @@ -1522,12 +1543,14 @@ public class UnixFdMessageArgument : BasicMessageArgument [EditorBrowsable(EditorBrowsableState.Never)] protected override bool InternalAppendTo(eldbus.Message msg) { + Contract.Requires(msg != null, nameof(msg)); return eldbus_message_arguments_append(msg.Handle, Signature, value); } [EditorBrowsable(EditorBrowsableState.Never)] protected override bool InternalAppendTo(eldbus.MessageIterator iter) { + Contract.Requires(iter != null, nameof(iter)); return eldbus_message_iter_basic_append(iter.Handle, TypeCode, value); } } diff --git a/src/bindings/mono/eldbus_mono/eldbus_message.cs b/src/bindings/mono/eldbus_mono/eldbus_message.cs index 71baf6d83f..8ab48ba2bd 100644 --- a/src/bindings/mono/eldbus_mono/eldbus_message.cs +++ b/src/bindings/mono/eldbus_mono/eldbus_message.cs @@ -18,6 +18,7 @@ using System; using System.Runtime.InteropServices; using System.ComponentModel; +using System.Diagnostics.Contracts; using static eldbus.EldbusMessageNativeFunctions; @@ -773,6 +774,7 @@ public class MessageIterator /// A . public eldbus.MessageIterator AppendOpenContainer(string signature) { + Contract.Requires(signature != null, nameof(signature)); CheckHandle(); IntPtr new_iter = IntPtr.Zero; diff --git a/src/bindings/mono/eldbus_mono/eldbus_proxy.cs b/src/bindings/mono/eldbus_mono/eldbus_proxy.cs index a3a984df97..8ed9bed82b 100644 --- a/src/bindings/mono/eldbus_mono/eldbus_proxy.cs +++ b/src/bindings/mono/eldbus_mono/eldbus_proxy.cs @@ -18,6 +18,7 @@ using System; using System.Runtime.InteropServices; using System.ComponentModel; +using System.Diagnostics.Contracts; using static eldbus.EldbusProxyNativeFunctions; @@ -123,6 +124,7 @@ public class Proxy : IDisposable /// The interface name. public Proxy(eldbus.Object obj, string _interface) { + Contract.Requires(obj != null, nameof(obj)); InitNew(eldbus_proxy_get(obj.Handle, _interface), true); }