Tcl Source Code

View Ticket
Login
Ticket UUID: 27b682284974d0cdf4686f31adcb9ee39a0befa6
Title: file delete can return an error if file was already deleted
Type: Bug Version: 8.5
Submitter: raimue Created on: 2018-04-06 14:05:10
Subsystem: 37. File System Assigned To: nobody
Priority: 5 Medium Severity: Severe
Status: Closed Last Modified: 2018-04-09 10:12:02
Resolution: Fixed Closed By: sebres
    Closed on: 2018-04-09 10:12:02
Description:
The 'file delete' command can fail with "no such file or directory", but it is
supposed to return successfully in this case.

This can happen when another process removes the file exactly between the
system calls to stat(2) and unlink(2) in TclFileDeleteCmd(), which is a classic
TOCTTOU race condition.

I will attach a patch that adds an additional check for the result of
Tcl_FSDeleteFile() and if it was ENOENT, the command is considered successful.
User Comments: sebres added on 2018-04-09 10:12:02:

I was confused by the comment header of both. Corresponding it Tcl_FSDeleteFile as well as Tcl_FSRemoveDirectory returns "Standard Tcl error code" (what is indeed wrong).

So fixed now in [6627aeef8ecda182] for TclFileDeleteCmd.

But I guess we have to fix all the comments "Returns" in tclIOUtil.c.


raimue added on 2018-04-06 18:44:52:
Thank you for the quick response. Applying the same change to
directories makes sense, it just did not occur to me.

Looking at the patch, I see you removed
  result = TCL_ERROR;
before the break statement, but I think this was actually needed as
Tcl_FSRemoveDirectory() and Tcl_FSDeleteFile() return -1 on error.

sebres added on 2018-04-06 17:37:45:

I've fixed it in [5acb57c7aec45e05] (although other 8.5th branch, but if not whole accepted this commit can be easy cherry-picked into core-8-5-branch).

Deviations from suggested patch: I've rewritten it also to catch the same race-condition in case of deleted directory.

Thanks you Rainer.


Attachments: