pg.ddx.io  pgsql-odbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
crash in LIBPQ_execute_query
5+ messages / 3 participants
[nested] [flat]

* crash in LIBPQ_execute_query
@ 2005-09-02 12:12  Scot Loach <sloach@sandvine.com>
  0 siblings, 0 replies; 5+ messages in thread

From: Scot Loach @ 2005-09-02 12:12 UTC (permalink / raw)
  To: pgsql-odbc

Some of the queries we use can be very large.
My code is crashing here:

QResultClass *
LIBPQ_execute_query(ConnectionClass *self,char *query)                          {       
        QResultClass *qres;                                                             PGresult *pgres;
        char            *ptr;
        char            cmdbuffer[ERROR_MSG_LENGTH + 1];
    char                errbuffer[ERROR_MSG_LENGTH + 1];                            int         pos=0;

        strcpy(cmdbuffer,query);


ERROR_MSG_LENGTH is 4096, my query is larger than that.
What's the rationale here and how do we fix it?  This works fine with the old driver.



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: crash in LIBPQ_execute_query
@ 2005-09-02 12:45  Merlin Moncure <merlin.moncure@rcsonline.com>
  0 siblings, 0 replies; 5+ messages in thread

From: Merlin Moncure @ 2005-09-02 12:45 UTC (permalink / raw)
  To: Scot Loach <sloach@sandvine.com>; +Cc: pgsql-odbc

> Some of the queries we use can be very large.
> My code is crashing here:
> 
> QResultClass *
> LIBPQ_execute_query(ConnectionClass *self,char *query)
> {
>         QResultClass *qres;
> PGresult *pgres;
>         char            *ptr;
>         char            cmdbuffer[ERROR_MSG_LENGTH + 1];
>     char                errbuffer[ERROR_MSG_LENGTH + 1];
> int         pos=0;
> 
>         strcpy(cmdbuffer,query);
> 
> 
> ERROR_MSG_LENGTH is 4096, my query is larger than that.
> What's the rationale here and how do we fix it?  This works fine with
the
> old driver.

That code is fishy.  I should be making strncpy, not strcpy, and I don't
understand why ERROR_MSG_LENGTH is used for the length.  The query is
copied again (with strdup) inside QR_set_command.

IMO, it doesn't seem worthwhile to make the extra copy just to be able
to trim spaces leter.

Merlin



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: crash in LIBPQ_execute_query
@ 2005-09-02 12:57  Dave Page <dpage@vale-housing.co.uk>
  0 siblings, 0 replies; 5+ messages in thread

From: Dave Page @ 2005-09-02 12:57 UTC (permalink / raw)
  To: Scot Loach <sloach@sandvine.com>; pgsql-odbc; +Cc: Anoop Kumar <anoopk@pervasive-postgres.com>

 

> -----Original Message-----
> From: pgsql-odbc-owner@postgresql.org 
> [mailto:pgsql-odbc-owner@postgresql.org] On Behalf Of Scot Loach
> Sent: 02 September 2005 13:12
> To: pgsql-odbc@postgresql.org
> Subject: [ODBC] crash in LIBPQ_execute_query
> 
> Some of the queries we use can be very large.
> My code is crashing here:
> 
> QResultClass *
> LIBPQ_execute_query(ConnectionClass *self,char *query)        
>                   {       
>         QResultClass *qres;                                   
>                           PGresult *pgres;
>         char            *ptr;
>         char            cmdbuffer[ERROR_MSG_LENGTH + 1];
>     char                errbuffer[ERROR_MSG_LENGTH + 1];      
>                       int         pos=0;
> 
>         strcpy(cmdbuffer,query);
> 
> 
> ERROR_MSG_LENGTH is 4096, my query is larger than that.
> What's the rationale here and how do we fix it?  This works 
> fine with the old driver.

Urgh. To be honest, I can't see any need to copy that string at all in
there, so the limit is entirely unwarranted. In addition, there's code
in there marked #ifndef USE_LIBPQ (in LIBPQ_Execute!!), and, it sets the
rowcount to -1 if it can't find a space in the SQL query!

I've tidied that up - patch attached. Please let me know how it goes.

Regards, Dave

Attachments:

libpq_execute.patch (application/octet-stream, 3.3K) download Index: connection.c =================================================================== RCS file: /cvsroot/psqlodbc/psqlodbc/connection.c,v retrieving revision 1.110 diff -u -r1.110 connection.c --- connection.c 2 Sep 2005 08:40:59 -0000 1.110 +++ connection.c 2 Sep 2005 12:46:58 -0000 @@ -3159,15 +3159,11 @@ QResultClass * LIBPQ_execute_query(ConnectionClass *self,char *query) { - QResultClass *qres; - PGresult *pgres; - char *ptr; - char cmdbuffer[ERROR_MSG_LENGTH + 1]; - char errbuffer[ERROR_MSG_LENGTH + 1]; - int pos=0; + QResultClass *qres; + PGresult *pgres; + char errbuffer[ERROR_MSG_LENGTH + 1]; + int pos=0; - strcpy(cmdbuffer,query); - mylog("send_query: setting cmdbuffer = '%s'\n", cmdbuffer); pgres = PQexec(self->pgconn,query); qres=QR_Constructor(); @@ -3175,7 +3171,7 @@ { CC_set_error(self, CONNECTION_COULD_NOT_RECEIVE, "Could not allocate memory for result set"); QR_Destructor(qres); - return NULL; + return NULL; } qres->status = PQresultStatus(pgres); @@ -3195,13 +3191,13 @@ return qres; } - if (strnicmp(cmdbuffer, "BEGIN", 5) == 0) + if (strnicmp(query, "BEGIN", 5) == 0) { CC_set_in_trans(self); } - else if (strnicmp(cmdbuffer, "COMMIT", 6) == 0) + else if (strnicmp(query, "COMMIT", 6) == 0) CC_on_commit(self); - else if (strnicmp(cmdbuffer, "ROLLBACK", 8) == 0) + else if (strnicmp(query, "ROLLBACK", 8) == 0) { /* The method of ROLLBACK an original form .... @@ -3210,23 +3206,12 @@ if (PG_VERSION_LT(self, 8.0) || !(contains_token(query, " TO "))) CC_on_abort(self, NO_TRANS); } - else if (strnicmp(cmdbuffer, "END", 3) == 0) + else if (strnicmp(query, "END", 3) == 0) CC_on_commit(self); - else if (strnicmp(cmdbuffer, "ABORT", 5) == 0) + else if (strnicmp(query, "ABORT", 5) == 0) CC_on_abort(self, NO_TRANS); else - { - trim(cmdbuffer); /* get rid of trailing space */ - ptr = strrchr(cmdbuffer, ' '); - if (ptr) -#ifdef USE_LIBPQ - qres->recent_processed_row_count = atoi(PQcmdTuples(pgres)); -#else - qres->recent_processed_row_count = atoi(ptr + 1); -#endif /* USE_LIBPQ */ - else - qres->recent_processed_row_count = -1; - } + qres->recent_processed_row_count = atoi(PQcmdTuples(pgres)); if( (PQresultStatus(pgres) == PGRES_COMMAND_OK) ) { @@ -3234,23 +3219,25 @@ PQclear(pgres); return qres; } + if ( (PQresultStatus(pgres) != PGRES_EMPTY_QUERY) && (PQresultStatus(pgres) != PGRES_TUPLES_OK) ) - { - snprintf(errbuffer, ERROR_MSG_LENGTH, "%s", PQerrorMessage(self->pgconn)); - /* Remove the training CR that libpq adds to the message */ - pos = strlen(errbuffer); - if (pos) - errbuffer[pos - 1] = '\0'; + { + snprintf(errbuffer, ERROR_MSG_LENGTH, "%s", PQerrorMessage(self->pgconn)); + + /* Remove the training CR that libpq adds to the message */ + pos = strlen(errbuffer); + if (pos) + errbuffer[pos - 1] = '\0'; mylog("the server returned the error: %s\n", errbuffer); CC_set_error(self, CONNECTION_SERVER_REPORTED_ERROR, errbuffer); - PQclear(pgres); + PQclear(pgres); return qres; - } + } qres=CC_mapping(self,pgres,qres); - QR_set_command(qres, cmdbuffer); - PQclear(pgres); + QR_set_command(qres, query); + PQclear(pgres); return qres; }
^ permalink raw reply [nested|flat] 5+ messages in thread

* Re: crash in LIBPQ_execute_query
@ 2005-09-02 14:21  Scot Loach <sloach@sandvine.com>
  0 siblings, 0 replies; 5+ messages in thread

From: Scot Loach @ 2005-09-02 14:21 UTC (permalink / raw)
  To: Dave Page <dpage@vale-housing.co.uk>; pgsql-odbc; +Cc: Anoop Kumar <anoopk@pervasive-postgres.com>

That seems to work.
Thanks for the quick reply Dave.

-----Original Message-----
From: Dave Page [mailto:dpage@vale-housing.co.uk]
Sent: Friday, September 02, 2005 8:58 AM
To: Scot Loach; pgsql-odbc@postgresql.org
Cc: Anoop Kumar
Subject: RE: [ODBC] crash in LIBPQ_execute_query


 

> -----Original Message-----
> From: pgsql-odbc-owner@postgresql.org 
> [mailto:pgsql-odbc-owner@postgresql.org] On Behalf Of Scot Loach
> Sent: 02 September 2005 13:12
> To: pgsql-odbc@postgresql.org
> Subject: [ODBC] crash in LIBPQ_execute_query
> 
> Some of the queries we use can be very large.
> My code is crashing here:
> 
> QResultClass *
> LIBPQ_execute_query(ConnectionClass *self,char *query)        
>                   {       
>         QResultClass *qres;                                   
>                           PGresult *pgres;
>         char            *ptr;
>         char            cmdbuffer[ERROR_MSG_LENGTH + 1];
>     char                errbuffer[ERROR_MSG_LENGTH + 1];      
>                       int         pos=0;
> 
>         strcpy(cmdbuffer,query);
> 
> 
> ERROR_MSG_LENGTH is 4096, my query is larger than that.
> What's the rationale here and how do we fix it?  This works 
> fine with the old driver.

Urgh. To be honest, I can't see any need to copy that string at all in
there, so the limit is entirely unwarranted. In addition, there's code
in there marked #ifndef USE_LIBPQ (in LIBPQ_Execute!!), and, it sets the
rowcount to -1 if it can't find a space in the SQL query!

I've tidied that up - patch attached. Please let me know how it goes.

Regards, Dave



^ permalink  raw  reply  [nested|flat] 5+ messages in thread

* Re: crash in LIBPQ_execute_query
@ 2005-09-02 14:29  Dave Page <dpage@vale-housing.co.uk>
  0 siblings, 0 replies; 5+ messages in thread

From: Dave Page @ 2005-09-02 14:29 UTC (permalink / raw)
  To: Scot Loach <sloach@sandvine.com>; pgsql-odbc; +Cc: Anoop Kumar <anoopk@pervasive-postgres.com>

No probs. I'll apply it to CVS. Thanks for testing.

/D 

> -----Original Message-----
> From: Scot Loach [mailto:sloach@sandvine.com] 
> Sent: 02 September 2005 15:22
> To: Dave Page; pgsql-odbc@postgresql.org
> Cc: Anoop Kumar
> Subject: RE: [ODBC] crash in LIBPQ_execute_query
> 
> That seems to work.
> Thanks for the quick reply Dave.
> 
> -----Original Message-----
> From: Dave Page [mailto:dpage@vale-housing.co.uk]
> Sent: Friday, September 02, 2005 8:58 AM
> To: Scot Loach; pgsql-odbc@postgresql.org
> Cc: Anoop Kumar
> Subject: RE: [ODBC] crash in LIBPQ_execute_query
> 
> 
>  
> 
> > -----Original Message-----
> > From: pgsql-odbc-owner@postgresql.org 
> > [mailto:pgsql-odbc-owner@postgresql.org] On Behalf Of Scot Loach
> > Sent: 02 September 2005 13:12
> > To: pgsql-odbc@postgresql.org
> > Subject: [ODBC] crash in LIBPQ_execute_query
> > 
> > Some of the queries we use can be very large.
> > My code is crashing here:
> > 
> > QResultClass *
> > LIBPQ_execute_query(ConnectionClass *self,char *query)        
> >                   {       
> >         QResultClass *qres;                                   
> >                           PGresult *pgres;
> >         char            *ptr;
> >         char            cmdbuffer[ERROR_MSG_LENGTH + 1];
> >     char                errbuffer[ERROR_MSG_LENGTH + 1];      
> >                       int         pos=0;
> > 
> >         strcpy(cmdbuffer,query);
> > 
> > 
> > ERROR_MSG_LENGTH is 4096, my query is larger than that.
> > What's the rationale here and how do we fix it?  This works 
> > fine with the old driver.
> 
> Urgh. To be honest, I can't see any need to copy that string at all in
> there, so the limit is entirely unwarranted. In addition, there's code
> in there marked #ifndef USE_LIBPQ (in LIBPQ_Execute!!), and, 
> it sets the
> rowcount to -1 if it can't find a space in the SQL query!
> 
> I've tidied that up - patch attached. Please let me know how it goes.
> 
> Regards, Dave
> 



^ permalink  raw  reply  [nested|flat] 5+ messages in thread


end of thread, other threads:[~2005-09-02 14:29 UTC | newest]

Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2005-09-02 12:12 crash in LIBPQ_execute_query Scot Loach <sloach@sandvine.com>
2005-09-02 12:45 Re: crash in LIBPQ_execute_query Merlin Moncure <merlin.moncure@rcsonline.com>
2005-09-02 12:57 Re: crash in LIBPQ_execute_query Dave Page <dpage@vale-housing.co.uk>
2005-09-02 14:21 Re: crash in LIBPQ_execute_query Scot Loach <sloach@sandvine.com>
2005-09-02 14:29 Re: crash in LIBPQ_execute_query Dave Page <dpage@vale-housing.co.uk>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox