Received: from fw.wintelcom.net (bright@ns1.wintelcom.net [209.1.153.20]) by hub.org (8.10.1/8.11.0) with ESMTP id e9GMPsW52262 for ; Mon, 16 Oct 2000 18:25:54 -0400 (EDT) (envelope-from bright@fw.wintelcom.net) Received: (from bright@localhost) by fw.wintelcom.net (8.10.0/8.10.0) id e9GMPXm23293; Mon, 16 Oct 2000 15:25:33 -0700 (PDT) Date: Mon, 16 Oct 2000 15:25:33 -0700 From: Alfred Perlstein To: Igor Gavriloff Cc: "pgsql-interfaces@postgresql.org" Subject: Re: What's going wrong? Message-ID: <20001016152532.U272@fw.wintelcom.net> References: <39EB8A6B.E1EB3E44@nupes.cefetpr.br> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.2.4i In-Reply-To: <39EB8A6B.E1EB3E44@nupes.cefetpr.br>; from igor@nupes.cefetpr.br on Mon, Oct 16, 2000 at 09:08:27PM -0200 X-Archive-Number: 200010/136 X-Sequence-Number: 1493 * Igor Gavriloff [001016 15:16] wrote: > Hi, > > I'm trying to write a function in C that I can load into postgres, > that basically removes a file from the system (quite unsafe thought, but > it'll be suitable for me here): > > #include > #include > > bool file_remove(text *this) > { > if (remove((char *)this) == 0) return (true); > else return (false); > } > > I compile it and link as instructed for Solaris (where the backend > runs), and load it with: > > CREATE FUNCTION file_remove(text) RETURNS bool AS '/path/file_remove.so' > LANGUAGE 'c'; > > But it always returns false when I try to remove something. I'm sure > that I'm trying to remove files that the the backend's user owns (have > write access). If I try to make any changes on this code (eg.: the > casts...), when I try to use it the backends terminates abnormally. > Does someone can give me a clue? from pgsql/src/includes/c.h: * BTW: when you need to copy a non-null-terminated string (like a text * datum) and add a null, do not do it with StrNCpy(..., len+1). That * might seem to work, but it fetches one byte more than there is in the * text object. One fine day you'll have a SIGSEGV because there isn't * another byte before the end of memory. Don't laugh, we've had real * live bug reports from real live users over exactly this mistake. * Do it honestly with "memcpy(dst,src,len); dst[len] = '\0';", instead. from postgres.h: struct varlena { int32 vl_len; char vl_dat[1]; }; typedef struct varlena text; so it seems that 'text' is not a char *, you'll probably want to use malloc and a memcpy to make a proper C string from a 'text' object. had you thought of running a debugger or looking at the struct definitions before doing such a dangerous cast? -- -Alfred Perlstein - [bright@wintelcom.net|alfred@freebsd.org] "I have the heart of a child; I keep it in a jar on my desk."