Skip to content

Commit 3c70583

Browse files
committed
C++: Add close calls to examples for cpp/toctou-race-condition.
1 parent 0288499 commit 3c70583

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
char *file_name;
22
FILE *f_ptr;
3-
3+
44
/* Initialize file_name */
5-
5+
66
f_ptr = fopen(file_name, "w");
77
if (f_ptr == NULL) {
88
/* Handle error */
99
}
10-
10+
1111
/* ... */
12-
12+
1313
if (chmod(file_name, S_IRUSR) == -1) {
1414
/* Handle error */
15-
}
15+
}
16+
17+
fclose(f_ptr);
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
char *file_name;
22
int fd;
3-
3+
44
/* Initialize file_name */
5-
5+
66
fd = open(
77
file_name,
88
O_WRONLY | O_CREAT | O_EXCL,
@@ -11,9 +11,11 @@ fd = open(
1111
if (fd == -1) {
1212
/* Handle error */
1313
}
14-
14+
1515
/* ... */
16-
16+
1717
if (fchmod(fd, S_IRUSR) == -1) {
1818
/* Handle error */
19-
}
19+
}
20+
21+
close(fd);

0 commit comments

Comments
 (0)