Skip to content

IOUtils.close throws NPE #2515

Open
Open
@TobiasKiecker

Description

@TobiasKiecker

Describe the bug
The IOUtils.close function:

/**
* <p>
* Tries to close the given objects and log the {@link IOException} at INFO level
* to make the code more readable when we assume that the {@link IOException} won't be managed.
* </p>
* <p/>
* <p>
* Also ignore {@code null} parameters.
* </p>
*
* @param closeableArray the objects to close
*/
public static void close(final Closeable... closeableArray) {
for (Closeable closeable : closeableArray) {
try {
if (closeable != null) {
closeable.close();
}
} catch (IOException ioe) {
logger.info("Can't close the object", ioe);
}
}
}

throws a null pointer exception if it is called with one single parameter which is null.

Atmosphere Info

  • version: all since b31804c
  • atmosphere.js version NA
  • extensions used None

Expected behavior
Should not throw null.

Screenshots

Systems (please complete the following information):

  • OS: NA
  • Browser name and version: NA
  • Java version and distribution: at least Java 21 probably more
  • Serveur name and version: NA

Additional context
This is an edge case of Java behavior of varargs.

An improved implementation could be:

if (closeableArray != null) {
    for (Closeable closeable : closeableArray) {
        if (closeable != null) {
            try {
                closeable.close();
            } catch (IOException ioe) {
                logger.info("Can't close the object", ioe);
            }
        }
    }
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions