#115 NumberFormatException in MagicNumberCheck

release_3.2
closed
Rick Giles
5
2012-10-10
2003-06-04
Andreas Holzmann
No

With the following code you get a NumberFormatException in
the MagicNumberCheck

-- begin class source
public class Number {
public Number () {
long l = 0xffffffff;
}
}
-- end class source

-- begin shell
> java -jar checkstyle-all-3.1.jar -c testconfig.xml
Number.java
Starting audit...
Number.java:0: Got an exception -
java.lang.NumberFormatException: For input string:
"ffffffff"
Audit done.
-- end shell

Using a smaller number 0xfffffff (one f removed)
results in the
correct answer:
Number.java:4:18: '0xfffffff' is a magic number.

Discussion

  • Rick Giles
    Rick Giles
    2003-06-04

    Logged In: YES
    user_id=539926

    Fixed and committed to 3.2 CVS

     
  • Dan Lacher
    Dan Lacher
    2004-02-04

    Logged In: YES
    user_id=968204

    % cat Foo.java
    public class Test {
    public static final long TESTVAL = 0xFE00000000000000;
    }

    With the above code, I am still seeing this bug using 3.3.

    Foo.java:0: Got an exception - java.lang.NumberFormatException: For
    input string: "FE00000000000000"

     
  • Rick Giles
    Rick Giles
    2004-02-04

    Logged In: YES
    user_id=539926

    The code does not compile. A checkstyle precondition is that
    the code compiles, so this is not a bug.

    javac LongConst.java
    LongConst.java:3: integer number too large: FE00000000000000
    public static final long TESTVAL = 0xFE00000000000000;
    ^
    1 error

     
  • Dan Lacher
    Dan Lacher
    2004-02-04

    Logged In: YES
    user_id=968204

    Sorry for the confusion. Here is the working code.

    % cat Test.java
    public class Test {
    public static final long TEST = -0xFE00000000000000L;

    public static void main(String[] args ) {
        System.out.println("TEST = " + Long.toHexString(TEST));
    

    }
    }
    % javac Test.java
    % java Test
    TEST = 200000000000000

    And the error is:

    Test.java:0: Got an exception - java.lang.NumberFormatException: For
    input string: "FE00000000000000"

     
  • Rick Giles
    Rick Giles
    2004-02-04

    Logged In: YES
    user_id=539926

    Thanks for clarifying the bug. This is a bug in 3.3 that is
    caused by the negative hex literal. It has been corrected in
    the 3.4 CVS.