Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TfXEU-0004yC-1x for pgsql-general@arkaria.postgresql.org; Mon, 03 Dec 2012 14:42:54 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.72) (envelope-from ) id 1TfXET-0002Wq-FS for pgsql-general@arkaria.postgresql.org; Mon, 03 Dec 2012 14:42:53 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TfXES-0002Vq-1Q for pgsql-general@postgresql.org; Mon, 03 Dec 2012 14:42:52 +0000 Received: from mail-oa0-f46.google.com ([209.85.219.46]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TfXEQ-0004qf-24 for pgsql-general@postgresql.org; Mon, 03 Dec 2012 14:42:51 +0000 Received: by mail-oa0-f46.google.com with SMTP id h16so2677975oag.19 for ; Mon, 03 Dec 2012 06:42:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=5ywzEojdilmPVoxfCc9LNbJ0y9OBCLIwsw1ZlFKBkZE=; b=0tkOVKWhmyDrRYdelm2hMkHrR9vuHuMf89hb/T1t+E2prhJd0mtCYkqVKIhAZtXKTt IyhttSTnZsoyQzTEmgIDsYW2GgEcmqsEnGuSTTETx+v0JDi5GhWeMc1fOBIpOuF3JZnT t7E4+rMm3ARHe6um+EnODZrKsiVIFUCBiC4OqZ4ZMsxVl/UFyD18QuhqO3/0nxt0IAOS QOv1mrTIUpTlM7GdtBzjcs4VHQ/Lpv4fZ/5Vnk+/q5tmoRtTcemVBvIflJi+tg//8ewN wHw+W1vIfWH9uoh+BWA8K6HvR82l8mzEF+4EbtOejm+8SW5Be9ae6aHXYKeUW0JEwbCf j2Ng== MIME-Version: 1.0 Received: by 10.182.95.205 with SMTP id dm13mr4595371obb.9.1354545769001; Mon, 03 Dec 2012 06:42:49 -0800 (PST) Received: by 10.76.71.200 with HTTP; Mon, 3 Dec 2012 06:42:48 -0800 (PST) In-Reply-To: <1354511937712-5734656.post@n5.nabble.com> References: <1354511937712-5734656.post@n5.nabble.com> Date: Mon, 3 Dec 2012 08:42:48 -0600 Message-ID: Subject: Re: Exception Handling in C-Language Functions? From: Merlin Moncure To: rahul143 Cc: pgsql-general@postgresql.org Content-Type: text/plain; charset=ISO-8859-1 X-Pg-Spam-Score: -2.7 (--) 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 On Sun, Dec 2, 2012 at 11:18 PM, 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 > #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); 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 (pgsql-general@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-general