[antlr-interest] Antlr-3.2 C# Target: RecognitionException.csConstructor Bug

Sam Harwell sharwell at pixelminegames.com
Tue Dec 15 13:07:26 PST 2009


RecognitionException is the base class for syntactical exceptions found
by the generated parser. For other semantics exceptions, such as the
function call validation you're referring to, you should throw another
exception relevant to your compilation process (one not derived from
RecognitionException).

I'm going to update the constructor of RecognitionException to throw an
ArgumentNullException if `input` is null. This is part of my recent work
on validating parameters of all public-facing members inside the CSharp3
runtime. I'm using the static analysis tools of Code Contracts library
to make sure I cover everything without introducing any additional
references in the final binaries.

Sam

-----Original Message-----
From: antlr-interest-bounces at antlr.org
[mailto:antlr-interest-bounces at antlr.org] On Behalf Of John Moses
Sent: Tuesday, December 15, 2009 1:38 PM
To: antlr-interest at antlr.org
Subject: [antlr-interest] Antlr-3.2 C# Target:
RecognitionException.csConstructor Bug

Howdy,

I believe I'm running into a bug in the constructors of class
RecognitionException in the CSharp code of Antlr.Runtime.

5 of the 6 constructors of RecognitionException call this version (with
various parameters 'nulled' out):
	public RecognitionException(string message, Exception inner,
IIntStream input)

3 of the 6 call the above constructor with null for the IIntStream input
parameter. Unfortunately, the 2nd line of this constructor is:
	this.index = input.Index();
which immediately throws the infamous 'Object reference not set to an
instance of an object' exception (since input is null).

The most obvious way to encounter the bug is to throw a new
RecognitionException upon discovery of some
'otherwise-not-an-Antlr-exception' situation (like the number of
expressions in an expression_list doesn't match what a function call
needed), e.g.:
	throw new RecognitionException("Improper number of parameters in
function");

(Just a simple example, in real life I do a better job of reporting an
error than this! :-)

My workaround is either:
	1) Capture the IIntStream from earlier in the process and call a
different constructor:
	throw new RecognitionException("Improper blah,
blah",MyCapturedIIntStream);
	2) Throw an exception unrelated to RecognitionException.

But it should be easy to fix.
Thanks,

-John

List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe:
http://www.antlr.org/mailman/options/antlr-interest/your-email-address


More information about the antlr-interest mailing list