pg.ddx.io  pgsql-interfaces@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Segmentation fault
10+ messages / 5 participants
[nested] [flat]

* Segmentation fault
@ 1998-07-17 16:15 Stefan Olsson <stefan.olsson@noname4us.com>
  0 siblings, 0 replies; 10+ messages in thread

From: Stefan Olsson @ 1998-07-17 16:15 UTC (permalink / raw)
  To: pgsql-interfaces

Hi, i am testing PostgreSQL and have already run into questions...

Can someone explain why i don“t get a error message the second time i
run this program saying "TABLE slask ALREADY EXISTS", instead i get
"Segmentation fault (core dumped)"

Thanks !

/Stefan

#include <stdio.h>
#include "libpq-fe.h"

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

main()
{
  char *pghost, *pgport, *pgoptions, *pgtty;
  char* dbName;

  PGconn* conn;
  PGresult* res;

  pghost = NULL;
  pgport = NULL;
  pgoptions = NULL;
  pgtty = NULL;
  dbName = "test";

  conn = PQsetdb(pghost, pgport, pgoptions, pgtty, dbName);

  if (PQstatus(conn) == CONNECTION_BAD)
  {
    fprintf(stderr,"%s",PQerrorMessage(conn));
    exit_nicely(conn);
  }

  // create the table
  res = PQexec(conn, "CREATE TABLE slask(name char(20), phone
char(10))");
  if (PQresultStatus(res) != PGRES_COMMAND_OK)
  {
    fprintf(stderr,"CREATE TABLE slask failed !");
    PQclear(res);
    exit_nicely(conn);
  }
  PQclear(res);

  PQfinish(conn);
}





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

* Segmentation Fault
@ 2003-04-03 06:40 Frankie Lam <frankie@ucr.com.hk>
  2003-04-03 09:31 ` Re: Segmentation Fault Frankie Lam <frankie@ucr.com.hk>
  2003-04-04 02:16 ` Re: Segmentation Fault Frankie Lam <frankie@ucr.com.hk>
  0 siblings, 2 replies; 10+ messages in thread

From: Frankie Lam @ 2003-04-03 06:40 UTC (permalink / raw)
  To: pgsql-interfaces

Hi,

I want to ask what does the error message mean by
"0x40023abb in resetPQExpBuffer () from /usr/local/pgsql/lib/libpq.so.3"?
I got this message using GDB to trace the program.
(Segmentation fault occurswhen run the program directly.)

I reviewed the program many times, and found this is only to happen when
I call the `ctime()'  or `time()' in a program function.

code fragment FYI
--8<------------------------
// `res = PQexec(conn, sql)' here..
...
...
  }
  else if (!strcmp(PQfname(res, i), SYNCTIME_FIELD_NAME))
  {
//   puts("7");
   //char chP[30];
   printf("timer=%ld\n", time(timer)); // <====== without this line, the
program works
//   printf("%s", ctime(time(NULL)));
//   printf("%s", asctime(localtime(&timer)));
   sprintf(tmp, "'1999-12-11 09:50:58'::timestamp, ");
   strcat(p2, tmp);
  }
  else if (!strcmp(PQfname(res, i), SERVERID_FIELD_NAME))
  {
   puts("8");
   sprintf(tmp, "'%s'", PQgetvalue(res, 0, i));
   strcat(p2, tmp);
  }
 }
...
...
freeing resources here
---------->8--------------

Thank you very much.

Frankie Lam.




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

* Re: Segmentation Fault
  2003-04-03 06:40 Segmentation Fault Frankie Lam <frankie@ucr.com.hk>
@ 2003-04-03 09:31 ` Frankie Lam <frankie@ucr.com.hk>
  2003-04-04 00:02   ` Re: Segmentation Fault Philip Yarra <philip@utiba.com>
  1 sibling, 1 reply; 10+ messages in thread

From: Frankie Lam @ 2003-04-03 09:31 UTC (permalink / raw)
  To: pgsql-interfaces

Oh, I'm so sorry....
I've made a stupid mistake... and this mistake is at the cost of 3 days
work.
The segmentation fault is totally irrelevant to the time functions in
previous thread.

Buggy Code Fragment FYI
--8<------------------------
..
..
   len =strlen(PQgetvalue(res, 0, i)) * 2 + 1;  // <=========
   printf("***********length=%d\n", len);
   tmp2 = (char *) calloc(sizeof(char), len);
   strcat(p2, "'");

   PQescapeString(tmp2, PQgetvalue(res, 0, i), len); // <========= this
line,
   // PQescapeString won't stop at '\0', so it will go out
   // of range of the returning value by PQgetvalue().

   strcat(p2, tmp2);
   strcat(p2, "', ");
   free(tmp2);
   tmp2 = NULL;
..
..
---------->8--------------

Here I have one more question, is there any debugging tools other than GDB?
I'm not quite used to it, debugging segmentation fault is really a pain. :-(

Thx,
Frankie Lam

"Frankie Lam" <frankie@ucr.com.hk> wrote in message
news:b6gku9$2llu$1@news.hub.org...
> Hi,
>
> I want to ask what does the error message mean by
> "0x40023abb in resetPQExpBuffer () from /usr/local/pgsql/lib/libpq.so.3"?
> I got this message using GDB to trace the program.
> (Segmentation fault occurswhen run the program directly.)
>
> I reviewed the program many times, and found this is only to happen when
> I call the `ctime()'  or `time()' in a program function.
>
> code fragment FYI
> --8<------------------------
> // `res = PQexec(conn, sql)' here..
> ...
> ...
>   }
>   else if (!strcmp(PQfname(res, i), SYNCTIME_FIELD_NAME))
>   {
> //   puts("7");
>    //char chP[30];
>    printf("timer=%ld\n", time(timer)); // <====== without this line, the
> program works
> //   printf("%s", ctime(time(NULL)));
> //   printf("%s", asctime(localtime(&timer)));
>    sprintf(tmp, "'1999-12-11 09:50:58'::timestamp, ");
>    strcat(p2, tmp);
>   }
>   else if (!strcmp(PQfname(res, i), SERVERID_FIELD_NAME))
>   {
>    puts("8");
>    sprintf(tmp, "'%s'", PQgetvalue(res, 0, i));
>    strcat(p2, tmp);
>   }
>  }
> ...
> ...
> freeing resources here
> ---------->8--------------
>
> Thank you very much.
>
> Frankie Lam.
>
>




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

* Re: Segmentation Fault
  2003-04-03 06:40 Segmentation Fault Frankie Lam <frankie@ucr.com.hk>
  2003-04-03 09:31 ` Re: Segmentation Fault Frankie Lam <frankie@ucr.com.hk>
@ 2003-04-04 00:02   ` Philip Yarra <philip@utiba.com>
  0 siblings, 0 replies; 10+ messages in thread

From: Philip Yarra @ 2003-04-04 00:02 UTC (permalink / raw)
  To: Frankie Lam <frankie@ucr.com.hk>; pgsql-interfaces

On Thu, 3 Apr 2003 19:31, Frankie Lam wrote:
> Here I have one more question, is there any debugging tools other than GDB?
> I'm not quite used to it, debugging segmentation fault is really a pain.

I'm using Redhat 7.3, with "everything" installed. It has ddd, which is a nice 
frontend to gdb. More detail here: http://www.gnu.org/software/ddd/

Also you might like to consider memprof (useful in tracking down memory leaks) 
- another graphical tool. 

Hope this helps!
Regards, Philip.




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

* Re: Segmentation Fault
  2003-04-03 06:40 Segmentation Fault Frankie Lam <frankie@ucr.com.hk>
@ 2003-04-04 02:16 ` Frankie Lam <frankie@ucr.com.hk>
  1 sibling, 0 replies; 10+ messages in thread

From: Frankie Lam @ 2003-04-04 02:16 UTC (permalink / raw)
  To: pgsql-interfaces

Thanks all for replying.

>  >    len =strlen(PQgetvalue(res, 0, i)) * 2 + 1;  // <=========
> Why not using PQgetlength(res, 0, i)?

Actually I'm quite new to libPQ, that's why I didn't know there's a
PQgetlength function. :-)

>  >    PQescapeString(tmp2, PQgetvalue(res, 0, i), len); // <=========
> One last question:
> Why are you using PQescapeString on PQgetvalue?
>
> The doc says:
> If you want to include strings that have been received from a source
> that is not trustworthy (for example, because a random user entered
> them), you cannot directly include them in SQL queries for security
> reasons. Instead, you have to quote special characters that are
> otherwise interpreted by the SQL parser.

hmm.., I did this on purpose. Because I want to grab SQL commands
I previously stored in a table from one database, then reformat them
and store them into another database.
E.g. the reformatted string looks like:
INSERT INTO tableOnAnotherDB(sqlCMD) values('delete from test where
name=''\\\\''');

I'm trying to implement a scale-down'ed version of synchronous
replication, only limited to replicating between two databases.
And this C program is responsible for synchronizing the two
databases and keep them consistent, in case one of them failed
and is up again later and other cases. (checking who's master, and
who's slave, are they accepting requests etc, is done by the server
side stored functions. VB + ODBC + PL/PGSQL in backend)

Regards
Frankie Lam




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

* Re: Segmentation Fault
@ 2003-04-03 09:11 Christoph Haller <ch@rodos.fzk.de>
  0 siblings, 0 replies; 10+ messages in thread

From: Christoph Haller @ 2003-04-03 09:11 UTC (permalink / raw)
  To: pgsql-interfaces; +Cc: frankie@ucr.com.hk

 >
 > I want to ask what does the error message mean by
 > "0x40023abb in resetPQExpBuffer () from /usr/local/pgsql/lib/libpq.so.3"?
 > I got this message using GDB to trace the program.
 > (Segmentation fault occurswhen run the program directly.)
 >
 > I reviewed the program many times, and found this is only to happen when
 > I call the `ctime()'  or `time()' in a program function.
 >
 > code fragment FYI
 > --8<------------------------
 > // `res = PQexec(conn, sql)' here..
 > ...
 > ...
 >   }
 >   else if (!strcmp(PQfname(res, i), SYNCTIME_FIELD_NAME))
 >   {
 > //   puts("7");
 >    //char chP[30];
 >    printf("timer=%ld\n", time(timer)); // <====== without this line, the
 > program works
 > //   printf("%s", ctime(time(NULL)));
 > //   printf("%s", asctime(localtime(&timer)));
 >    sprintf(tmp, "'1999-12-11 09:50:58'::timestamp, ");

I would like to see the declaration of the "timer" variable.
Is it "time_t"? Then you should code "&timer" as in localtime(&timer).
Is it "time_t *"? Then, did you alloc'd memory?
Regards, Christoph




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

* Re: Segmentation Fault
@ 2003-04-03 11:23 Christoph Haller <ch@rodos.fzk.de>
  2003-04-03 11:26 ` Re: Segmentation Fault James Williamson <james@nameonthe.net>
  0 siblings, 1 reply; 10+ messages in thread

From: Christoph Haller @ 2003-04-03 11:23 UTC (permalink / raw)
  To: pgsql-interfaces; +Cc: frankie@ucr.com.hk

 >
 > Oh, I'm so sorry....
 > I've made a stupid mistake... and this mistake is at the cost of 3 days
 > work.
 > The segmentation fault is totally irrelevant to the time functions in
 > previous thread.
 >
Keep your head.
AFAICT segmentation faults are every C programmer's nightmare
and best known for hard to find. I know of people searching for
mistakes even longer and I've seen mistakes more stupid than yours.

 >
 > Here I have one more question, is there any debugging tools other 
than GDB?
 > I'm not quite used to it, debugging segmentation fault is really a 
pain. :-(
 >
I don't know of one. I'm using xdb on HP-UX, which is quite similar I 
suppose.
I've found it useful when trying to track down, do not trace, but let it 
simply run
within the debugger and then use the "down" command to find your code area.
There is also a function called memorymap(int show_stats) which often 
detects
memory faults before the operating systems does.

 >    len =strlen(PQgetvalue(res, 0, i)) * 2 + 1;  // <=========
Why not using PQgetlength(res, 0, i)?
 >    PQescapeString(tmp2, PQgetvalue(res, 0, i), len); // <=========
One last question:
Why are you using PQescapeString on PQgetvalue?

The doc says:
If you want to include strings that have been received from a source 
that is not trustworthy (for example, because a random user entered 
them), you cannot directly include them in SQL queries for security 
reasons. Instead, you have to quote special characters that are 
otherwise interpreted by the SQL parser.

So PQgetvalue is trustworthy. There should be no need to call 
PQescapeString,
except you were using a binary cursor. But if you were using one, then why
would you want to escape the result instead of just using a normal cursor?

Regards, Christoph




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

* Re: Segmentation Fault
  2003-04-03 11:23 Re: Segmentation Fault Christoph Haller <ch@rodos.fzk.de>
@ 2003-04-03 11:26 ` James Williamson <james@nameonthe.net>
  2003-04-04 02:12   ` Re: Segmentation Fault Frankie Lam <frankie@ucr.com.hk>
  0 siblings, 1 reply; 10+ messages in thread

From: James Williamson @ 2003-04-03 11:26 UTC (permalink / raw)
  To: pgsql-interfaces

----- Original Message -----
From: "Christoph Haller" <ch@rodos.fzk.de>
> >
>  > Oh, I'm so sorry....
>  > I've made a stupid mistake... and this mistake is at the cost of 3 days
>  > work.
>  > The segmentation fault is totally irrelevant to the time functions in
>  > previous thread.
>  >
> Keep your head.
> AFAICT segmentation faults are every C programmer's nightmare
> and best known for hard to find. I know of people searching for
> mistakes even longer and I've seen mistakes more stupid than yours.
>
>  >
>  > Here I have one more question, is there any debugging tools other
> than GDB?
>  > I'm not quite used to it, debugging segmentation fault is really a
> pain. :-(
>  >

Have you looked at efence, purify and valgrind?

Regards,

James Williamson
www.nameonthe.net
Tel: +44 208 7415453
Fax: + 44 208 7411615


> I don't know of one. I'm using xdb on HP-UX, which is quite similar I
> suppose.
> I've found it useful when trying to track down, do not trace, but let it
> simply run
> within the debugger and then use the "down" command to find your code
area.
> There is also a function called memorymap(int show_stats) which often
> detects
> memory faults before the operating systems does.
>
>  >    len =strlen(PQgetvalue(res, 0, i)) * 2 + 1;  // <=========
> Why not using PQgetlength(res, 0, i)?
>  >    PQescapeString(tmp2, PQgetvalue(res, 0, i), len); // <=========
> One last question:
> Why are you using PQescapeString on PQgetvalue?
>
> The doc says:
> If you want to include strings that have been received from a source
> that is not trustworthy (for example, because a random user entered
> them), you cannot directly include them in SQL queries for security
> reasons. Instead, you have to quote special characters that are
> otherwise interpreted by the SQL parser.
>
> So PQgetvalue is trustworthy. There should be no need to call
> PQescapeString,
> except you were using a binary cursor. But if you were using one, then why
> would you want to escape the result instead of just using a normal cursor?
>
> Regards, Christoph
>
>
> ---------------------------(end of broadcast)---------------------------
> TIP 4: Don't 'kill -9' the postmaster
>




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

* Re: Segmentation Fault
  2003-04-03 11:23 Re: Segmentation Fault Christoph Haller <ch@rodos.fzk.de>
  2003-04-03 11:26 ` Re: Segmentation Fault James Williamson <james@nameonthe.net>
@ 2003-04-04 02:12   ` Frankie Lam <frankie@ucr.com.hk>
  0 siblings, 0 replies; 10+ messages in thread

From: Frankie Lam @ 2003-04-04 02:12 UTC (permalink / raw)
  To: pgsql-interfaces

>
> Have you looked at efence, purify and valgrind?
>

I will, thanks :-). (I'm a newbie)




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

* Re: Segmentation Fault
@ 2003-04-04 07:44 Christoph Haller <ch@rodos.fzk.de>
  0 siblings, 0 replies; 10+ messages in thread

From: Christoph Haller @ 2003-04-04 07:44 UTC (permalink / raw)
  To: pgsql-interfaces; +Cc: frankie@ucr.com.hk

 > >
 > > The doc says:
 > > If you want to include strings that have been received from a source
 > > that is not trustworthy (for example, because a random user entered
 > > them), you cannot directly include them in SQL queries for security
 > > reasons. Instead, you have to quote special characters that are
 > > otherwise interpreted by the SQL parser.
 >
 > hmm.., I did this on purpose. Because I want to grab SQL commands
 > I previously stored in a table from one database, then reformat them
 > and store them into another database.
 > E.g. the reformatted string looks like:
 > INSERT INTO tableOnAnotherDB(sqlCMD) values('delete from test where
 > name=''\\\\''');
 >
Ok, I see. I didn't think of that possibility. You are right.
Regards, Christoph




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


end of thread, other threads:[~2003-04-04 07:44 UTC | newest]

Thread overview: 10+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
1998-07-17 16:15 Segmentation fault Stefan Olsson <stefan.olsson@noname4us.com>
2003-04-03 06:40 Segmentation Fault Frankie Lam <frankie@ucr.com.hk>
2003-04-03 09:31 ` Re: Segmentation Fault Frankie Lam <frankie@ucr.com.hk>
2003-04-04 00:02   ` Re: Segmentation Fault Philip Yarra <philip@utiba.com>
2003-04-04 02:16 ` Re: Segmentation Fault Frankie Lam <frankie@ucr.com.hk>
2003-04-03 09:11 Re: Segmentation Fault Christoph Haller <ch@rodos.fzk.de>
2003-04-03 11:23 Re: Segmentation Fault Christoph Haller <ch@rodos.fzk.de>
2003-04-03 11:26 ` Re: Segmentation Fault James Williamson <james@nameonthe.net>
2003-04-04 02:12   ` Re: Segmentation Fault Frankie Lam <frankie@ucr.com.hk>
2003-04-04 07:44 Re: Segmentation Fault Christoph Haller <ch@rodos.fzk.de>

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