JDK-8193137 : Nashorn crashes when given an empty script file.
  • Type: Bug
  • Component: core-libs
  • Sub-Component: jdk.nashorn
  • Affected Version: 8u112,9.0.1,10
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: windows
  • CPU: x86_64
  • Submitted: 2017-12-05
  • Updated: 2020-07-16
  • Resolved: 2017-12-08
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 10 JDK 8 Other
10 b36Fixed 8u261Fixed openjdk8u272Fixed
Description
FULL PRODUCT VERSION :
java version "9.0.1"
Java(TM) SE Runtime Environment (build 9.0.1+11)
Java HotSpot(TM) 64-Bit Server VM (build 9.0.1+11, mixed mode)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows [Version 10.0.16299.98]

A DESCRIPTION OF THE PROBLEM :
Nashorn crashes with NullPointerException when passed an empty script file:

jjs empty.js

Fails only when file is totally empty, size=0. If file contains a single space character, or a single '0' character, does not crash.

REGRESSION.  Last worked in version 7u79

ADDITIONAL REGRESSION INFORMATION: 
I know the empty file did work OK in a *fairly* recent prior version, but not certain which one. Just tried using java 8 and it failed the same way. Just guessing about java 7.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Run:

jjs empty.js

where empty.js is a zero-length file.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Should to nothing, and not crash An empty string is a valid JavaScript program.
ACTUAL -
Crashes.

ERROR MESSAGES/STACK TRACES THAT OCCUR :
>jjs empty.js
Exception in thread "main" java.lang.NullPointerException
        at jdk.scripting.nashorn/jdk.nashorn.tools.Shell.preprocessArgs(Shell.java:298)
        at jdk.scripting.nashorn/jdk.nashorn.tools.Shell.makeContext(Shell.java:213)
        at jdk.scripting.nashorn/jdk.nashorn.tools.Shell.run(Shell.java:166)
        at jdk.scripting.nashorn.shell/jdk.nashorn.tools.jjs.Main.main(Main.java:104)
        at jdk.scripting.nashorn.shell/jdk.nashorn.tools.jjs.Main.main(Main.java:80)

REPRODUCIBILITY :
This bug can be reproduced always.

CUSTOMER SUBMITTED WORKAROUND :
I use this command to get the Nashorn version from another program:

    jjs -v empty.js

This is the only way I know to get the version. If the "empty.js" argument is omitted, interactive mode is entered.

This used to work with an empty file, but now crashes. Workaround is to put a space in empty.js.


Comments
Fix Request (8u) Improves Nashorn reliability and keeps codebases in sync (I see 8u261). Patch applies with reshuffles, but test needs work. 8u RFR (reviewed): https://mail.openjdk.java.net/pipermail/jdk8u-dev/2020-March/011484.html
30-03-2020

URL: http://hg.openjdk.java.net/jdk/jdk/rev/ef5199ed52f0 User: hannesw Date: 2017-12-08 11:07:16 +0000
08-12-2017

Tested with an empty js file. Following are the results: JDK 8u111 - Pass JDK 8u112 - Fail JDK 9.0.1+11 - Fail JDK 10-ea+30 - Fail
06-12-2017