This error occurs when a temporary file is scheduled to be deleted on exit but is not explicitly deleted. This error mostly presents a problem in a server or servlet context.

Vulnerability and risk

Temporary files should be deleted as soon as possible. If a file contains sensitive information, the longer it exists, the better chance an attacker has to gain access to its contents. Also, because directories typically have limits on the number of files allowed, it is possible to overflow the number of temporary files, leading to a denial-of-service (DoS) problem.

Klocwork security vulnerability (SV) checkers identify calls that create potentially dangerous data; these calls are considered unsafe sources. An unsafe source can be any data provided by the user, since the user could be an attacker or has the potential for introducing human error.

Mitigation and prevention

Temporary files should be deleted immediately after use.

Example 1

     protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
         String name = req.getParameter("userName");
         File file = File.createTempFile("file", ".dat");
         char[] data = readData(file);

SV.DOS.TMPFILEDEL is reported for line 19: File 'file' scheduled for 'delete on exit', meaning that it will remain for the lifetime of the jvm (which can be very long in the case of a server or servlet). Server-side applications should remove files explicitly.


This checker can be extended through the Klocwork knowledge base. See Tuning Java analysis for more information.