espressif / newlib-esp32

Version of newlib used in ESP32 ROM and ESP-IDF
GNU General Public License v2.0
31 stars 18 forks source link

mkstemp et al do not create unique file names #3

Closed dexterbg closed 2 years ago

dexterbg commented 5 years ago

mkstemp() always creates the same suffix regardless of the file name and/or existence of the temp file.

Test code

void test_mkstemp(int argc, const char* const* argv)
{
  int fd1, e1, fd2, e2;
  char tn1[100], tn2[100];
  snprintf(tn1, sizeof(tn1), "%s.XXXXXX", argv[0]);
  snprintf(tn2, sizeof(tn2), "%s.XXXXXX", argv[0]);
  errno = 0;
  fd1 = mkstemp(tn1); e1 = errno;
  fd2 = mkstemp(tn2); e2 = errno;
  printf("tempfile 1: '%s' => fd=%d error='%s'\n", tn1, fd1, (fd1<0) ? strerror(e1) : "-");
  printf("tempfile 2: '%s' => fd=%d error='%s'\n", tn2, fd2, (fd2<0) ? strerror(e2) : "-");
  if (fd1 >= 0) { close(fd1); unlink(tn1); }
  if (fd2 >= 0) { close(fd2); unlink(tn2); }
}

Result

OVMS# test mkstemp /sd/a    
tempfile 1: '/sd/a.967295' => fd=3 error='-'
tempfile 2: '/sd/a.967295' => fd=-1 error='No such file or directory'
OVMS# test mkstemp /sd/bb
tempfile 1: '/sd/bb.967295' => fd=3 error='-'
tempfile 2: '/sd/bb.967295' => fd=-1 error='No such file or directory'
OVMS# test mkstemp /sd/ccc
tempfile 1: '/sd/ccc.967295' => fd=3 error='-'
tempfile 2: '/sd/ccc.967295' => fd=-1 error='No such file or directory'
OVMS# test mkstemp /sd/dddd
tempfile 1: '/sd/dddd.967295' => fd=3 error='-'
tempfile 2: '/sd/dddd.967295' => fd=-1 error='No such file or directory'
dexterbg commented 5 years ago

If the file already exists, the first mkstemp() call fails as well:

OVMS# vfs append "x" /sd/dddd.967295
OVMS# test mkstemp /sd/dddd
tempfile 1: '/sd/dddd.967295' => fd=-1 error='No such file or directory'
tempfile 2: '/sd/dddd.967295' => fd=-1 error='No such file or directory'
igrr commented 2 years ago

I'm sorry we didn't notice this issue in time. Running a similar test case with CONFIG_FATFS_LFN_HEAP=y set, I am not able to reproduce an error with IDF 4.4 or master. Two different files get created:

tempfile 1: '/spiflash/file.967295' => fd=3 error='-'
tempfile 2: '/spiflash/file.a67295' => fd=4 error='-'

This may have been related to stat failing for the root directory of FAT filesystem in the past (https://github.com/espressif/esp-idf/issues/984) but that issue got fixed before this one was opened, so perhaps there was some other issue.