Subject: Re: libssh2_knownhosts_writefile_fp()

Re: libssh2_knownhosts_writefile_fp()

From: Tor Arntsen <tor_at_spacetec.no>
Date: Sun, 21 Nov 2010 14:57:55 +0100

On Sun, Nov 21, 2010 at 14:48, Alexander Lamaison <swish_at_lammy.co.uk> wrote:

> 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.

The above is Windows specfic though. On Unix you can program against
libA and libB and a malloc from one can safely be free'd by the other,
if you so wish. (It's of course possible to manually insert local
versions of any libc function and mess this up, but there's only a
single instance of the libc runtime even if there are multiple API
libs involved. Passing a FILE * from one lib to another is quite
common, and safe on Unix. Not so on Windows, from what I understand.)

-Tor
_______________________________________________
libssh2-devel http://cool.haxx.se/cgi-bin/mailman/listinfo/libssh2-devel
Received on 2010-11-21