Integrated: 8302514: Misleading error generated when empty class file encountered

Archie L. Cobbs duke at openjdk.org
Thu Feb 16 14:51:44 UTC 2023


On Wed, 15 Feb 2023 15:59:36 GMT, Archie L. Cobbs <duke at openjdk.org> wrote:

> When javac encounters an empty class file, you get the following strange error referring to `java.lang.AutoCloseable`:
> 
> Test.java:3: error: cannot access Empty
>         new Empty();
>             ^
>   bad class file: ./Empty.class
>     class file contains wrong class: java.lang.AutoCloseable
>     Please remove or make sure it appears in the correct subdirectory of the classpath.
> 1 error
> 
> This is a side effect of javac blindly reading past the end of the data in a data buffer. Further investigation revealed three distinct bugs in this area of the code:
> 
> 1. There are several unchecked corner cases in `ByteBuffer` and `ArrayUtils` that could potentially lead to `ArrayIndexOutOfBoundsException`s and/or infinite loops. Instead we should "fail fast" on any attempt to read or allocate data out of bounds.
> 1. The method `ByteBuffer.appendStream()` assumes that the result from `InputStream.available()` is always accurate, but this is not guaranteed. As a result, class files could (in theory) be read incorrectly by the compiler, especially when they come from non-standard sources.
> 1. There is no systematic protection against truncated class files. Instead, depending on where exactly it is truncated, a truncated class file generates one of several possible different errors:
>    * `index 25926 is not within pool size 13`
>    * `bad constant pool tag: 0 at 28317`
>    * `unexpected constant pool tag: 10 at 11`
>    * `class file is invalid for class Empty`
>    * `class file contains wrong class: java.lang.AutoCloseable`
> 
> This patch fixes issues 1 and 2, and addresses 3 by having `ByteBuffer` throw a new unchecked `UnderflowException` if ever data is read past the end of the buffer. Then `PoolReader`, `ClassReader`, and `ModuleNameReader` are updated to catch this exception and convert it into a bad class file error. As a result, truncated class files generate a consistent `class file truncated at offset X` error regardless of where they are truncated.

This pull request has now been integrated.

Changeset: a58fa6e7
Author:    Archie L. Cobbs <archie.cobbs at gmail.com>
Committer: Julian Waters <jwaters at openjdk.org>
URL:       https://git.openjdk.org/jdk/commit/a58fa6e73e4594cfb0e46bdbebad48072771e5bd
Stats:     351 lines in 10 files changed: 306 ins; 0 del; 45 mod

8302514: Misleading error generated when empty class file encountered

Reviewed-by: vromero, jwaters

-------------

PR: https://git.openjdk.org/jdk/pull/12574


More information about the compiler-dev mailing list