[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Function behaves differently when libssh is compiled statically

On 2/1/21 2:31 PM, Anderson Sasaki wrote:

----- Original Message -----
From: "petergeorgenm" <petergeorgenm@xxxxxxxxxxxxxx>
To: libssh@xxxxxxxxxx
Sent: Monday, February 1, 2021 2:04:18 PM
Subject: Function behaves differently when libssh is compiled statically

I wrote a simple connection setup following the tutorial:
#pragma comment(lib, "user32")
#pragma comment(lib, "Advapi32")
#pragma comment(lib, "Shell32")
#pragma comment(lib, "Ws2_32")

#include <stdio.h>
#include <libssh/sftp.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(void){
int ret_c=0;
int rc;
int port=22;
ssh_session the_session=ssh_new();
if(the_session==NULL) {printf("1\n"); return -1;}

ssh_options_set(the_session, SSH_OPTIONS_HOST, "user@");
ssh_options_set(the_session, SSH_OPTIONS_PORT, &port);

if(rc !=SSH_OK) {
printf("2, %d\n", rc);
} else {

return 0;

I used vcpkg to install both static and dynamic versions of libssh, they
reside as follows respectively:
If I compile the previous code dynamically with:
cl /I C:\Users\User\vcpkg\installed\x86-windows\include main.c /link
C:\Users\User\vcpkg\installed\x86-windows\lib\* /SUBSYSTEM:CONSOLE
and then run, the output I see is:

If I compile it statically with:
C:\Users\User\vcpkg\installed\x86-windows-static\include main.c /link
C:\Users\User\vcpkg\installed\x86-windows-static\lib\* /SUBSYSTEM:CONSOLE
and then run, the output I see is:
2, -1

Both versions compile and link without any errors. I also tried to use putty
to ssh to user@ and it works fine.
Please tell me if I need to provide any more debugging information, or if any
information isn't clear.


Could you please try to call ssh_init() before calling any other function from libssh (and ssh_finalize() at the end)?
When using dynamic linking this will be a no-op.

This is a requirement when statically linked. It is mentioned in Chapter 8 of the tutorial and in the API documentation. I'm sorry that this is not better documented.

I believe this was already discussed, but would it be possible to add some more noisy safeguard? At this moment, it is inside of ssh_connect() setting error quite visibly:


At this moment, to read the error, one needs to use ssh_get_error() (I see it properly documented in the tutorial), but I am wondering if we could add this error earlier (even ssh_new?) or add normal fprintf(stderr, ...) as this is is quite huge programming issue and is most likely something that will be really captured in development environment. Even better would be some build-time check, but I do not think it is simply possible.

Jakub Jelen
Senior Software Engineer
Crypto Team, Security Engineering
Red Hat, Inc.

Function behaves differently when libssh is compiled staticallypetergeorgenm <petergeorgenm@xxxxxxxxxxxxxx>
Re: Function behaves differently when libssh is compiled staticallyAnderson Sasaki <ansasaki@xxxxxxxxxx>
Archive administrator: postmaster@lists.cynapses.org