Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WfXbF-0001x0-FW for pgsql-odbc@arkaria.postgresql.org; Wed, 30 Apr 2014 16:43:13 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1WfXbE-0003MV-MZ for pgsql-odbc@arkaria.postgresql.org; Wed, 30 Apr 2014 16:43:12 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WfB9N-00034K-6B for pgsql-odbc@postgresql.org; Tue, 29 Apr 2014 16:44:57 +0000 Received: from mail-ee0-x22f.google.com ([2a00:1450:4013:c00::22f]) by makus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1WfB9J-0007rV-KP for pgsql-odbc@postgresql.org; Tue, 29 Apr 2014 16:44:56 +0000 Received: by mail-ee0-f47.google.com with SMTP id b15so532421eek.34 for ; Tue, 29 Apr 2014 09:44:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=sender:message-id:subject:from:reply-to:to:date:organization :content-type:mime-version; bh=vV/OSO7E3NESbDYPxeUstlma/Pp5tHs0Ls8bFKWHQPg=; b=Cr03lmP1BQbbfiu6jqfzCUFuMEa+0GfjUK7cPGpmIq2zwxkGwB/E7LYCcjL5a1ptXa J3xnGmXECxBVskvYjd0f8mZ0L/va+hkmMjI0xyvMUkXArijkm7JDebW5hNB98AEqqsOM RPrRdMgya1UEGZYv+SK6jO2aGl3jJoPpESFNhw8wSzfCVfszV6lL11YwFat/vTOPTump vsIhjpke25dldgU1ptPQeKANXFATb9gMKLBHZA5z2MGrkzAiQzt04hL1EyfcWrTQ7y2J PAY3hWEXXadXTBurtvtRlz8tUso7btfwNZAHSulBRpGfxM6y6TdI1Chf9xZiI3X/9auf NZAA== X-Received: by 10.14.211.133 with SMTP id w5mr376256eeo.5.1398789892303; Tue, 29 Apr 2014 09:44:52 -0700 (PDT) Received: from [10.0.0.24] (105-236-113-85.access.mtnbusiness.co.za. [105.236.113.85]) by mx.google.com with ESMTPSA id a42sm60547322ees.10.2014.04.29.09.44.50 for (version=TLSv1 cipher=RC4-SHA bits=128/128); Tue, 29 Apr 2014 09:44:51 -0700 (PDT) Message-ID: <1398789887.4578.19.camel@watchmen.homenetwork> Subject: CC_send_query_append crash From: Malcolm MacLeod Reply-To: malcolm.macleod@tshwanedje.com To: pgsql-odbc@postgresql.org Date: Tue, 29 Apr 2014 18:44:47 +0200 Organization: TshwaneDJe HLT Content-Type: multipart/mixed; boundary="=-zDOLTt6ys53wXaSYTsMH" X-Mailer: Evolution 3.8.5 Mime-Version: 1.0 X-Pg-Spam-Score: -0.5 (/) 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 --=-zDOLTt6ys53wXaSYTsMH Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Hello, While stress testing our software to handle network reconnects/disconnects I have run into a crash in your ODBC driver. 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. I hope that you can fix this soon. Regards, Malcolm MacLeod --=-zDOLTt6ys53wXaSYTsMH Content-Description: Content-Disposition: attachment; filename="Dangling_socket_pointer_fix.diff" Content-Type: text/x-patch; name="Dangling_socket_pointer_fix.diff"; charset="us-ascii" Content-Transfer-Encoding: 7bit --- C:\Users\m\Desktop\psqlodbc-09.03.0210\connection.c 2014-03-06 15:48:44.000000000 +0200 +++ C:\Users\m\Desktop\psqlodbc-09.03.0210_fixed\connection.c 2014-04-06 22:47:59.000000000 +0200 @@ -2743,13 +2743,13 @@ CSTR per_query_svp = "_per_query_svp_"; CSTR rlscmd = "RELEASE"; static size_t lenbgncmd = 0, lenrbkcmd = 0, lensvpcmd = 0, lenrlscmd = 0, lenperqsvp = 0; size_t qrylen; int id; - SocketClass *sock = self->sock; + //SocketClass *sock = self->sock; int maxlen, empty_reqs; BOOL ReadyToReturn = FALSE, query_completed = FALSE, beforeV2 = PG_VERSION_LT(self, 6.4), aborted = FALSE, @@ -2812,13 +2812,13 @@ if ((NULL == query) || (query[0] == '\0')) { CLEANUP_FUNC_CONN_CS(func_cs_count, self); return NULL; } - if (SOCK_get_errcode(sock) != 0) + if (SOCK_get_errcode(self->sock) != 0) { CC_set_error(self, CONNECTION_COULD_NOT_SEND, "Could not send Query to backend", func); CC_on_abort(self, CONN_DEAD); CLEANUP_FUNC_CONN_CS(func_cs_count, self); return NULL; } @@ -2866,14 +2866,14 @@ goto cleanup; } } } } - SOCK_put_char(sock, 'Q'); - if (SOCK_get_errcode(sock) != 0) + SOCK_put_char(self->sock, 'Q'); + if (SOCK_get_errcode(self->sock) != 0) { CC_set_error(self, CONNECTION_COULD_NOT_SEND, "Could not send Query to backend", func); goto cleanup; } if (stmt) SC_forget_unnamed(stmt); @@ -2896,46 +2896,46 @@ if (query_rollback) { leng += (UInt4) (lensvpcmd + 1 + lenperqsvp + 1); leng += (UInt4) (1 + lenrlscmd + 1 + lenperqsvp); } leng++; - SOCK_put_int(sock, leng + 4, 4); + SOCK_put_int(self->sock, leng + 4, 4); inolog("leng=%d\n", leng); } if (issue_begin) { - SOCK_put_n_char(sock, bgncmd, lenbgncmd); - SOCK_put_n_char(sock, semi_colon, 1); + SOCK_put_n_char(self->sock, bgncmd, lenbgncmd); + SOCK_put_n_char(self->sock, semi_colon, 1); discard_next_begin = TRUE; } if (query_rollback) { char cmd[64]; snprintf(cmd, sizeof(cmd), "%s %s;", svpcmd, per_query_svp); - SOCK_put_n_char(sock, cmd, strlen(cmd)); + SOCK_put_n_char(self->sock, cmd, strlen(cmd)); discard_next_savepoint = TRUE; } - SOCK_put_n_char(sock, query, qrylen); + SOCK_put_n_char(self->sock, query, qrylen); if (appendq) { - SOCK_put_n_char(sock, semi_colon, 1); - SOCK_put_n_char(sock, appendq, strlen(appendq)); + SOCK_put_n_char(self->sock, semi_colon, 1); + SOCK_put_n_char(self->sock, appendq, strlen(appendq)); } if (query_rollback) { char cmd[64]; snprintf(cmd, sizeof(cmd), ";%s %s", rlscmd, per_query_svp); - SOCK_put_n_char(sock, cmd, strlen(cmd)); + SOCK_put_n_char(self->sock, cmd, strlen(cmd)); } - SOCK_put_n_char(sock, NULL_STRING, 1); - leng = SOCK_flush_output(sock); + SOCK_put_n_char(self->sock, NULL_STRING, 1); + leng = SOCK_flush_output(self->sock); - if (SOCK_get_errcode(sock) != 0) + if (SOCK_get_errcode(self->sock) != 0) { CC_set_error(self, CONNECTION_COULD_NOT_SEND, "Could not send Query to backend", func); goto cleanup; } mylog("send_query: done sending query %dbytes flushed\n", leng); @@ -2958,40 +2958,40 @@ } } res = cmdres; while (!ReadyToReturn) { /* what type of message is coming now ? */ - id = SOCK_get_id(sock); + id = SOCK_get_id(self->sock); - if ((SOCK_get_errcode(sock) != 0) || (id == EOF)) + if ((SOCK_get_errcode(self->sock) != 0) || (id == EOF)) { CC_set_error(self, CONNECTION_NO_RESPONSE, "No response from the backend", func); mylog("send_query: 'id' - %s\n", CC_get_errormsg(self)); kill_conn = TRUE; ReadyToReturn = TRUE; break; } mylog("send_query: got id = '%c'\n", id); - response_length = SOCK_get_response_length(sock); + response_length = SOCK_get_response_length(self->sock); inolog("send_query response_length=%d\n", response_length); switch (id) { case 'A': /* Asynchronous Messages are ignored */ - (void) SOCK_get_int(sock, 4); /* id of notification */ - SOCK_get_string(sock, msgbuffer, ERROR_MSG_LENGTH); + (void) SOCK_get_int(self->sock, 4); /* id of notification */ + SOCK_get_string(self->sock, msgbuffer, ERROR_MSG_LENGTH); /* name of the relation the message comes from */ break; case 'C': /* portal query command, no tuples * returned */ /* read in the return message from the backend */ - SOCK_get_string(sock, cmdbuffer, ERROR_MSG_LENGTH); - if (SOCK_get_errcode(sock) != 0) + SOCK_get_string(self->sock, cmdbuffer, ERROR_MSG_LENGTH); + if (SOCK_get_errcode(self->sock) != 0) { CC_set_error(self, CONNECTION_NO_RESPONSE, "No response from backend while receiving a portal query command", func); mylog("send_query: 'C' - %s\n", CC_get_errormsg(self)); ReadyToReturn = TRUE; } else @@ -3083,14 +3083,14 @@ * empty query, and keep reading out of the pipe until * an 'I' is received */ if (empty_reqs == 0) { - SOCK_put_string(sock, "Q "); - SOCK_flush_output(sock); + SOCK_put_string(self->sock, "Q "); + SOCK_flush_output(self->sock); empty_reqs++; } } break; case 'Z': /* Backend is ready for new query (6.4) */ if (empty_reqs == 0) @@ -3110,14 +3110,14 @@ case 'I': /* The server sends an empty query */ /* There is a closing '\0' following the 'I', so we eat it */ if (PROTOCOL_74(ci) && 0 == response_length) swallow = '\0'; else - swallow = SOCK_get_char(sock); - if ((swallow != '\0') || SOCK_get_errcode(sock) != 0) + swallow = SOCK_get_char(self->sock); + if ((swallow != '\0') || SOCK_get_errcode(self->sock) != 0) { CC_set_errornumber(self, CONNECTION_BACKEND_CRAZY); QR_set_message(res, "Unexpected protocol character from backend (send_query - I)"); QR_set_rstatus(res, PORES_FATAL_ERROR); kill_conn = TRUE; ReadyToReturn = TRUE; @@ -3143,13 +3143,13 @@ aborted = TRUE; query_completed = TRUE; break; case 'P': /* get the Portal name */ - SOCK_get_string(sock, msgbuffer, ERROR_MSG_LENGTH); + SOCK_get_string(self->sock, msgbuffer, ERROR_MSG_LENGTH); break; case 'T': /* Tuple results start here */ if (query_completed) { res->next = QR_Constructor(); if (!res->next) @@ -3239,31 +3239,31 @@ alsize *= 2; pos = len; continue; } } } - SOCK_put_char(sock, 'd'); /* CopyData */ - SOCK_put_int(sock, 4 + len, 4); - SOCK_put_n_char(sock, buf, len); + SOCK_put_char(self->sock, 'd'); /* CopyData */ + SOCK_put_int(self->sock, 4 + len, 4); + SOCK_put_n_char(self->sock, buf, len); pos = 0; } if (aborted) { mylog("copy fail\n"); - SOCK_put_char(sock, 'f'); /* CopyFail */ - SOCK_put_int(sock, 18, 4); - SOCK_put_string(sock, "Out of memory"); + SOCK_put_char(self->sock, 'f'); /* CopyFail */ + SOCK_put_int(self->sock, 18, 4); + SOCK_put_string(self->sock, "Out of memory"); } else { mylog("copy done\n"); - SOCK_put_char(sock, 'c'); /* CopyDone */ - SOCK_put_int(sock, 4, 4); + SOCK_put_char(self->sock, 'c'); /* CopyDone */ + SOCK_put_int(self->sock, 4, 4); } - SOCK_flush_output(sock); + SOCK_flush_output(self->sock); free(buf); } break; case 'H': /* Copy out command began successfully */ break; case 'c': /* Copy out command donesuccessfully */ @@ -3313,13 +3313,13 @@ mylog("send_query: error - %s\n", CC_get_errormsg(self)); ReadyToReturn = TRUE; retres = NULL; break; } - if (SOCK_get_errcode(sock) != 0) + if (SOCK_get_errcode(self->sock) != 0) break; if (CONN_DOWN == self->status) break; /* * There was no ReadyForQuery response before 6.4. */ @@ -3328,13 +3328,13 @@ if (empty_reqs == 0 && query_completed) break; } } cleanup: - if (SOCK_get_errcode(sock) != 0) + if (self->sock && SOCK_get_errcode(self->sock) != 0) { if (0 == CC_get_errornumber(self)) CC_set_error(self, CONNECTION_COMMUNICATION_ERROR, "Communication error while sending query", func); kill_conn = TRUE; ReadyToReturn = TRUE; } --=-zDOLTt6ys53wXaSYTsMH Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-odbc mailing list (pgsql-odbc@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-odbc --=-zDOLTt6ys53wXaSYTsMH--