Skip to content

Commit

Permalink
Write to an in-memory buffer before writing to an actual output file
Browse files Browse the repository at this point in the history
We don't want to leave a partial assembly output if the compiler
fails during compilation.

Technically speaking, there's still a risk of leaving a partially-
written output file if the compiler dies during file copy. To fix
that, we have to write to a temporary file in the same filesystem as
the final output file and rename the temporary file to atomically
replace the output file. We don't do that in this patch for the sake
of succinctness, though.
  • Loading branch information
rui314 committed Sep 30, 2020
1 parent bbe088f commit f688d63
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion main.c
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,19 @@ static void cc1(void) {

Obj *prog = parse(tok);

// Open a temporary output buffer.
char *buf;
size_t buflen;
FILE *output_buf = open_memstream(&buf, &buflen);

// Traverse the AST to emit assembly.
codegen(prog, output_buf);
fclose(output_buf);

// Write the asembly text to a file.
FILE *out = open_file(output_file);
codegen(prog, out);
fwrite(buf, buflen, 1, out);
fclose(out);
}

static void assemble(char *input, char *output) {
Expand Down

0 comments on commit f688d63

Please sign in to comment.