Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WhDvv-00018E-DY for pgsql-odbc@arkaria.postgresql.org; Mon, 05 May 2014 08:07:31 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1WhDvu-00010V-U0 for pgsql-odbc@arkaria.postgresql.org; Mon, 05 May 2014 08:07:30 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WhDvu-00010P-AU for pgsql-odbc@postgresql.org; Mon, 05 May 2014 08:07:30 +0000 Received: from smtp-outbound-2.vmware.com ([208.91.2.13]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WhDvm-00085G-Qk for pgsql-odbc@postgresql.org; Mon, 05 May 2014 08:07:29 +0000 Received: from sc9-mailhost1.vmware.com (sc9-mailhost1.vmware.com [10.113.161.71]) by smtp-outbound-2.vmware.com (Postfix) with ESMTP id 6782F2876A; Mon, 5 May 2014 01:07:21 -0700 (PDT) Received: from zcs-prod-ext-mta-2.vmware.com (zcs-prod-ext-mta-2.vmware.com [10.113.62.224]) by sc9-mailhost1.vmware.com (Postfix) with ESMTP id 55D301823E; Mon, 5 May 2014 01:07:21 -0700 (PDT) Received: from zcs-prod-ext-mta-2.vmware.com (localhost.localdomain [127.0.0.1]) by zcs-prod-ext-mta-2.vmware.com (Postfix) with ESMTP id 7DF82C005F; Mon, 5 May 2014 01:07:24 -0700 (PDT) Received: from [192.168.1.90] (zimbra-prod-ext-proxy-vip.vmware.com [10.113.63.87]) by zcs-prod-ext-mta-2.vmware.com (Postfix) with ESMTPSA; Mon, 5 May 2014 01:07:23 -0700 (PDT) Message-ID: <536746B6.50100@vmware.com> Date: Mon, 05 May 2014 11:07:18 +0300 From: Heikki Linnakangas Organization: VMware User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Icedove/24.4.0 MIME-Version: 1.0 To: malcolm.macleod@tshwanedje.com, Michael Paquier CC: PostgreSQL mailing lists Subject: Re: CC_send_query_append crash References: <1398789887.4578.19.camel@watchmen.homenetwork> <1398944856.4578.51.camel@watchmen.homenetwork> In-Reply-To: <1398944856.4578.51.camel@watchmen.homenetwork> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -7.6 (-------) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-odbc Precedence: bulk Sender: pgsql-odbc-owner@postgresql.org On 05/01/2014 02:47 PM, Malcolm MacLeod wrote: > >> wrote: >>> The crash seems to occur because CC_send_query_append crash takes a >>> local copy of the pointer 'self->sock' at the top of the function, >>> 'self' is then passed around to various functions (some of which have >>> the side effect of setting self->sock to NULL (and deleting) if there is >>> a lost connection) and then the local copy of the pointer (which is now >>> dangling) is dereferenced lower down in the function. >>> Essentially if there is a disconnect while CC_send_query_append is >>> running there is a risk of crash. >> Looking at the code, I am seeing that the problem is related to >> CC_on_abort where conn->sock is set to NULL when the connection is >> considered as dead. And I am indeed seeing two code paths (when >> sending the 'C' message there is an ABORT check and in cleanup >> section) that could use this NULL socket afterwards. Your patch is >> perhaps a bit too much. So I am proposing the attached patch instead. >> Let me know if this fixes your issue as well. > > Thanks for the fast response! > Your proposed patch would also fix the issue, so I have no problem with > it being used instead. > > I guess from my side I just don't personally understand the point of > keeping the local pointer copy at all (it just seems like an invitation > for this sort of thing to occur) - so it made more sense to me to remove > it entirely to prevent future occurrences of similar issues - although I > suppose also the less code disturbed the better. I am not overly > familiar with the code so can't say what is best. I agree that keeping the local pointer copy is an accident waiting to happen. Michael's more surgical approach would make sense if we had to carefully track the places where self->sock might become NULL. But we don't, as all the SOCK_* macros and functions contain NULL checks. I think the extra null check that both patch versions added to the cleanup code path is also unnecessary and wrong. SOCK_get_errcode() returns SOCKET_CLOSED error if the argument is null, which will cause the connection to be marked as dead, which is the right thing to do. However, when I removed that check, the "diagnostics" regression test started to fail. What happens is that when the socket is closed while processing results in CC_send_query_append, the incomplete QResult object is discarded, even though it contained a useful error message. So we lose some information that way. But that in turn is pretty simple to fix: if we don't set ReadyToReturn in the cleanup routine, then the partial QResult object is returned again. Looking closer at the loop in CC_send_query_append(), I think it's basically wrong to set ReadyToReturn when an error occurs. ReadyToReturn indicates that the whole result set has been processed, and we're ready to send a new query. In many of the error conditions where we currently set ReadyToReturn, we're clearly not ready to process the next query. For example, in the 'T' case, if QR_Constructor() fails because of out-of-memory, we set ReadyToReturn=true and just bail out of the loop. That's wrong - we must process the rest of the result set before returning. Anyway, I committed your original patch with minor changes, which fixes your original problem (commit 22b151e). But the error handling in that function could use some more work... - Heikki -- Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-odbc