Page MenuHomePhabricator

"ssh_init" and "ssh_finalize" leaks memory
Open, LowPublic

Description

Whenever ssh_init is called, function "ssh_mutex_lock" will allocate memory for mutex, but this memory is never released. I would expect that mutex "ssh_init_mutex" is released upon "ssh_finalize", but it is not.

Event Timeline

matick created this task.Jul 14 2020, 3:23 PM
Jakuje triaged this task as Low priority.Aug 12 2020, 1:37 PM
Jakuje added a subscriber: Jakuje.

This looks like Windows-only issue. If the pthreads on Linux are used, the mutex is initialized statically, which is probably a reason why it does not pop up for us. It looks like windows locks do not support static initialization.

At this moment, there s no simple way to free this particlar mutex, but given that it is really one-off allocation when libssh is initialized, one-off leak during finalization and this memory is not growing throughout the usage it is very low priority to solve now.

I guess that if in function "static int _ssh_finalize(unsigned destructor)"
this:

if (!destructor) {
    ssh_mutex_unlock(&ssh_init_mutex);
}
return 0;

would be replaced with this:

if (!destructor) {
    ssh_mutex_unlock(&ssh_init_mutex);
    if (!_ssh_initialized) {
        free(ssh_init_mutex);
        ssh_init_mutex = SSH_MUTEX_STATIC_INIT;
    }
}
return 0;

It should work just fine without leaks.

On windows yes, but it would fail everywhere else, where ssh_init_mutex is static mutex. Feel free to submit your suggested change in the gitlab -- it should run it through the CI to check if it works:

https://gitlab.com/libssh/libssh-mirror