bitwiseworks / libc

LIBC Next (kLIBC fork)
9 stars 4 forks source link

Improve tmpfile functionality #125

Open dmik opened 1 year ago

dmik commented 1 year ago

LIBC provides tmpfile posix call implementation which is not complete. It has the following flaws which make it not compliant to POSIX standards:

  1. The temporary file created by tmpfile is deleted only by fclose or on LIBC process termination (by _rmtmp from _exit_streams). If, for instance, the file handle is duplicated with dup (fileno (FILE *)), then a subsequent fclose will destroy the FILE instance but will be unable to delete the file (because it's open through another file handle). As a result, the file will be left on disk after program termination creating unnecessary clobber. A real life example is GNU make, see https://github.com/bitwiseworks/kbuild-os2/issues/5.

  2. When tmpfile creates a temporary file with .tmp extension in the current directory, its full path is not stored anywhere. Upon deletion, the file name is reconstructed from the index value and the code assumes the current directory did not change. However, if it did change, then the file will not be deleted (more over, a wrong file may be deleted if a file with the same name just happens to be in the directory) even if no other handles for that file exist.