Skip to content

Commit

Permalink
Remove redundant file API calls (unlink before open, seek before close)
Browse files Browse the repository at this point in the history
  • Loading branch information
eranzim committed Jan 23, 2022
1 parent 3873947 commit 244807f
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 24 deletions.
4 changes: 1 addition & 3 deletions afl-analyze.c
Original file line number Diff line number Diff line change
Expand Up @@ -309,14 +309,12 @@ static void write_to_file(u8* path, u8* mem, u32 len) {

s32 ret;

_unlink(path); /* Ignore errors */
ret = _open(path, O_RDWR | O_CREAT | O_EXCL | O_BINARY, DEFAULT_PERMISSION);
ret = _open(path, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, DEFAULT_PERMISSION);

if (ret < 0) PFATAL("Unable to create '%s'", path);

ck_write(ret, mem, len, path);

_lseek(ret, 0, SEEK_SET);
_close(ret);

}
Expand Down
19 changes: 4 additions & 15 deletions afl-fuzz.c
Original file line number Diff line number Diff line change
Expand Up @@ -2956,16 +2956,12 @@ static void write_to_testcase(void* mem, u32 len) {

if (out_file) {

unlink(out_file); /* Ignore errors. */

fd = open(out_file, O_WRONLY | O_BINARY | O_CREAT | O_EXCL, DEFAULT_PERMISSION);
fd = open(out_file, O_WRONLY | O_BINARY | O_CREAT | O_TRUNC, DEFAULT_PERMISSION);

if (fd < 0) {
destroy_target_process(0);

unlink(out_file); /* Ignore errors. */

fd = open(out_file, O_WRONLY | O_BINARY | O_CREAT | O_EXCL, DEFAULT_PERMISSION);
fd = open(out_file, O_WRONLY | O_BINARY | O_CREAT | O_TRUNC, DEFAULT_PERMISSION);

if (fd < 0) PFATAL("Unable to create '%s'", out_file);

Expand Down Expand Up @@ -5134,13 +5130,8 @@ static u8 trim_case(char** argv, struct queue_entry* q, u8* in_buf) {
if (needs_write) {

s32 fd;
int oflag = O_WRONLY | O_BINARY | O_CREAT;

if (!unlink(q->fname)) {
fd = open(q->fname, oflag | O_EXCL, DEFAULT_PERMISSION);
} else {
fd = open(q->fname, oflag | O_TRUNC, DEFAULT_PERMISSION);
}
fd = open(q->fname, O_WRONLY | O_BINARY | O_CREAT | O_TRUNC, DEFAULT_PERMISSION);

if (fd < 0) PFATAL("Unable to create '%s'", q->fname);

Expand Down Expand Up @@ -7664,9 +7655,7 @@ static void setup_stdio_file(void) {

u8* fn = alloc_printf("%s\\.cur_input", out_dir);

unlink(fn); /* Ignore errors */

out_fd = open(fn, O_RDWR | O_BINARY | O_CREAT | O_EXCL, DEFAULT_PERMISSION);
out_fd = open(fn, O_RDWR | O_BINARY | O_CREAT | O_TRUNC, DEFAULT_PERMISSION);

if (out_fd < 0) PFATAL("Unable to create '%s'", fn);

Expand Down
3 changes: 1 addition & 2 deletions afl-showmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,7 @@ static u32 write_results(void) {

} else {

_unlink(out_file); /* Ignore errors */
fd = _open(out_file, O_WRONLY | O_CREAT | O_EXCL, DEFAULT_PERMISSION);
fd = _open(out_file, O_WRONLY | O_CREAT | O_TRUNC, DEFAULT_PERMISSION);
if (fd < 0) PFATAL("Unable to create '%s'", out_file);

}
Expand Down
5 changes: 1 addition & 4 deletions afl-tmin.c
Original file line number Diff line number Diff line change
Expand Up @@ -343,15 +343,12 @@ static void write_to_file(u8* path, u8* mem, u32 len) {

s32 ret;

_unlink(path); /* Ignore errors */

ret = _open(path, O_RDWR | O_CREAT | O_EXCL | O_BINARY, DEFAULT_PERMISSION);
ret = _open(path, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, DEFAULT_PERMISSION);

if (ret < 0) PFATAL("Unable to create '%s'", path);

ck_write(ret, mem, len, path);

_lseek(ret, 0, SEEK_SET);
_close(ret);

}
Expand Down

0 comments on commit 244807f

Please sign in to comment.