psqlodbc libpq version: quiet error on connect failure

Поиск
Список
Период
Сортировка
Искать

psqlodbc libpq version: quiet error on connect failure

От:
Marko Ristola <Marko.Ristola@kolumbus.fi>
Дата:

Problem

psqlodbc, with libpq enabled, will do exit(1), if connecting into the
database fails.
A good fix seems to be to remove the exit_nicely() function call.

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

Connect code:

    self->pgconn = PQconnectdb(conninfo);
    if (PQstatus(self->pgconn) != CONNECTION_OK)
    {
       
CC_set_error(self,CONNECTION_COULD_NOT_ESTABLISH,PQerrorMessage(self->pgconn));
        mylog("could not establish connection to the database %s
\n",PQerrorMessage(self->pgconn));
        exit_nicely(self->pgconn);
        free(conninfo);
        return 0;
    }

------------------------

Marko Ristola

Re: psqlodbc libpq version: quiet error on connect failure

От:
"Dave Page" <dpage@vale-housing.co.uk>
Дата:
Thanks, I've applied a patch to fix this. Iirc, exit_nicely() is used in
the libpq examples in the PostgreSQL manual :-)

/D 

> -----Original Message-----
> From: pgsql-odbc-owner@postgresql.org 
> [mailto:pgsql-odbc-owner@postgresql.org] On Behalf Of Marko Ristola
> Sent: 20 July 2005 12:23
> To: pgsql-odbc@postgresql.org
> Subject: [ODBC] psqlodbc libpq version: quiet error on connect failure
> 
> 
> Problem
> 
> psqlodbc, with libpq enabled, will do exit(1), if connecting into the
> database fails.
> A good fix seems to be to remove the exit_nicely() function call.
> 
> ----------------------
> static void
> exit_nicely(PGconn *conn)
> {
>         PQfinish(conn);
>         exit(1);
> }
> ----------------------
> 
> Connect code:
> 
>     self->pgconn = PQconnectdb(conninfo);
>     if (PQstatus(self->pgconn) != CONNECTION_OK)
>     {
>        
> CC_set_error(self,CONNECTION_COULD_NOT_ESTABLISH,PQerrorMessag
> e(self->pgconn));
>         mylog("could not establish connection to the database %s
> \n",PQerrorMessage(self->pgconn));
>         exit_nicely(self->pgconn);
>         free(conninfo);
>         return 0;
>     }
> 
> ------------------------
> 
> Marko Ristola
> 
> 
> ---------------------------(end of 
> broadcast)---------------------------
> TIP 5: don't forget to increase your free space map settings
> 

Re: psqlodbc libpq version: quiet error on connect failure

От:
Marko Ristola <Marko.Ristola@kolumbus.fi>
Дата:

I found one small required fix:
self->pgconn needs to be set as NULL
after PQfinish(self->pgconn) on line 3117.

Otherwise there comes a double free problem later.

    self->pgconn = PQconnectdb(conninfo);
    if (PQstatus(self->pgconn) != CONNECTION_OK)
    {
       
CC_set_error(self,CONNECTION_COULD_NOT_ESTABLISH,PQerrorMessage(self->pgconn));
        mylog("could not establish connection to the database %s
\n",PQerrorMessage(self->pgconn));
        PQfinish(self->pgconn);
        self->pgconn = NULL; //THIS IS THE FIX, THAT IS NEEDED TO AVOID
THE DOUBLE free().
        free(conninfo);
        return 0;
    }

Marko Ristola

Dave Page wrote:

>Thanks, I've applied a patch to fix this. Iirc, exit_nicely() is used in
>the libpq examples in the PostgreSQL manual :-)
>
>/D 
>
>  
>
>>-----Original Message-----
>>From: pgsql-odbc-owner@postgresql.org 
>>[mailto:pgsql-odbc-owner@postgresql.org] On Behalf Of Marko Ristola
>>Sent: 20 July 2005 12:23
>>To: pgsql-odbc@postgresql.org
>>Subject: [ODBC] psqlodbc libpq version: quiet error on connect failure
>>
>>
>>Problem
>>
>>psqlodbc, with libpq enabled, will do exit(1), if connecting into the
>>database fails.
>>A good fix seems to be to remove the exit_nicely() function call.
>>
>>----------------------
>>static void
>>exit_nicely(PGconn *conn)
>>{
>>        PQfinish(conn);
>>        exit(1);
>>}
>>----------------------
>>
>>Connect code:
>>
>>    self->pgconn = PQconnectdb(conninfo);
>>    if (PQstatus(self->pgconn) != CONNECTION_OK)
>>    {
>>       
>>CC_set_error(self,CONNECTION_COULD_NOT_ESTABLISH,PQerrorMessag
>>e(self->pgconn));
>>        mylog("could not establish connection to the database %s
>>\n",PQerrorMessage(self->pgconn));
>>        exit_nicely(self->pgconn);
>>        free(conninfo);
>>        return 0;
>>    }
>>
>>------------------------
>>
>>Marko Ristola
>>
>>
>>---------------------------(end of 
>>broadcast)---------------------------
>>TIP 5: don't forget to increase your free space map settings
>>
>>    
>>
>
>---------------------------(end of broadcast)---------------------------
>TIP 4: Have you searched our list archives?
>
>               http://archives.postgresql.org
>  
>

Re: psqlodbc libpq version: quiet error on connect failure

От:
"Dave Page" <dpage@vale-housing.co.uk>
Дата:
Thanks, fix applied.

/D 

> -----Original Message-----
> From: Marko Ristola [mailto:Marko.Ristola@kolumbus.fi] 
> Sent: 20 July 2005 14:22
> To: Dave Page
> Cc: pgsql-odbc@postgresql.org
> Subject: Re: [ODBC] psqlodbc libpq version: quiet error on 
> connect failure
> 
> 
> I found one small required fix:
> self->pgconn needs to be set as NULL
> after PQfinish(self->pgconn) on line 3117.
> 
> Otherwise there comes a double free problem later.
> 
>     self->pgconn = PQconnectdb(conninfo);
>     if (PQstatus(self->pgconn) != CONNECTION_OK)
>     {
>        
> CC_set_error(self,CONNECTION_COULD_NOT_ESTABLISH,PQerrorMessag
> e(self->pgconn));
>         mylog("could not establish connection to the database %s
> \n",PQerrorMessage(self->pgconn));
>         PQfinish(self->pgconn);
>         self->pgconn = NULL; //THIS IS THE FIX, THAT IS 
> NEEDED TO AVOID
> THE DOUBLE free().
>         free(conninfo);
>         return 0;
>     }
> 
> Marko Ristola
> 
> Dave Page wrote:
> 
> >Thanks, I've applied a patch to fix this. Iirc, 
> exit_nicely() is used in
> >the libpq examples in the PostgreSQL manual :-)
> >
> >/D 
> >
> >  
> >
> >>-----Original Message-----
> >>From: pgsql-odbc-owner@postgresql.org 
> >>[mailto:pgsql-odbc-owner@postgresql.org] On Behalf Of Marko Ristola
> >>Sent: 20 July 2005 12:23
> >>To: pgsql-odbc@postgresql.org
> >>Subject: [ODBC] psqlodbc libpq version: quiet error on 
> connect failure
> >>
> >>
> >>Problem
> >>
> >>psqlodbc, with libpq enabled, will do exit(1), if 
> connecting into the
> >>database fails.
> >>A good fix seems to be to remove the exit_nicely() function call.
> >>
> >>----------------------
> >>static void
> >>exit_nicely(PGconn *conn)
> >>{
> >>        PQfinish(conn);
> >>        exit(1);
> >>}
> >>----------------------
> >>
> >>Connect code:
> >>
> >>    self->pgconn = PQconnectdb(conninfo);
> >>    if (PQstatus(self->pgconn) != CONNECTION_OK)
> >>    {
> >>       
> >>CC_set_error(self,CONNECTION_COULD_NOT_ESTABLISH,PQerrorMessag
> >>e(self->pgconn));
> >>        mylog("could not establish connection to the database %s
> >>\n",PQerrorMessage(self->pgconn));
> >>        exit_nicely(self->pgconn);
> >>        free(conninfo);
> >>        return 0;
> >>    }
> >>
> >>------------------------
> >>
> >>Marko Ristola
> >>
> >>
> >>---------------------------(end of 
> >>broadcast)---------------------------
> >>TIP 5: don't forget to increase your free space map settings
> >>
> >>    
> >>
> >
> >---------------------------(end of 
> broadcast)---------------------------
> >TIP 4: Have you searched our list archives?
> >
> >               http://archives.postgresql.org
> >  
> >
> 
> 
FAQ