From db413945da7ef668b86811c0df507cb6931090a1 Mon Sep 17 00:00:00 2001
From: Craig Ringer <craig@2ndquadrant.com>
Date: Mon, 16 Jun 2014 21:16:31 +0800
Subject: [PATCH] Fix access to unallocated memory in IAsyncPG

IAsyncPG was deleting its self, then returning an instance variable
by value. This is probably going to be safe pretty much all the time,
but it's still a memory error, and DrMemory (a Windows valgrind-alike)
complains about it:

	Error #4: UNADDRESSABLE ACCESS: reading 0x00b1b2b0-0x00b1b2b4 4 byte(s)
	# 0 pgenlista.dll!IAsyncPG::Release   [c:\users\administrator\desktop\psqlodbc\msdtc_enlist.cpp:352]
	# 1 pgenlista.dll!CloseCommitThread   [c:\users\administrator\desktop\psqlodbc\msdtc_enlist.cpp:971]
	# 2 MSVCR120D.dll!_callthreadstart    [f:\dd\vctools\crt\crtw32\startup\thread.c:255]
	# 3 MSVCR120D.dll!_threadstart        [f:\dd\vctools\crt\crtw32\startup\thread.c:237]
	# 4 KERNEL32.dll!BaseThreadInitThunk +0x11     (0x7768338a <KERNEL32.dll+0x1338a>)
	Note: @0:00:57.879 in thread 4516
	Note: next higher malloc: 0x00b1b420-0x00b1b440
	Note: 0x00b1b2b0-0x00b1b2b4 overlaps memory 0x00b1b2a0-0x00b1b390 that was freed here:
	Note: # 0 replace_operator_delete                              [d:\drmemory_package\common\alloc_replace.c:2524]
	Note: # 1 pgenlista.dll!IAsyncPG::`scalar deleting destructor'
	Note: # 2 pgenlista.dll!IAsyncPG::Release                      [c:\users\administrator\desktop\psqlodbc\msdtc_enlist.cpp:341]
	Note: # 3 pgenlista.dll!CloseCommitThread                      [c:\users\administrator\desktop\psqlodbc\msdtc_enlist.cpp:971]
	Note: # 4 MSVCR120D.dll!_callthreadstart                       [f:\dd\vctools\crt\crtw32\startup\thread.c:255]
	Note: # 5 MSVCR120D.dll!_threadstart                           [f:\dd\vctools\crt\crtw32\startup\thread.c:237]
	Note: # 6 KERNEL32.dll!BaseThreadInitThunk                    +0x11     (0x7768338a <KERNEL32.dll+0x1338a>)
	Note: instruction: mov    0x10(%eax) -> %eax

This patch fixes the issue.
---
 msdtc_enlist.cpp | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/msdtc_enlist.cpp b/msdtc_enlist.cpp
index ff40761..4795c75 100755
--- a/msdtc_enlist.cpp
+++ b/msdtc_enlist.cpp
@@ -337,8 +337,10 @@ ULONG	STDMETHODCALLTYPE IAsyncPG::Release(void)
 		SLOCK_ACQUIRE();
 		if (refcnt <=0)
 		{
+			const int refcnt_copy = refcnt;
 			mylog("delete %p\n", this);
 			delete this;
+			return refcnt_copy;
 		}
 		else
 		{
-- 
1.9.0

