public inbox for [email protected]  
help / color / mirror / Atom feed
Exception Handling in C-Language Functions?
4+ messages / 4 participants
[nested] [flat]

* Exception Handling in C-Language Functions?
@ 2012-12-03 05:18  rahul143 <[email protected]>
  0 siblings, 2 replies; 4+ messages in thread

From: rahul143 @ 2012-12-03 05:18 UTC (permalink / raw)
  To: [email protected]

I have the created a C-Language function (code is below).  Now, I 
wonder: How do I handle exceptions, for example if malloc cannot assign 
the necessary memory?  Do "palloc" and "pfree" handle such a case 
cleanly?  Should I simply use an "assert"? 

#include "postgres.h" 
#include <string.h> 
#include <stdlib.h> 
#include "fmgr.h" 
#include "libinn.h" 

PG_FUNCTION_INFO_V1(ffiinews_uwildmat); 

/* Wrapper for INN's function uwildmat.  Needs parameters in UTF-8. */ 
Datum ffiinews_uwildmat(PG_FUNCTION_ARGS) { 
    VarChar *text = PG_GETARG_VARCHAR_P(0); 
    VarChar *pattern = PG_GETARG_VARCHAR_P(1); 
    int text_len = VARSIZE(text)-VARHDRSZ; 
    int pattern_len = VARSIZE(pattern)-VARHDRSZ; 
    char *tmp_text = (char *)malloc(text_len+1); 
    if (tmp_text == NULL) 
        ; /* What now? */ 
    char *tmp_pattern = (char *)malloc(pattern_len+1); 
    if (tmp_pattern == NULL) 
        ; /* What now? */ 
    strncpy(tmp_text, VARDATA(text), text_len); 
    tmp_text[text_len] = '\0'; 
    strncpy(tmp_pattern, VARDATA(pattern), pattern_len); 
    tmp_pattern[pattern_len] = '\0'; 
    bool matches = uwildmat(tmp_text, tmp_pattern); 



-----




--
View this message in context: http://postgresql.1045698.n5.nabble.com/GENERAL-Exception-Handling-in-C-Language-Functions-tp5734656...
Sent from the PostgreSQL - general mailing list archive at Nabble.com.


-- 
Sent via pgsql-general mailing list ([email protected])
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general



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

* MODERATOR WARNING  Re: [GENERAL] Exception Handling in C-Language Functions?
@ 2012-12-03 06:14  Alvaro Herrera <[email protected]>
  parent: rahul143 <[email protected]>
  1 sibling, 1 reply; 4+ messages in thread

From: Alvaro Herrera @ 2012-12-03 06:14 UTC (permalink / raw)
  To: [email protected]; [email protected]; +Cc: [email protected]; pgsql-www

MODERATOR WARNING

I noticed that this guy Rahul seems to be reinjecting old list emails
somehow.  Please don't approve anything coming from him.  Observe this
example:

http://postgresql.1045698.n5.nabble.com/Re-GENERAL-MS-Access-and-Stored-procedures-td1843848.html
http://postgresql.1045698.n5.nabble.com/Re-GENERAL-MS-Access-and-Stored-procedures-td5734652.html
The original message was posted in 2005!

The message here is this one (also in 2005):
http://postgresql.1045698.n5.nabble.com/GENERAL-Exception-Handling-in-C-Language-Functions-td1843896...

I have no idea what's going on.  Maybe it's something to do with Nabble.
There are others pending moderation in pgsql-admin and pgsql-hackers too.

rahul143 wrote:
> I have the created a C-Language function (code is below).  Now, I 
> wonder: How do I handle exceptions, for example if malloc cannot assign 
> the necessary memory?  Do "palloc" and "pfree" handle such a case 
> cleanly?  Should I simply use an "assert"? 
> 
> #include "postgres.h" 
> #include <string.h> 
> #include <stdlib.h> 
> #include "fmgr.h" 
> #include "libinn.h" 
> 
> PG_FUNCTION_INFO_V1(ffiinews_uwildmat); 
> 
> /* Wrapper for INN's function uwildmat.  Needs parameters in UTF-8. */ 
> Datum ffiinews_uwildmat(PG_FUNCTION_ARGS) { 
>     VarChar *text = PG_GETARG_VARCHAR_P(0); 
>     VarChar *pattern = PG_GETARG_VARCHAR_P(1); 
>     int text_len = VARSIZE(text)-VARHDRSZ; 
>     int pattern_len = VARSIZE(pattern)-VARHDRSZ; 
>     char *tmp_text = (char *)malloc(text_len+1); 
>     if (tmp_text == NULL) 
>         ; /* What now? */ 
>     char *tmp_pattern = (char *)malloc(pattern_len+1); 
>     if (tmp_pattern == NULL) 
>         ; /* What now? */ 
>     strncpy(tmp_text, VARDATA(text), text_len); 
>     tmp_text[text_len] = '\0'; 
>     strncpy(tmp_pattern, VARDATA(pattern), pattern_len); 
>     tmp_pattern[pattern_len] = '\0'; 
>     bool matches = uwildmat(tmp_text, tmp_pattern); 
> 
> 
> 
> -----
> 
> 
> 
> 
> --
> View this message in context: http://postgresql.1045698.n5.nabble.com/GENERAL-Exception-Handling-in-C-Language-Functions-tp5734656...
> Sent from the PostgreSQL - general mailing list archive at Nabble.com.
> 
> 
> -- 
> Sent via pgsql-general mailing list ([email protected])
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-general


-- 
Álvaro Herrera                http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services


-- 
Sent via pgsql-www mailing list ([email protected])
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-www



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

* Re: MODERATOR WARNING  Re: [GENERAL] Exception Handling in C-Language Functions?
@ 2012-12-03 13:38  David Fetter <[email protected]>
  parent: Alvaro Herrera <[email protected]>
  0 siblings, 0 replies; 4+ messages in thread

From: David Fetter @ 2012-12-03 13:38 UTC (permalink / raw)
  To: Alvaro Herrera <[email protected]>; +Cc: [email protected]; [email protected]; pgsql-www

OK :)

Is there a way to do this automatically?

Cheers,
David.
On Mon, Dec 03, 2012 at 03:14:54AM -0300, Alvaro Herrera wrote:
> MODERATOR WARNING
> 
> I noticed that this guy Rahul seems to be reinjecting old list emails
> somehow.  Please don't approve anything coming from him.  Observe this
> example:
> 
> http://postgresql.1045698.n5.nabble.com/Re-GENERAL-MS-Access-and-Stored-procedures-td1843848.html
> http://postgresql.1045698.n5.nabble.com/Re-GENERAL-MS-Access-and-Stored-procedures-td5734652.html
> The original message was posted in 2005!
> 
> The message here is this one (also in 2005):
> http://postgresql.1045698.n5.nabble.com/GENERAL-Exception-Handling-in-C-Language-Functions-td1843896...
> 
> I have no idea what's going on.  Maybe it's something to do with Nabble.
> There are others pending moderation in pgsql-admin and pgsql-hackers too.
> 
> rahul143 wrote:
> > I have the created a C-Language function (code is below).  Now, I 
> > wonder: How do I handle exceptions, for example if malloc cannot assign 
> > the necessary memory?  Do "palloc" and "pfree" handle such a case 
> > cleanly?  Should I simply use an "assert"? 
> > 
> > #include "postgres.h" 
> > #include <string.h> 
> > #include <stdlib.h> 
> > #include "fmgr.h" 
> > #include "libinn.h" 
> > 
> > PG_FUNCTION_INFO_V1(ffiinews_uwildmat); 
> > 
> > /* Wrapper for INN's function uwildmat.  Needs parameters in UTF-8. */ 
> > Datum ffiinews_uwildmat(PG_FUNCTION_ARGS) { 
> >     VarChar *text = PG_GETARG_VARCHAR_P(0); 
> >     VarChar *pattern = PG_GETARG_VARCHAR_P(1); 
> >     int text_len = VARSIZE(text)-VARHDRSZ; 
> >     int pattern_len = VARSIZE(pattern)-VARHDRSZ; 
> >     char *tmp_text = (char *)malloc(text_len+1); 
> >     if (tmp_text == NULL) 
> >         ; /* What now? */ 
> >     char *tmp_pattern = (char *)malloc(pattern_len+1); 
> >     if (tmp_pattern == NULL) 
> >         ; /* What now? */ 
> >     strncpy(tmp_text, VARDATA(text), text_len); 
> >     tmp_text[text_len] = '\0'; 
> >     strncpy(tmp_pattern, VARDATA(pattern), pattern_len); 
> >     tmp_pattern[pattern_len] = '\0'; 
> >     bool matches = uwildmat(tmp_text, tmp_pattern); 
> > 
> > 
> > 
> > -----
> > 
> > 
> > 
> > 
> > --
> > View this message in context: http://postgresql.1045698.n5.nabble.com/GENERAL-Exception-Handling-in-C-Language-Functions-tp5734656...
> > Sent from the PostgreSQL - general mailing list archive at Nabble.com.
> > 
> > 
> > -- 
> > Sent via pgsql-general mailing list ([email protected])
> > To make changes to your subscription:
> > http://www.postgresql.org/mailpref/pgsql-general
> 
> 
> -- 
> Álvaro Herrera                http://www.2ndQuadrant.com/
> PostgreSQL Development, 24x7 Support, Training & Services

-- 
David Fetter <[email protected]> http://fetter.org/
Phone: +1 415 235 3778  AIM: dfetter666  Yahoo!: dfetter
Skype: davidfetter      XMPP: [email protected]
iCal: webcal://www.tripit.com/feed/ical/people/david74/tripit.ics

Remember to vote!
Consider donating to Postgres: http://www.postgresql.org/about/donate


-- 
Sent via pgsql-www mailing list ([email protected])
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-www



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

* Re: Exception Handling in C-Language Functions?
@ 2012-12-03 14:42  Merlin Moncure <[email protected]>
  parent: rahul143 <[email protected]>
  1 sibling, 0 replies; 4+ messages in thread

From: Merlin Moncure @ 2012-12-03 14:42 UTC (permalink / raw)
  To: rahul143 <[email protected]>; +Cc: [email protected]

On Sun, Dec 2, 2012 at 11:18 PM, rahul143 <[email protected]> wrote:
> I have the created a C-Language function (code is below).  Now, I
> wonder: How do I handle exceptions, for example if malloc cannot assign
> the necessary memory?  Do "palloc" and "pfree" handle such a case
> cleanly?  Should I simply use an "assert"?
>
> #include "postgres.h"
> #include <string.h>
> #include <stdlib.h>
> #include "fmgr.h"
> #include "libinn.h"
>
> PG_FUNCTION_INFO_V1(ffiinews_uwildmat);
>
> /* Wrapper for INN's function uwildmat.  Needs parameters in UTF-8. */
> Datum ffiinews_uwildmat(PG_FUNCTION_ARGS) {
>     VarChar *text = PG_GETARG_VARCHAR_P(0);
>     VarChar *pattern = PG_GETARG_VARCHAR_P(1);
>     int text_len = VARSIZE(text)-VARHDRSZ;
>     int pattern_len = VARSIZE(pattern)-VARHDRSZ;
>     char *tmp_text = (char *)malloc(text_len+1);
>     if (tmp_text == NULL)
>         ; /* What now? */
>     char *tmp_pattern = (char *)malloc(pattern_len+1);
>     if (tmp_pattern == NULL)
>         ; /* What now? */
>     strncpy(tmp_text, VARDATA(text), text_len);
>     tmp_text[text_len] = '\0';
>     strncpy(tmp_pattern, VARDATA(pattern), pattern_len);
>     tmp_pattern[pattern_len] = '\0';
>     bool matches = uwildmat(tmp_text, tmp_pattern);

yes, you should always use database memory api: palloc/pfree and if
necessary memory context switching.  memory allocation error then
raises database exception.  any situation that raises an exception or
other critical needs to be caught and rethrown as database exception
(ereport, etc).

merlin


-- 
Sent via pgsql-general mailing list ([email protected])
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general




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


end of thread, other threads:[~2012-12-03 14:42 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2012-12-03 05:18 Exception Handling in C-Language Functions? rahul143 <[email protected]>
2012-12-03 06:14 ` MODERATOR WARNING  Re: [GENERAL] Exception Handling in C-Language Functions? Alvaro Herrera <[email protected]>
2012-12-03 13:38   ` Re: MODERATOR WARNING  Re: [GENERAL] Exception Handling in C-Language Functions? David Fetter <[email protected]>
2012-12-03 14:42 ` Merlin Moncure <[email protected]>

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