Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IOUtils.close throws NPE #2515

Open
TobiasKiecker opened this issue Sep 27, 2024 · 2 comments
Open

IOUtils.close throws NPE #2515

TobiasKiecker opened this issue Sep 27, 2024 · 2 comments
Assignees

Comments

@TobiasKiecker
Copy link
Contributor

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);
            }
        }
    }
}
@slovdahl
Copy link
Contributor

Good catch. Just out of curiosity, have you seen this cause problems in reality?

@TobiasKiecker
Copy link
Contributor Author

No not really, we found it as a part of a research project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants