Ejemplo n.º 1
0
        private static Bitmask <int> GetStringFormatExpectedParameters(string format)
        {
            Bitmask <int> result = new Bitmask <int> (false);

            if (format == null)
            {
                return(result);
            }

            // if last character is { then there's no digit after it
            for (int index = 0; index < format.Length - 1; index++)
            {
                if (format [index] != '{')
                {
                    continue;
                }

                char nextChar = format [index + 1];
                if (nextChar == '{')
                {
                    index++;                     // skip special {{
                    continue;
                }

                if (!char.IsDigit(nextChar))
                {
                    continue;
                }

                int value = nextChar - '0';

                index++;                 // next char is already added to value

                int tenPower = 1;
                while (index++ < format.Length)
                {
                    char current = format [index];
                    if (!char.IsDigit(current))
                    {
                        break;
                    }
                    tenPower *= 10;
                    value     = value * tenPower + current - '0';
                }

                if (index == format.Length)
                {
                    break;                     // Incorrect format
                }
                result.Set(value);
            }

            return(result);
        }
        private void CheckParameter(ParameterDefinition parameter)
        {
            // nothing to do if it could not be resolved
            if (parameter == null)
            {
                return;
            }

            // out parameters can't be null
            if (parameter.IsOut)
            {
                return;
            }

            int sequence = parameter.Index;

            // ldarg this - 'this' cannot be null
            if (sequence == -1)
            {
                return;
            }

            // was there a null check done before ?
            if (has_null_check.Get(sequence))
            {
                return;
            }

            // make sure we don't report it more than once
            has_null_check.Set(sequence);

            // ignore a value type parameter (as long as its not an array of value types)
            TypeReference ptype = parameter.ParameterType;
            // take care of references (ref)
            ByReferenceType rt = (ptype as ByReferenceType);

            if (rt != null)
            {
                ptype = rt.ElementType;
            }
            if (ptype.IsValueType && !ptype.IsArray)
            {
                return;
            }

            // last chance, if the type is a generic type constrained not to be nullable
            GenericParameter gp = (parameter.ParameterType as GenericParameter);

            if ((gp != null) && gp.HasNotNullableValueTypeConstraint)
            {
                return;
            }

            // nullable with no check

            // we assume a better control of family members so this is less severe
            Severity s = (parameter.Method as MethodDefinition).IsFamily ? Severity.Medium : Severity.High;

            Runner.Report(parameter, s, Confidence.Normal);
        }
        void CheckReassignment(MethodDefinition method, Instruction ins)
        {
            VariableDefinition v = ins.GetVariable(method);
            ulong index          = (ulong)v.Index;

            if (locals.Get(index))
            {
                string msg = String.Format(CultureInfo.InvariantCulture,
                                           "Local {0}is not disposed before being re-assigned.",
                                           GetFriendlyNameOrEmpty(v));
                Runner.Report(method, ins, Severity.High, Confidence.Normal, msg);
            }
            else
            {
                locals.Set(index);
            }
        }
Ejemplo n.º 4
0
		public void Intersect ()
		{
			Bitmask<long> x = new Bitmask<long> ();
			Assert.IsTrue (x.Intersect (null), "null"); // special case since they are equals
			Assert.IsFalse (x.Intersect (x), "self");

			Bitmask<long> all = new Bitmask<long> ();
			all.SetAll ();
			Assert.IsFalse (x.Intersect (all), "x N all");
			Assert.IsFalse (all.Intersect (x), "all N x");

			x.Set (0);
			Assert.IsTrue (x.Intersect (all), "1 N all");
			Assert.IsTrue (all.Intersect (x), "all N 1");

			Assert.IsTrue (x.Intersect (x), "self 1");
		}
Ejemplo n.º 5
0
		public void BitmaskSeverity ()
		{
			Bitmask<Severity> x = new Bitmask<Severity> ();
			Assert.IsFalse (x.Get (Severity.Audit), "Empty/Audit");
			Assert.AreEqual (0, x.GetHashCode (), "Empty/GetHashCode");
			Assert.AreEqual ("0", x.ToString (), "Empty/ToString");
	
			x.Set (Severity.Audit);
			Assert.IsTrue (x.Get (Severity.Audit), "Set/Audit");
			Assert.AreEqual (16, x.GetHashCode (), "GetHashCode");
			Assert.AreEqual ("10000", x.ToString (), "ToString");
			Assert.IsTrue (x.Equals (x), "Equals(self)");
			Assert.IsTrue (x.Equals ((object) x), "Equals((object)self)");
			Assert.IsFalse (x.Equals (Severity.Audit), "Equals");

			x.Clear (Severity.Audit);
			Assert.IsFalse (x.Get (Severity.Audit), "Clear/Audit");
		}
Ejemplo n.º 6
0
        public void IsSubsetOf()
        {
            Bitmask <long> x = new Bitmask <long> ();

            Assert.IsFalse(x.IsSubsetOf(null), "null");
            Assert.IsTrue(x.IsSubsetOf(x), "self");

            Bitmask <long> all = new Bitmask <long> ();

            all.SetAll();
            Assert.IsTrue(x.IsSubsetOf(all), "x < all");
            Assert.IsFalse(all.IsSubsetOf(x), "all < x");

            x.Set(0);
            Assert.IsTrue(x.IsSubsetOf(all), "1 < all");
            Assert.IsFalse(all.IsSubsetOf(x), "all < 1");

            Assert.IsTrue(x.IsSubsetOf(x), "self 1");
        }
Ejemplo n.º 7
0
        public void Intersect()
        {
            Bitmask <long> x = new Bitmask <long> ();

            Assert.IsTrue(x.Intersect(null), "null");               // special case since they are equals
            Assert.IsFalse(x.Intersect(x), "self");

            Bitmask <long> all = new Bitmask <long> ();

            all.SetAll();
            Assert.IsFalse(x.Intersect(all), "x N all");
            Assert.IsFalse(all.Intersect(x), "all N x");

            x.Set(0);
            Assert.IsTrue(x.Intersect(all), "1 N all");
            Assert.IsTrue(all.Intersect(x), "all N 1");

            Assert.IsTrue(x.Intersect(x), "self 1");
        }
Ejemplo n.º 8
0
        public void BitmaskSeverity()
        {
            Bitmask <Severity> x = new Bitmask <Severity> ();

            Assert.IsFalse(x.Get(Severity.Audit), "Empty/Audit");
            Assert.AreEqual(0, x.GetHashCode(), "Empty/GetHashCode");
            Assert.AreEqual("0", x.ToString(), "Empty/ToString");

            x.Set(Severity.Audit);
            Assert.IsTrue(x.Get(Severity.Audit), "Set/Audit");
            Assert.AreEqual(16, x.GetHashCode(), "GetHashCode");
            Assert.AreEqual("10000", x.ToString(), "ToString");
            Assert.IsTrue(x.Equals(x), "Equals(self)");
            Assert.IsTrue(x.Equals((object)x), "Equals((object)self)");
            Assert.IsFalse(x.Equals(Severity.Audit), "Equals");

            x.Clear(Severity.Audit);
            Assert.IsFalse(x.Get(Severity.Audit), "Clear/Audit");
        }
Ejemplo n.º 9
0
        private bool FindBadParameters(Collection <ParameterDefinition> parameters)
        {
            // Every uri parameter that is a string is recorded by the bitmask.
            // Note: we're assuming the number of parameters will fit into the bitmask.
            methodBitmask.ClearAll();
            var defect = false;

            for (int i = 0; i < parameters.Count; i++)
            {
                var param = parameters [i];
                var type  = param.ParameterType;
                if (IsOkay(type, param.Name))
                {
                    continue;
                }
                defect = true;
                methodBitmask.Set(1 << i);
            }

            return(defect);
        }
Ejemplo n.º 10
0
		public void IsSubsetOf ()
		{
			Bitmask<long> x = new Bitmask<long> ();
			Assert.IsFalse (x.IsSubsetOf (null), "null");
			Assert.IsTrue (x.IsSubsetOf (x), "self");

			Bitmask<long> all = new Bitmask<long> ();
			all.SetAll ();
			Assert.IsTrue (x.IsSubsetOf (all), "x < all");
			Assert.IsFalse (all.IsSubsetOf (x), "all < x");

			x.Set (0);
			Assert.IsTrue (x.IsSubsetOf (all), "1 < all");
			Assert.IsFalse (all.IsSubsetOf (x), "all < 1");

			Assert.IsTrue (x.IsSubsetOf (x), "self 1");
		}
		private static Bitmask<int> GetStringFormatExpectedParameters (string format)
		{
			Bitmask<int> result = new Bitmask<int> (false);

			if (format == null)
				return result;

			// if last character is { then there's no digit after it
			for (int index = 0; index < format.Length - 1; index++) {
				if (format [index] != '{')
					continue;

				char nextChar = format [index + 1];
				if (nextChar == '{') {
					index++; // skip special {{
					continue;
				}

				if (!char.IsDigit (nextChar))
					continue;

				int value = nextChar - '0';

				index++; // next char is already added to value

				int tenPower = 1;
				while (index++ < format.Length) {
					char current = format [index];
					if (!char.IsDigit (current))
						break;
					tenPower *= 10;
					value = value * tenPower + current - '0';
				}

				if (index == format.Length)
					break; // Incorrect format

				result.Set (value);
			}

			return result;
		}