GSoC/GCI Archive
Google Code-in 2010 Mono Project

Gendarme rule to always specify a FileAccess when using File.Open

completed by: Yuri Stuken

mentors: spouliot

Background

Gendarme is a static analysis tool to find problems in .NET software. Gendarme inspects executables and libraries that contain code in ECMA CIL format (Mono and .NET) and looks for common problems with the code, problems that compiler do not typically check or have not historically checked.

 

Task

The task is to write one Gendarme rule that check for file access calls that use FileMode.Open but that do not provide a (non-default) FileAccess. It's likely that the code expects a read-only file to work - but in reality it requires a read-write access.

This happens because the default implementation of File.Open (using only FileMode) looks like:

public static FileStream Open (string path, FileMode mode)
{
return new FileStream (path, mode, mode == FileMode.Append ? FileAccess.Write : FileAccess.ReadWrite, FileShare.None);
}
see https://github.com/mono/mono/blob/master/mcs/class/corlib/System.IO/File.cs

while, many times, people expect the call to success on a read-only file (e.g. https://bugzilla.novell.com/show_bug.cgi?id=MONO82617)

 

Deliverables

To complete the task the student must provide:

*  the rules, written in C#. The source code must follow the Mono source code guidelines (available at http://www.mono-project.com/Coding_Guidelines). Also a 'self-check' must be done one the code (i.e. you need to run gendarme on your own code) and the defects, if any, must be fixed (in doubt please ask for guidance on IRC).

* its documentation (in-source xmldoc) that includes 'good' and 'bad' examples; and

* the unit tests (also in C# using the gendarme helpers around NUnit) proving the rule is working as intended

Once ready the files (or the patch) can be submitted to the mentor for review.

 

Hints

* In order to avoid misunderstandings you should start by writing your unit tests and ask for a review of them. That will quickly tell you if you're on the right path to solve the problem.

* Gendarme provides hundreds of rules that can be used as example, here's one

rule: https://github.com/mono/mono-tools/blob/master/gendarme/rules/Gendarme.Rules.Interoperability/PInvokeShouldNotBeVisibleRule.cs

tests: https://github.com/mono/mono-tools/blob/master/gendarme/rules/Gendarme.Rules.Interoperability/Test/PInvokeShouldNotBeVisibleTest.cs

 

Resources

* Gendarme web site: http://www.mono-project.com/Gendarme

* Mailing-list / discussions: http://groups.google.com/group/gendarme

* IRC: #gendarme on GimpNET