diff --git a/tccelf.c b/tccelf.c index 507e83c5..69176749 100644 --- a/tccelf.c +++ b/tccelf.c @@ -2423,11 +2423,10 @@ static int tcc_write_elf_file(TCCState *s1, const char *filename, int phnum, mode = 0777; unlink(filename); fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, mode); - if (fd < 0) { - tcc_error_noabort("could not write '%s'", filename); + if (fd < 0 || (f = fdopen(fd, "wb")) == NULL) { + tcc_error_noabort("could not write or fdopen '%s'", filename); return -1; } - f = fdopen(fd, "wb"); if (s1->verbose) printf("<- %s\n", filename); diff --git a/tccmacho.c b/tccmacho.c index 57c62c32..8e86b75f 100644 --- a/tccmacho.c +++ b/tccmacho.c @@ -795,11 +795,10 @@ ST_FUNC int macho_output_file(TCCState *s1, const char *filename) mode = 0777; unlink(filename); fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, mode); - if (fd < 0) { + if (fd < 0 || (fp = fdopen(fd, "wb")) == NULL) { tcc_error_noabort("could not write '%s: %s'", filename, strerror(errno)); return -1; } - fp = fdopen(fd, "wb"); if (s1->verbose) printf("<- %s\n", filename);