Alfusainey opened a new issue, #6165:
URL: https://github.com/apache/netbeans/issues/6165
### Apache NetBeans version
Apache NetBeans 18
### What happened
There is a potential resource leak bug in the implementation of the
`VisualDevelopmentUtil#copy()` method.
This method copies the contents of one file to another. However, the input
and output streams will not be closed if an exception is thrown during the
`write` operation. One solution is to use try-with-resources so that it is
guaranteed that the streams will be close in the event an exception is thrown.
```java
public static void copy(File src, File dst) throws IOException {
try (InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dst)) {
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
}
}
```
### Context
We came across this issue while investigating open-source projects to see if
the projects contain code that is similar to (or reused from) Stack Overflow
and whether the code on Stack Overflow underwent any bug or security fixes. We
found that the code in the `VisualDevelopmentUtil#copy()` method is somewhat
similar to the one in the first version of [this
answer](https://stackoverflow.com/revisions/9293885/1). The issue was
subsequently fixed in the [second
version](https://stackoverflow.com/revisions/9293885/2), however, the fix is
not reflected in this method.
### How to reproduce
_No response_
### Did this work correctly in an earlier version?
No / Don't know
### Operating System
MacOS
### JDK
11
### Apache NetBeans packaging
Other
### Anything else
_No response_
### Are you willing to submit a pull request?
Yes
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists