Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1U9vzs-0000Mc-Mi for pgsql-interfaces@arkaria.postgresql.org; Mon, 25 Feb 2013 11:13:28 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1U9vzs-00032M-1S for pgsql-interfaces@arkaria.postgresql.org; Mon, 25 Feb 2013 11:13:28 +0000 Received: from makus.postgresql.org ([2001:4800:7903:4::125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1U9vzr-00032H-Eu for pgsql-interfaces@postgresql.org; Mon, 25 Feb 2013 11:13:27 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1U9vzq-0005dk-Gs for pgsql-interfaces@postgresql.org; Mon, 25 Feb 2013 11:13:26 +0000 Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.5/8.14.5) with ESMTP id r1PBDOqg019858; Mon, 25 Feb 2013 06:13:24 -0500 (EST) From: Tom Lane To: Andrei Petru Mura cc: pgsql-interfaces@postgresql.org Subject: Re: PQconnectdbParams returns exit value 01 (from gdb). In-reply-to: References: Comments: In-reply-to Andrei Petru Mura message dated "Mon, 25 Feb 2013 12:42:10 +0200" Date: Mon, 25 Feb 2013 06:13:24 -0500 Message-ID: <19857.1361790804@sss.pgh.pa.us> X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-interfaces Precedence: bulk Sender: pgsql-interfaces-owner@postgresql.org Andrei Petru Mura writes: > I'm trying to create a connection to my database with libpq. Bellow is my > code: > keywords = malloc(6 * sizeof(char *)); > values = malloc(6 * sizeof(char *)); That looks less than safe ... what happens if you have more than five lines in the creds file? > while (fgets(line, LINE_SIZE, creds) != NULL) { > if (line[strlen(line) - 1] == '\n') > line[strlen(line) - 1] = '\0'; > prop = line; > while(*(prop++) != '=') { > i++; > } > tmp = prop; > prop = malloc(i + 1); > strncpy(prop, line, i); > prop[i] = '\0'; > keywords[j++] = prop; > val = malloc(strlen(line) - strlen(prop) + 1); > strcpy(val, tmp); > values[k++] = val; > i = 0; > } This has got a few issues, like it'll die badly if there's no '=' in a line, and not behave too well if a line overruns the fixed buffer length. > keywords[++j] = NULL; > values[++k] = NULL; But your real problem is here, where you're leaving undefined holes in the arrays. These should be j++ and k++, not ++j and ++k. If malloc doesn't give you back all-zeroes storage, PQconnectdbParams will be led to try to dereference garbage pointer values, resulting in your SIGSEGVs. regards, tom lane -- Sent via pgsql-interfaces mailing list (pgsql-interfaces@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-interfaces