From d0fbb7f0ab355fd4e73d1aab215a2aa387b615c3 Mon Sep 17 00:00:00 2001 From: "H. Peter Anvin" Date: Sun, 12 Jul 2009 12:11:52 -0700 Subject: Don't fclose() the output in the backend We fopen() the output file in common code but fclose() it in the backend. This is bad for a variety of reasons: 1. it is generally an awkward interface to change ownership. 2. we should use ferror() to test for write errors, and that is better done in common code. 3. it requires more code. 4. we still need to fclose() in common code during error handing. Thus, move the fclose() of the output out of the backends, and add fflush() so we can test ferror() on output. Signed-off-by: H. Peter Anvin --- nasm.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) (limited to 'nasm.c') diff --git a/nasm.c b/nasm.c index d1cd4659..53e1c72b 100644 --- a/nasm.c +++ b/nasm.c @@ -431,6 +431,7 @@ int main(int argc, char **argv) fclose(ofile); if (ofile && terminate_after_phase) remove(outname); + ofile = NULL; } break; @@ -466,16 +467,17 @@ int main(int argc, char **argv) if (!terminate_after_phase) { ofmt->cleanup(using_debug_info); cleanup_labels(); - } else { - /* - * Despite earlier comments, we need this fclose. - * The object output drivers only fclose on cleanup, - * and we just skipped that. - */ - fclose (ofile); + fflush(ofile); + if (ferror(ofile)) { + report_error(ERR_NONFATAL|ERR_NOFILE, + "write error on output file `%s'", outname); + } + } + fclose(ofile); + if (ofile && terminate_after_phase) remove(outname); - } + ofile = NULL; } break; } @@ -1961,6 +1963,7 @@ static void report_error_common(int severity, const char *fmt, if (ofile) { fclose(ofile); remove(outname); + ofile = NULL; } if (want_usage) usage(); -- cgit v1.2.1