agora inbox for pgsql-interfaces@postgresql.org
help / color / mirror / Atom feedFrom: Tom Lane <tgl@sss.pgh.pa.us>
To: Andrei Petru Mura <mapandrei@gmail.com>
Cc: pgsql-interfaces@postgresql.org
Subject: Re: PQconnectdbParams returns exit value 01 (from gdb).
Date: Mon, 25 Feb 2013 06:13:24 -0500
Message-ID: <19857.1361790804@sss.pgh.pa.us> (raw)
In-Reply-To: <CAK18DJUsbdMYS3m-7az4Zc32S-AurcMZG7MkoQ5G3SCub_1-fg@mail.gmail.com>
References: <CAK18DJUsbdMYS3m-7az4Zc32S-AurcMZG7MkoQ5G3SCub_1-fg@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-interfaces>
Andrei Petru Mura <mapandrei@gmail.com> 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
view thread (2+ messages)
Message-ID: <19857.1361790804@sss.pgh.pa.us>
Permalink: ../19857.1361790804@sss.pgh.pa.us/
Also on: postgresql.org/message-id/19857.1361790804@sss.pgh.pa.us
reply
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: pgsql-interfaces@postgresql.org
Cc: tgl@sss.pgh.pa.us, mapandrei@gmail.com
Subject: Re: PQconnectdbParams returns exit value 01 (from gdb).
In-Reply-To: <19857.1361790804@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox