JDK-8042470 : (fs) Path.register doesn't throw IllegalArgumentException if multiple OVERFLOW events are specified
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.nio
  • Affected Version: 7u60
  • Priority: P5
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2014-05-06
  • Updated: 2015-01-21
  • Resolved: 2014-05-07
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.
8u40Fixed 9 b13Fixed
Related Reports
Relates :  
The code doesn't handle some types of invalid input:

For example: path.register(watcher, OVERFLOW, OVERFLOW) does not throw the exception, and instead makes sun.nio.fs.LinuxWatchService$Poller to fail.
Thanks. Yes, I had the same in mind. Additionally, it reduces code duplication.

In that case, this shoudl do it. diff -r 08e2dabe48e9 src/share/classes/sun/nio/fs/AbstractPoller.java --- a/src/share/classes/sun/nio/fs/AbstractPoller.java Sun Apr 27 18:28:56 2014 -0700 +++ b/src/share/classes/sun/nio/fs/AbstractPoller.java Tue May 06 16:07:03 2014 +0100 @@ -100,8 +100,6 @@ // validate arguments before request to poller if (dir == null) throw new NullPointerException(); - if (events.length == 0) - throw new IllegalArgumentException("No events to register"); Set<WatchEvent.Kind<?>> eventSet = new HashSet<>(events.length); for (WatchEvent.Kind<?> event: events) { // standard events @@ -115,8 +113,6 @@ // OVERFLOW is ignored if (event == StandardWatchEventKinds.OVERFLOW) { - if (events.length == 1) - throw new IllegalArgumentException("No events to register"); continue; } @@ -125,6 +121,8 @@ throw new NullPointerException("An element in event set is 'null'"); throw new UnsupportedOperationException(event.name()); } + if (eventSet.size() == 0) + throw new IllegalArgumentException("No events to register"); return (WatchKey)invoke(RequestType.REGISTER, dir, eventSet, modifiers); }

Sorry, I wasn't clear enough. If we call path.register(watcher, OVERFLOW), it will throw IllegalArgumentException("No events to register") from AbstractPoller#register. This is correct behavior. But if we specify OVERFLOW *twice*, it will not throw the exception, but will pass an empty list of events to the poller implementation instead. This, in turn, will make it fail. The bug is the wrong assumption that there might be no more than one OVERFLOW constant in the list. No doubt, it is an edge case.

From the spec: "Objects are automatically registered for the OVERFLOW event. This event is not required to be present in the array of events.". IAE should not be thrown for this case.