tags:

views:

34

answers:

2

I write code to delete a file, just like:

File logFile = new File(fileName);

deleteSuccess = logFile.delete();

But the Veracode: give me some warning about the resource is not released. I want to know how to release the resource after delete a file.

The Veracode error is like follows

Veracode:

Description
The application fails to release (or incorrectly releases) a system resource before it is made available for re-use. This condition often occurs with resources such as database connections or file handles. Most unreleased resource issues result in general software reliability problems, but if an attacker can intentionally trigger a resource leak, it may be possible to launch a denial of service attack by depleting the resource pool.

Recommendations When a resource is created or allocated, the developer is responsible for properly releasing the resource as well as accounting for all potential paths of expiration or invalidation. Ensure that all code paths properly release resources.

+1  A: 

Are you sure that is your whole code? Because there is nothing wrong with it. The File object is just a wrapper around the file name, there are no resources allocated for it. The delete method also just calls the appropriate OS function and does not require any cleanup afterwards.

What you need to close are streams derived from the File (but you do not seem to have any).

Thilo
Yes, close streams are necessary, but for delete, I do not know anything I can release.
I think the tool is in error. Do you get other warnings that you do not understand or agree with?
Thilo
A: 

You could attempt to explicitly nullify the File object once the delete operation has succeeded (check the boolean value returned, if necessary, but then I don't know about your intentions if the delete operation fails). Atleast that way, you could be assured of the fact that the reference to the File object is lost, and hence all related objects are also eligible for garbage collection.

If VercaCode continues to flag this as a warning, I presume you could be justified in ignoring this issue.

While the suggested change does not add any value, especially if the File object is eligible for garbage collection once it is out of scope, you could adopt this practice if you are keen on getting rid of this warning.

Vineet Reynolds
Thanks your suggestion, I will have a try