You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 6 Next »

Calling remove() on a file that is currently open has implementation defined behavior. In the case of Cygwin it just fails, and in the case of Andrew Linux is acts like the file still exists, but you can create a new file with the same name without worrying about overwriting the file pointer from the first time the file was opened.

Code

#include <stdio.h>

int main(int argc, char* argv) {
  FILE* first;
  FILE* second;

  char buf1[10];
  char buf2[10];

  int i;

  first = fopen("a.in", "r");

  if (remove("a.in") == -1) {
    printf("Unlink Failed\n");
  } else {
    printf("Unlink Success\n");
  }

  second = fopen("a.in", "w");
  if (second) {
    printf("Good OPEN\n");
  } else {
    printf("Bad OPEN\n");
  }

  for (i = 0; i < 10; i++) {
    buf2[i] = 'Q';
  }
  fwrite(buf2, sizeof(char), 9, second);
  fflush(second);

  fread(buf1, sizeof(char), 9, first);
  buf1[9] = '\0';
  buf2[9] = '\0';

  printf("First: %10s\n", buf1);

  printf("Second: %10s\n", buf2);
}

File a.in
AAAAAAAAA

Output on Andrew Linux
Unlink Success
Good OPEN
First:  AAAAAAAAA
Second:  QQQQQQQQQ

 Output on Cygwin
Unlink Failed
Good OPEN
  15568 [main] rule11 5048 _cygtls::handle_exceptions: Error while dumping state (probably corrupted stack)
Segmentation fault (core dumped)

 From 'man 2 unlink':
BUGS
       Infelicities in the protocol underlying NFS can cause the unexpected disappearance of files which are still being used.

Risk Assessment

Rule

Severity

Likelihood

Remediation Cost

Priority

Level

INT10-A

2 (medium)

1 (low)

1 (low)

P2

L3

  • No labels