]> mj.ucw.cz Git - libucw.git/commitdiff
Logging: Fixed a bug in log_close_all().
authorMartin Mares <mj@ucw.cz>
Thu, 19 Feb 2009 17:03:14 +0000 (18:03 +0100)
committerMartin Mares <mj@ucw.cz>
Thu, 19 Feb 2009 17:03:14 +0000 (18:03 +0100)
Under some circumstances (involving loops in substream structure),
log_close_all() crashed, because a stream could have become freed
when removing its substreams.

I have changed log_close_all() to unlink all substreams first and
then proceed with deallocating memory.

The same problem can never occur in simple log_close_stream(),
because a stream that is a part of a loop never gets a use count of 0.

ucw/log-stream.c

index 1ca76a5567215da0b4c67179dc1bb8063f22ae38..3e94d74832ab678e1de24d4708c126e956facb1b 100644 (file)
@@ -58,15 +58,17 @@ log_close_all(void)
   if (!log_initialized)
     return;
 
-  // Close all open streams
+  // Remove substreams of all streams
   for (int i=0; i < log_streams_after; i++)
     if (log_streams.ptr[i]->regnum >= 0)
-      log_close_stream(log_streams.ptr[i]);
+      log_rm_substream(log_streams.ptr[i], NULL);
 
-  // Free all cached structures
+  // Close all streams that remain and free all cached structures
   for (int i=0; i < log_streams_after; i++)
     {
       struct log_stream *ls = log_streams.ptr[i];
+      if (ls->regnum >= 0)
+       log_close_stream(ls);
       ASSERT(ls->regnum < 0 || !ls->use_count);
       xfree(ls);
     }