Subject: Re: libssh2_knownhosts_writefile_fp()

Re: libssh2_knownhosts_writefile_fp()

From: Ben Kibbey <bjk_at_luxsci.net>
Date: Sun, 21 Nov 2010 10:25:16 -0500

On Sun, Nov 21, 2010 at 01:48:45PM +0000, Alexander Lamaison wrote:
> > Is it only the FILE* structure? What about the file descriptor of the
> > opened stream obtained from fileno(3)? Is that safe?
>
> Also not safe. Any 'object' that belongs to one instance of the
> C-runtime must not be manipulated by another instance. The file
> descriptor data is owned by the C-runtime regardless of whether you
> access it via a fileno or a FILE*.
>
> It's a bit like calling malloc() in one instance and calling free() in
> the other which will also corrupt the runtime memory and most likely
> crash. While the memory allocated by malloc() is not owned by the
> C-runtime (you can access the memory anywhere you wish), the table of
> memory allocations *is*. When you call another instance's free() on
> the pointer, it is the table of allocations that is changed, not the
> allocated memory. As each runtime instance has its own table, boo boo
> ensues.
>
> Unfortunately, as far as I'm aware, every file operation manipulates
> internal tables so you can never safely use any form of opened file
> descriptor across API calls.

I'm sorry if I'm asking the same question, but what about a file
descriptor returned from open(2)? The data structures rely on the OS
rather than libraries don't they?

-- 
Ben Kibbey
[XMPP: bjk AT thiessen DOT im] - [IRC: (bjk) FreeNode/OFTC]
_______________________________________________
libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel
Received on 2010-11-21