Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TfORe-0004XN-J9 for pgsql-general@arkaria.postgresql.org; Mon, 03 Dec 2012 05:19:54 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1TfORd-0002KE-D1 for pgsql-general@arkaria.postgresql.org; Mon, 03 Dec 2012 05:19:53 +0000 Received: from magus.postgresql.org ([87.238.57.229]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TfOQl-0001pJ-FB for pgsql-general@postgresql.org; Mon, 03 Dec 2012 05:18:59 +0000 Received: from sam.nabble.com ([216.139.236.26]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TfOQk-0007U0-IZ for pgsql-general@postgresql.org; Mon, 03 Dec 2012 05:18:59 +0000 Received: from [192.168.236.26] (helo=sam.nabble.com) by sam.nabble.com with esmtp (Exim 4.72) (envelope-from ) id 1TfOQj-0000Na-NE for pgsql-general@postgresql.org; Sun, 02 Dec 2012 21:18:57 -0800 Date: Sun, 2 Dec 2012 21:18:57 -0800 (PST) From: rahul143 To: pgsql-general@postgresql.org Message-ID: <1354511937712-5734656.post@n5.nabble.com> Subject: Exception Handling in C-Language Functions? MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: 1.2 (+) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-general Precedence: bulk Sender: pgsql-general-owner@postgresql.org 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 #include #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.html Sent from the PostgreSQL - general mailing list archive at Nabble.com. -- Sent via pgsql-general mailing list (pgsql-general@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-general