-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileCode.c
More file actions
37 lines (30 loc) · 687 Bytes
/
FileCode.c
File metadata and controls
37 lines (30 loc) · 687 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <cerrno>
int access(const char* file, const char* status) {
return -1;
}
void fopen_with_toctou(const char* file)
{
if (access(file, "F_OK") == -1 && errno == ENOENT)
{
FILE* f = fopen(file, "w"); // Noncompliant
if (NULL == f) {
/* Handle error */
}
if (fclose(f) == EOF) {
/* Handle error */
}
}
}
void open_without_toctou(const char* file) {
FILE* f = fopen(file, "wx"); // Compliant
if (NULL == f) {
/* Handle error */
}
/* Write to file */
if (fclose(f) == EOF) {
/* Handle error */
}
}