From 247bf4eec9bf2514a23f208f8e2da725ecaa3fba Mon Sep 17 00:00:00 2001 From: Ivan Maidanski Date: Wed, 24 Oct 2012 08:29:42 +0400 Subject: [PATCH] Fix potential double fclose in test_extras (cordtest) * cord/tests/cordtest.c (test_extras): Explicitly clear cord handles obtained from CORD_from_file(), invoke GC_gcollect and GC_invoke_finalizers to force f1a, f1b, f2 handles to be closed before removing the files; remove the second attempts to remove the files on rename failure (print a warning instead) as it might cause double fclose (or double free); add comments. --- cord/tests/cordtest.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/cord/tests/cordtest.c b/cord/tests/cordtest.c index fd0bc325..cf5c6966 100644 --- a/cord/tests/cordtest.c +++ b/cord/tests/cordtest.c @@ -187,15 +187,20 @@ void test_extras(void) if (CORD_str(x,0,"9abcdefghijx") != CORD_NOT_FOUND) ABORT("CORD_str failed 3"); if (CORD_str(x,0,"9>") != CORD_NOT_FOUND) ABORT("CORD_str failed 4"); + /* Note: f1a, f1b, f2 handles are closed lazily by CORD library. */ + /* TODO: Propose and use CORD_fclose. */ + *(CORD volatile *)&w = CORD_EMPTY; + *(CORD volatile *)&z = CORD_EMPTY; + GC_gcollect(); + GC_invoke_finalizers(); + /* Of course, this does not guarantee the files are closed. */ if (remove(FNAME1) != 0) { /* On some systems, e.g. OS2, this may fail if f1 is still open. */ - if ((fclose(f1a) == EOF) & (fclose(f1b) == EOF)) - ABORT("fclose(f1) failed"); - if (remove(FNAME1) != 0) ABORT("remove 1 failed"); + /* But we cannot call fclose as it might lead to double close. */ + fprintf(stderr, "WARNING: remove(FNAME1) failed\n"); } if (remove(FNAME2) != 0) { - if (fclose(f2) == EOF) ABORT("fclose(f2) failed"); - if (remove(FNAME2) != 0) ABORT("remove 2 failed"); + fprintf(stderr, "WARNING: remove(FNAME2) failed\n"); } } -- 2.40.0