when calling PQconnectdb in the main program all run very well, but if i call it inside a function a seg fault appears.
here the code that run
#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;
}
this code run very well.
but when i put PQconnect inside a function, the program will generate a seg fault
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;
}
herein the crash stack
(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
When i declare my function connect as static, the seg fault error doesn’t occur but the returned pointer for the variable conn is NULL
WHY? 🙁
I think you have a mixup between your
connectand the standard libraryconnect. Yourconnecttakes precedence, so whenPQconnectdbtries to callconnect, things would go bad.Try to rename the function.
Making
connectstatic also prevents the mixup, which explains why the crash is removed.Also, you pass the
connparameter toconnectincorrectly. It’s passed by value, so the variable inmainisn’t changed, and staysNULL.You need to pass it by reference.