Don't try to operate on freed data structure.
authorIain Patterson <me@iain.cx>
Mon, 15 Dec 2014 13:58:39 +0000 (13:58 +0000)
committerIain Patterson <me@iain.cx>
Mon, 15 Dec 2014 14:20:07 +0000 (14:20 +0000)
We were calling HeapFree() on the logger structure then trying to
call CloseHandle() on one of its elements.

io.cpp

diff --git a/io.cpp b/io.cpp
index 90cc0d1..508d7db 100644 (file)
--- a/io.cpp
+++ b/io.cpp
@@ -518,9 +518,9 @@ unsigned long WINAPI log_and_rotate(void *arg) {
           /* Write up to the newline. */\r
           ret = try_write(logger, address, i, &out, &complained);\r
           if (ret < 0) {\r
-            HeapFree(GetProcessHeap(), 0, logger);\r
             CloseHandle(logger->read_handle);\r
             CloseHandle(logger->write_handle);\r
+            HeapFree(GetProcessHeap(), 0, logger);\r
             return 3;\r
           }\r
           size += (__int64) out;\r
@@ -574,9 +574,9 @@ unsigned long WINAPI log_and_rotate(void *arg) {
             error = GetLastError();\r
             log_event(EVENTLOG_ERROR_TYPE, NSSM_EVENT_CREATEFILE_FAILED, logger->path, error_string(error), 0);\r
             /* Oh dear.  Now we can't log anything further. */\r
-            HeapFree(GetProcessHeap(), 0, logger);\r
             CloseHandle(logger->read_handle);\r
             CloseHandle(logger->write_handle);\r
+            HeapFree(GetProcessHeap(), 0, logger);\r
             return 4;\r
           }\r
 \r
@@ -600,15 +600,15 @@ unsigned long WINAPI log_and_rotate(void *arg) {
     ret = try_write(logger, address, in, &out, &complained);\r
     size += (__int64) out;\r
     if (ret < 0) {\r
-      HeapFree(GetProcessHeap(), 0, logger);\r
       CloseHandle(logger->read_handle);\r
       CloseHandle(logger->write_handle);\r
+      HeapFree(GetProcessHeap(), 0, logger);\r
       return 3;\r
     }\r
   }\r
 \r
-  HeapFree(GetProcessHeap(), 0, logger);\r
   CloseHandle(logger->read_handle);\r
   CloseHandle(logger->write_handle);\r
+  HeapFree(GetProcessHeap(), 0, logger);\r
   return 0;\r
 }\r