使用PQconnectdb的libpq的奇怪行为

时间:2012-06-26 07:57:21

标签: c postgresql-8.4

在主程序中调用PQconnectdb时运行得非常好,但是如果我在函数内调用它会出现seg错误。 这里运行的代码

#include <stdio.h>
#include <stdlib.h>
#include <postgresql/libpq-fe.h>



#define PG_HOST    "127.0.0.1"
#define PG_USER    "postgres"
#define PG_DB      "postgres"
#define PG_PASS    "postgres"
#define PG_PORT    5432



static void
exit_nicely(PGconn *conn)
{
    PQfinish(conn);
    exit(1);
}


int main( void )

{

char       conninfo[250];
PGconn     *conn = NULL;
PGresult   *pgres = NULL;

sprintf(conninfo, "user=%s password=%s dbname=%s hostaddr=%s port=%d", PG_USER, PG_PASS, PG_DB, PG_HOST, PG_PORT);
conn = PQconnectdb(conninfo);

if (PQstatus(conn) != CONNECTION_OK)
{
    fprintf(stderr, "ERROR: Connection to database failed: %s", PQerrorMessage(conn));
    exit_nicely(conn);
}


PQfinish(conn);

return 0;
}

此代码运行良好。

但是当我把PQconnect放在一个函数中时,程序会产生一个seg错误

int connect(char* conninfo, PGconn* conn)
{

conn = PQconnectdb(conninfo);

if (PQstatus(conn) != CONNECTION_OK)
{
    fprintf(stderr, "ERROR: Connection to database failed: %s", PQerrorMessage(conn));
    exit_nicely(conn);
}

return 1;
}




int main( void )

{

char       conninfo[250];
PGconn     *conn = NULL;
PGresult   *pgres = NULL;

sprintf(conninfo, "user=%s password=%s dbname=%s hostaddr=%s port=%d", PG_USER, PG_PASS, PG_DB, PG_HOST, PG_PORT);

connect(conninfo, conn);
if(!conn)
 fprintf(stderr, "conn is null.\n");


PQfinish(conn);

return 0;

}

这里是崩溃堆栈

(gdb) where
#0  __strlen_sse2 () at ../sysdeps/x86_64/multiarch/../strlen.S:32
#1  0x00007ffff7893086 in __GI___strdup (s=0x7 <Address 0x7 out of bounds>) at strdup.c:42
#2  0x00007ffff7bbbd87 in ?? () from /usr/lib/libpq.so.5
#3  0x00007ffff7bbc2a5 in ?? () from /usr/lib/libpq.so.5
#4  0x00007ffff7bbe389 in PQconnectStart () from /usr/lib/libpq.so.5
#5  0x00007ffff7bbe416 in PQconnectdb () from /usr/lib/libpq.so.5
#6  0x0000000000400912 in connect (conninfo=0x7 <Address 0x7 out of bounds>, conn=0x60a630) at pqconnect.c:25
#7  0x00007ffff7bbcadb in PQconnectPoll () from /usr/lib/libpq.so.5
#8  0x00007ffff7bbd77e in ?? () from /usr/lib/libpq.so.5
#9  0x00007ffff7bbe3b4 in PQconnectStart () from /usr/lib/libpq.so.5
#10 0x00007ffff7bbe416 in PQconnectdb () from /usr/lib/libpq.so.5
#11 0x0000000000400912 in connect (conninfo=0x7fffffffe600 "user=btel_user password=JwN5K9e18PsTb dbname=ULIC hostaddr=127.0.0.1 port=5432", conn=0x0) at pqconnect.c:25

#12 0x00000000004009e3 in main()at pqconnect.c:49

当我声明我的函数connect as static时,不会发生seg错误错误,但变量conn的返回指针是NULL

为什么? :(

2 个答案:

答案 0 :(得分:2)

我认为您的connect和标准库connect之间存在混淆。您的connect优先,因此当PQconnectdb尝试拨打connect时,情况会变糟。
尝试重命名该功能 使connect静态也可以防止混淆,这解释了崩溃被删除的原因。

另外,您错误地将conn参数传递给connect。它是按值传递的,因此main中的变量不会更改,并保持NULL 您需要通过引用传递它。

答案 1 :(得分:0)

注意:@Aleix ==&gt;这段代码运行良好。

int pg_connect(char* conninfo, PGconn** conn)
{

    *conn = PQconnectdb(conninfo);
    if (PQstatus(*conn) != CONNECTION_OK) {
        fprintf(stderr,
                "ERROR: Connection to database failed: %s",
                PQerrorMessage(*conn));
        return 0;
    }

    return 1;
}

/* set correctely your values here */
#define PG_HOST    "127.0.0.1"
#define PG_USER    "postgres"
#define PG_DB      "postgres"
#define PG_PASS    "postgres"
#define PG_PORT    5432

int main(int argc, char *argv[])
{
    char        conninfo[250];
    PGconn     *conn  = NULL;
    PGresult   *pgres = NULL;

    sprintf(conninfo,
            "user=%s password=%s dbname=%s hostaddr=%s port=%d",
            PG_USER, PG_PASS, PG_DB, PG_HOST, PG_PORT);

    if (!pg_connect(conninfo, &conn)) {
        goto end;
    }


    /*
        Here fit your staff
     */


    end:
      PQfinish(conn);
      return 0;

}