[Mono-bugs] [Bug 676722] New: New Rule - Validate (non Flag) Enum parameters before using

bugzilla_noreply at novell.com bugzilla_noreply at novell.com
Thu Mar 3 11:13:34 EST 2011


https://bugzilla.novell.com/show_bug.cgi?id=676722

https://bugzilla.novell.com/show_bug.cgi?id=676722#c0


           Summary: New Rule - Validate (non Flag) Enum parameters before
                    using
    Classification: Mono
           Product: Mono: Tools
           Version: 2.10.x
          Platform: All
        OS/Version: All
            Status: NEW
          Severity: Enhancement
          Priority: P5 - None
         Component: Gendarme
        AssignedTo: mono-bugs at lists.ximian.com
        ReportedBy: ethan_j_brown at hotmail.com
         QAContact: mono-bugs at lists.ximian.com
          Found By: ---
           Blocker: ---


User-Agent:       Mozilla/5.0 (Windows; Windows NT 6.1) AppleWebKit/534.23
(KHTML, like Gecko) Chrome/11.0.686.1 Safari/534.23

Enums can break type safety without too much trouble, since they're basically
implemented as const ints.

For instance, consider the code below where enum Test is passed as a parameter.
 The compiler allows the value 3, which is not defined in the enum, to be
passed to a method.  Consider a situation where this value was written to a
database or similar.

Enums decorated with [Flags] should be ignored.

Performing a switch (value) { case Test.One: break; case Test.Two: break;
default: throw ... ; } would be sufficient to not cause a violation.

Calling Enum.IsDefined and throwing would also not cause a violation.


Consider the quick code snippet below:


using System;
using System.Collections.Generic;

public class MyClass
{
    public enum Test
    {
        One = 1,
        Two = 2        
    }

    public static void UseEnum(Test test)
    {
        //consider doing something dangerous with the enum
        Console.WriteLine("Enum is defined? {0}", Enum.IsDefined(typeof(Test),
test));
    }

    public static void Main()
    {
        try
        {
            UseEnum((Test)3);
        }
        finally
        {
            Console.Write("Press any key to continue...");
            Console.ReadKey();
        }
    }
}





Reproducible: Always

Steps to Reproduce:
1.
2.
3.

-- 
Configure bugmail: https://bugzilla.novell.com/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the QA contact for the bug.
You are the assignee for the bug.


More information about the mono-bugs mailing list