From a3a1ec01064dfa5e1b9bb483b1b50b64fe534511 Mon Sep 17 00:00:00 2001 From: guyio Date: Mon, 13 Aug 2018 15:14:48 +0300 Subject: [PATCH] removed code duplication of write_to_testcase --- afl-fuzz.c | 39 ++++++--------------------------------- 1 file changed, 6 insertions(+), 33 deletions(-) diff --git a/afl-fuzz.c b/afl-fuzz.c index 444d42e9..5f71b4dd 100755 --- a/afl-fuzz.c +++ b/afl-fuzz.c @@ -2728,39 +2728,12 @@ static void write_to_testcase(void* mem, u32 len) { /* The same, but with an adjustable gap. Used for trimming. */ static void write_with_gap(char* mem, u32 len, u32 skip_at, u32 skip_len) { - - s32 fd = out_fd; - u32 tail_len = len - skip_at - skip_len; - - if (out_file) { - - unlink(out_file); /* Ignore errors. */ - - fd = open(out_file, O_WRONLY | O_BINARY | O_CREAT | O_EXCL, 0600); - - if (fd < 0) { - destroy_target_process(0); - - unlink(out_file); /* Ignore errors. */ - - fd = open(out_file, O_WRONLY | O_BINARY | O_CREAT | O_EXCL, 0600); - - if (fd < 0) PFATAL("Unable to create '%s'", out_file); - } - - } else lseek(fd, 0, SEEK_SET); - - if (skip_at) ck_write(fd, mem, skip_at, out_file); - - if (tail_len) ck_write(fd, mem + skip_at + skip_len, tail_len, out_file); - - if (!out_file) { - - if (_chsize(fd, len - skip_len)) PFATAL("ftruncate() failed"); - lseek(fd, 0, SEEK_SET); - - } else close(fd); - + + char* trimmed_mem = malloc(len - skip_len); + memcpy(trimmed_mem, mem, skip_at); //copy start + memcpy(trimmed_mem + skip_at, mem + skip_at + skip_len, len - (skip_at + skip_len)); + write_to_testcase(trimmed_mem, len - skip_len); + free(trimmed_mem); }