Fix potential double fclose in test_extras (cordtest)
authorIvan Maidanski <ivmai@mail.ru>
Wed, 24 Oct 2012 04:29:42 +0000 (08:29 +0400)
committerIvan Maidanski <ivmai@mail.ru>
Wed, 24 Oct 2012 11:11:57 +0000 (15:11 +0400)
* 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

index fd0bc32..cf5c696 100644 (file)
@@ -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");
     }
 }