Hi, This patch stops the compiler from bombing out when it tries to parse a literal which is too big to fit into int/float/whatever. It will now give an error and continue the compilation using an appropriately typed zero value in place of the literal.
I've attached the patch, and a tarball containing five new tests which should go into mcs/errors. Andrew - www.tardis.ed.ac.uk/~adb -
? patch
? class/corlib/Test/StreamWriterTest.temp
? errors/cs0594-2.cs
? errors/cs0594-3.cs
? errors/cs0594.cs
? errors/cs1021-2.cs
? errors/cs1021.cs
Index: mcs/cs-tokenizer.cs
===================================================================
RCS file: /mono/mcs/mcs/cs-tokenizer.cs,v
retrieving revision 1.55
diff -u -r1.55 cs-tokenizer.cs
--- mcs/cs-tokenizer.cs 13 Jun 2002 00:16:03 -0000 1.55
+++ mcs/cs-tokenizer.cs 26 Jun 2002 11:53:44 -0000
@@ -630,8 +630,15 @@
//
int adjust_int (int c)
{
- ulong ul = System.UInt64.Parse (number_builder.ToString ());
- return integer_type_suffix (ul, c);
+ try {
+ ulong ul = System.UInt64.Parse
+(number_builder.ToString ());
+ return integer_type_suffix (ul, c);
+ } catch (OverflowException) {
+ error_details = "Integral constant is too large";
+ Report.Error (1021, Location, error_details);
+ val = 0ul;
+ return Token.LITERAL_INTEGER;
+ }
}
int adjust_real (int t)
@@ -639,26 +646,38 @@
string s = number_builder.ToString ();
switch (t){
- case Token.LITERAL_DECIMAL:
- val = new System.Decimal ();
- val = System.Decimal.Parse (
- s, styles, csharp_format_info);
- break;
- case Token.LITERAL_FLOAT:
- val = new System.Double ();
- val = (float) System.Double.Parse (
- s, styles, csharp_format_info);
- break;
-
- case Token.LITERAL_DOUBLE:
- case Token.NONE:
- val = new System.Double ();
- val = System.Double.Parse (
- s, styles, csharp_format_info);
- t = Token.LITERAL_DOUBLE;
- break;
- }
- return t;
+ case Token.LITERAL_DECIMAL:
+ try {
+ val = System.Decimal.Parse (s, styles,
+csharp_format_info);
+ } catch (OverflowException) {
+ val = 0m;
+ error_details = "Floating-point
+constant is outside the range of the type 'decimal'";
+ Report.Error(594, Location,
+error_details);
+ }
+ break;
+ case Token.LITERAL_FLOAT:
+ try {
+ val = (float) System.Double.Parse (s,
+styles, csharp_format_info);
+ } catch (OverflowException) {
+ val = 0.0f;
+ error_details = "Floating-point
+constant is outside the range of the type 'float'";
+ Report.Error(594, Location,
+error_details);
+ }
+ break;
+
+ case Token.LITERAL_DOUBLE:
+ case Token.NONE:
+ t = Token.LITERAL_DOUBLE;
+ try {
+ val = System.Double.Parse (s, styles,
+csharp_format_info);
+ } catch (OverflowException) {
+ val = 0.0;
+ error_details = "Floating-point
+constant is outside the range of the type 'double'";
+ Report.Error(594, Location,
+error_details);
+ }
+ break;
+ }
+ return t;
}
//
@@ -679,8 +698,15 @@
string s = number_builder.ToString ();
- ul = System.UInt64.Parse (s,
NumberStyles.HexNumber);
- return integer_type_suffix (ul, peekChar ());
+ try {
+ ul = System.UInt64.Parse (s,
+NumberStyles.HexNumber);
+ return integer_type_suffix (ul,
+peekChar ());
+ } catch (OverflowException) {
+ error_details = "Integral constant is
+too large";
+ Report.Error (1021, Location,
+error_details);
+ val = 0ul;
+ return Token.LITERAL_INTEGER;
+ }
}
decimal_digits (c);
c = getChar ();
new-tests.tar.gz
Description: New files for mcs/errors
