Synopsis: | Do not access members of type FILE directly. |
Language: | C |
Severity Level: | 6 |
Category: | Library |
Description: |
Justification The FILE struct can be different on different operating systems. Source code should not depend on the member fields. Programs that access for instance the "_fileno" field directly are in risk of accessing another file descriptor and causing unpredictable harm to the system (write to another file, close another file or socket, etc.). Use the available functions for FILE instead. Examples of such functions are fopen(), fclose(), fread(), fwrite(), fflush(), fgetline(), fseek(), fscanf(), fgets(), fgetc(), etc. Example FILE * f = fopen("/tmp/test", "w"); unsigned char c_lookahead = *f->_ptr; /* WRONG: not portable */ printf("file descriptor number: %d\n", f->_fileno); /* WRONG: not portable */ long pos = ftell(f); /* RIGHT */ |