Subject: Re: libssh2_knownhosts_writefile_fp()

Re: libssh2_knownhosts_writefile_fp()

From: Alexander Lamaison <swish_at_lammy.co.uk>
Date: Sun, 21 Nov 2010 16:48:41 +0000

On 21 November 2010 16:13, Tor Arntsen <tor_at_spacetec.no> wrote:
> On Sun, Nov 21, 2010 at 15:17, Alexander Lamaison <swish_at_lammy.co.uk> wrote:
>
>> This is a myth (one I've discussed at length at various times on this
>> list).  You're right that it is much more common on Windows than Unix
>> but this is luck rather than design.  It relies on the fact that on
>> Unix every .so is typically linked against the same version of libc so
>> only one instance is ever loaded.  So, you're lucky, on most Unix
>> systems your software won't crash ... until it does.
>>
>> Passing a FILE* between your code and the _C-runtime that created it_
>> is safe.  Anything else is not.
>
> Er, no. It's not a myth, and certainly not related to luck. It would
> be a weird setup indeed if you manage to link a process so that it
> ends up with two different C runtime libraries. It's actually very
> difficult to do. For a normal Unix executable to be linked against two
> different libc.so.. I've yet to see it, and I've been working with all
> the major *nix systems. You would have to do tricks with static
> linking to achieve something like that (AIX has some weird way of
> handling multiple dynamic libc versions at the same time, but even
> there I can't actually think of how to achieve that inside the same
> process in any easy way.)

Sure, it's hard to do on Linux, no denying that. If it weren't it
would be forbidden to use FILE*s in API rather than simply not
recommended. The point is that this isn't a Windows problem. It will
affect any OS where you run a program that you have managed to link to
two C-runtimes.

> Thus, passing FILE * between libs is perfectly safe, and it's in fact
> _by_ design and not the opposite.. and when it comes to file
> descriptors (ints as returned by open(2), not FILE *), you can even
> pass them between processes, as when in forking and passing on open
> file descriptors, or pass them via UNIX domain sockets.

You're probably right. My main objection to this should have been
that file descriptors are POSIX rather than C and therefore not as
portable.

Just out of interest, what happens on Linux in this situation: libA.so
links to libc.so.1 and libB.so links to libc.so.2 and your program
links to libA.so and libB.so? Then you pass something from libA
across a call to libB. Would calls normally be redirected to a single
libc?

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