Subject: Re: libssh2_knownhosts_writefile_fp()

Re: libssh2_knownhosts_writefile_fp()

From: Tor Arntsen <>
Date: Sun, 21 Nov 2010 17:13:31 +0100

On Sun, Nov 21, 2010 at 15:17, Alexander Lamaison <> 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 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.)

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'll find a great number of *nix libraries wich take a FILE * as
function arguments, including X11, OpenSSL, graphviz, and many others.
  But if this is an issue on Windows, and we want libssh2 to be
Windows compatible, then we'll have to stick to the least common
denominator and make sure that it works there too.

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

In *nix it's handled by the kernel and there aren't any issues. But I
don't know how it's done on Windows.

Received on 2010-11-21