google-code-export / dblinq2007

Automatically exported from code.google.com/p/dblinq2007
Other
0 stars 0 forks source link

Specifying invalid languages should not result in a NullReferenceException #268

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Grab mono 2.6.7 source tarball & complile
2. Grab dblinq from svn r1410 and compile from src/ using xbuild:

xbuild DbLinq.sln "/p:Configuration=Debug - Mono Strict"

3. Execute: ./DbMetal.exe --user=userid -password=xxx --database=Scramble  
--provider=MySql --debug --language=C#2 --code=foo.cs

What is the expected output? What do you see instead?
Creates an empty foo.cs, then throws a NullReferenceException:

DbLinq Database mapping generator 2008 version 0.20
for Microsoft (R) .NET Framework version 3.5
Distributed under the MIT licence (http://linq.to/db/license)

>>> Reading schema from MySQL database
<<< writing C# classes in file 'foo.cs'
DbMetal: System.NullReferenceException: Object reference not set to an instance 
of an object
  at DbMetal.Generator.CodeDomGenerator.Write (System.IO.TextWriter textWriter, DbLinq.Schema.Dbml.Database dbSchema, DbMetal.Generator.GenerationContext context) [0x00000] 
  at DbMetal.Generator.Implementation.Processor.GenerateCode (DbMetal.Parameters parameters, DbLinq.Schema.Dbml.Database dbSchema, ISchemaLoader schemaLoader, System.String filename) [0x00000] 
  at DbMetal.Generator.Implementation.Processor.WriteSchema (DbLinq.Schema.Dbml.Database dbSchema, ISchemaLoader schemaLoader, DbMetal.Parameters parameters) [0x00000] 
  at DbMetal.Generator.Implementation.Processor.ProcessSchema (DbMetal.Parameters parameters) [0x00000] 

What version of the product are you using? On what operating system?
mono 2.6.7
dblinq r1410 (Aug 4th 2010)
Ubuntu 64 bit, 10.04

Please provide any additional information below.

In CodeDomGenerator.cs, line 78:

public static CodeDomGenerator CreateFromLanguage(string language)

{

    return new CodeDomGenerator(CodeDomProvider.CreateProvider(language));

}

CreateFromLanguage probably should test if CodeDomProvider.CreateProvider is 
returning null, since the mono 2.6.7 runtime does not throw a 
ConfigurationException if it cannot find the language specified.

CodeDomProvider.cs: GetCompilerInfo does not test for null:

public static CompilerInfo GetCompilerInfo (string language)
{
    if (language == null)
        throw new ArgumentNullException ("language");
    if (Config == null)
        return null;
    CompilerCollection cc = Config.Compilers;
    return cc[language];
}

Nor does CompilerCollection (ComplierCollection.cs, ln 156):

public CompilerInfo GetCompilerInfoForExtension (string extension)
{
    if (compiler_extensions.Count == 0)
        return null;

    CompilerInfo ci;
    if (compiler_extensions.TryGetValue (extension, out ci))
        return ci;

    return null;
}

These methods are unchanged as of the latest github tip.

Changing CreateFromLanguage to something like this should do the trick:

public static CodeDomGenerator CreateFromLanguage(string language)
{
    CodeDomProvider cdp = CodeDomProvider.CreateProvider(language);

    if ( cdp == null )
        throw new ArgumentException( "Invalid language specified" );

    return new CodeDomGenerator(cdp);
}

Original issue reported on code.google.com by spurious...@gmail.com on 5 Aug 2010 at 3:45

GoogleCodeExporter commented 9 years ago
So the actual bug isn't that C#2 isn't supported, but rather that using C#2 
generates a NullReferenceException instead of something more useful.

Original comment by jonmpr...@gmail.com on 5 Aug 2010 at 1:23

GoogleCodeExporter commented 9 years ago
This is one of those slightly blurry usability issues. 

I assume C#2 is a valid language type for MS .Net and the assumption was that 
mono would support it. The dbmetal documentation says C#2 is supported, however 
mono does not return a CompilerInfo instance if "C#2" is passed to 
GetComplierInfo(). Additionally, mono does not throw the ConfigurationException 
exception that (according to the MS .Net docs for GetCompilerInfo) it should. 

So, the end result is someone uses C#2 because the dbmetal help says it is a 
valid option, and then gets a more cryptic null ref exception. 

So perhaps the error message should say something to the effect of "Language 
type not supported on this .net implementation." Then add an entry to a faq and 
probably modify the command-line help. 

Btw, I would be happy to make whatever changes you decide on.

Original comment by spurious...@gmail.com on 5 Aug 2010 at 2:00