Skip to content

Commit 8818f63

Browse files
committed
C++: Add some practical details to the examples.
1 parent 80af5b7 commit 8818f63

File tree

1 file changed

+23
-3
lines changed

1 file changed

+23
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,38 @@
11
void write_default_config_bad() {
22
// BAD - this is world-writable so any user can overwrite the config
33
int out = creat(OUTFILE, 0666);
4-
dprintf(out, DEFAULT_CONFIG);
4+
if (out < 0) {
5+
// handle error
6+
}
7+
8+
dprintf(out, "%s", DEFAULT_CONFIG);
9+
close(out);
510
}
611

712
void write_default_config_good() {
813
// GOOD - this allows only the current user to modify the file
914
int out = creat(OUTFILE, S_IWUSR | S_IRUSR);
10-
dprintf(out, DEFAULT_CONFIG);
15+
if (out < 0) {
16+
// handle error
17+
}
18+
19+
dprintf(out, "%s", DEFAULT_CONFIG);
20+
close(out);
1121
}
1222

1323
void write_default_config_good_2() {
1424
// GOOD - this allows only the current user to modify the file
1525
int out = open(OUTFILE, O_WRONLY | O_CREAT, S_IWUSR | S_IRUSR);
26+
if (out < 0) {
27+
// handle error
28+
}
29+
1630
FILE *fd = fdopen(out, "w");
17-
fprintf(fd, DEFAULT_CONFIG);
31+
if (fd == NULL) {
32+
close(out);
33+
// handle error
34+
}
35+
36+
fprintf(fd, "%s", DEFAULT_CONFIG);
37+
fclose(fd);
1838
}

0 commit comments

Comments
 (0)