Skip to content

Commit

Permalink
MidiALSA: Fix crash when snd_seq_subscribe_port fails
Browse files Browse the repository at this point in the history
The pthread and lock are never created if the "if" in the constructor
fails, and the members are never initialized.  But the destructor
unconditionally destroys them.  Fix this by adding a guard flag.

This fixes a segfault I encountered on Arch with Pipewire as the ALSA
server.

    Thread 1 "node" received signal SIGSEGV, Segmentation fault.
    0x00007ffff428aebd in pthread_cancel () from /usr/bin/../lib/libc.so.6
    (gdb) bt
    #0  0x00007ffff428aebd in pthread_cancel () from /usr/bin/../lib/libc.so.6
    jazz-soft#1  0x00007ffff3b773ff in CMidiInHW::~CMidiInHW (this=0x55555572a430, __in_chrg=<optimized out>) at ../../midi/MidiALSA.cpp:366
    jazz-soft#2  0x00007ffff3b77452 in CMidiInHW::~CMidiInHW (this=0x55555572a430, __in_chrg=<optimized out>) at ../../midi/MidiALSA.cpp:369
    jazz-soft#3  0x00007ffff3b77105 in CMidiALSA::MidiInOpen[abi:cxx11](wchar_t const*, void*) (this=0x5555557b9880, name=0x5555556b3cf0 L"input", p=0x55555579e3a0) at ../../midi/MidiALSA.cpp:329
    jazz-soft#4  0x00007ffff3b6f0c8 in MidiInOpen (env=0x55555579e410, args=0x7fffffff9e20) at ../jazz-midi.cpp:461
    jazz-soft#5  0x00007ffff535bd33 in ?? () from /usr/bin/../lib/libnode.so.115
    jazz-soft#6  0x00007ffff5840d97 in ?? () from /usr/bin/../lib/libnode.so.115
    jazz-soft#7  0x00007ffff5841942 in v8::internal::Builtin_HandleApiCall(int, unsigned long*, v8::internal::Isolate*) () from /usr/bin/../lib/libnode.so.115
    jazz-soft#8  0x00007ffff56cadf6 in ?? () from /usr/bin/../lib/libnode.so.115
  • Loading branch information
thirtythreeforty committed Oct 10, 2023
1 parent c391cc4 commit c1ba6da
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
13 changes: 8 additions & 5 deletions midi/MidiALSA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ str_type CMidiALSA::MidiInOpen(const char_type*name, void* p)


CMidiInHW::CMidiInHW(snd_seq_t* seq, int client, int port, const char_type* n, void* p)
: m_Seq(seq), m_C(-1), m_P(-1), plugin(p)
: m_Seq(seq), m_C(-1), m_P(-1), plugin(p), have_thread(false)
{
if (!m_Seq) return;
name = n;
Expand All @@ -349,6 +349,7 @@ CMidiInHW::CMidiInHW(snd_seq_t* seq, int client, int port, const char_type* n, v
m_C = client; m_P = port;
pthread_mutex_init(&lock, 0);
pthread_create(&thread, 0, MidiInThread, this);
have_thread = true;
}
}

Expand All @@ -359,10 +360,12 @@ CMidiInHW::~CMidiInHW()
snd_seq_unsubscribe_port(m_Seq, m_Sub);
snd_seq_port_subscribe_free(m_Sub);
snd_seq_delete_port(m_Seq, m_Port);
pthread_mutex_lock(&lock);
pthread_cancel(thread);
pthread_mutex_unlock(&lock);
pthread_mutex_destroy(&lock);
if(have_thread) {
pthread_mutex_lock(&lock);
pthread_cancel(thread);
pthread_mutex_unlock(&lock);
pthread_mutex_destroy(&lock);
}
}


Expand Down
1 change: 1 addition & 0 deletions midi/MidiALSA.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ friend class CMidiALSA;
snd_rawmidi_t* handle;
pthread_mutex_t lock;
pthread_t thread;
bool have_thread;
CMidiInHW(snd_seq_t* seq, int client, int port, const char_type* n, void* p);
~CMidiInHW();
virtual void ReadMidiInput(void*,std::vector<unsigned char>&);
Expand Down

0 comments on commit c1ba6da

Please sign in to comment.