JDK-8077600 : Long.parseUnsignedLong doesn't check for numeric overflow properly
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 8u40
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: linux
  • CPU: x86_64
  • Submitted: 2015-04-11
  • Updated: 2015-04-13
  • Resolved: 2015-04-13
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
openjdk version "1.8.0_40-internal"
OpenJDK Runtime Environment (build 1.8.0_40-internal-b27)
OpenJDK 64-Bit Server VM (build 25.40-b25, mixed mode)

A DESCRIPTION OF THE PROBLEM :
According to the documentation, java.lang.Long.parseUnsignedLong static method is supposed to throw NumberFormatException on strings representing values greater than 2**64-1. However, sometimes numeric overflow is not detected. Example:

Long.parseUnsignedLong("46116860184273879040")

On my machine, this expression is evaluated without throwing any exceptions, and the result is -9223372036854775808L (which is equal to 9223372036854775808 when interpreted as unsigned).
Expected behavior: evaluation of this expression should result in NumberFormatException being thrown.


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
public class Test {
	public static void main(String[] args) {
		System.out.println(Long.toUnsignedString(Long.parseUnsignedLong("46116860184273879040")));
	}
}
---------- END SOURCE ----------


Comments
Closing this as fix confirmed with JDK 9 and duplicate of JDK-8030814.
13-04-2015

1) Run the attached test case (Test.java) 2) Tested this on Linux and Windows 7 (64-bit) with JDK 8u31, 8u40, 8u60 ea b07 and 9 ea b57. ****************************************************************************************** 8u31: FAIL 8u40: FAIL 8u60 ea b07: FAIL 9 ea b57: OK ******************************************************************************************* Output (JDK 8u40): >java Test 9223372036854775808 -------------------------------------------------- Output (JDK 9 ea b57): >java Test Exception in thread "main" java.lang.NumberFormatException: String value 46116860184273879040 exceeds range of unsigned long. at java.lang.Long.parseUnsignedLong(Long.java:843) at java.lang.Long.parseUnsignedLong(Long.java:988) at Test.main(Test.java:3) ************************************************************************************************** Result: The issue is reproducible with JDK 8-all versions. However it seems resolved in JDK 9 ea b57 with the desired NumberFormatException.
13-04-2015