From ab050b88e5f2445048b7000bd825fa3868acc954 Mon Sep 17 00:00:00 2001 From: Michael Gregorius Date: Thu, 7 Aug 2025 12:12:18 +0200 Subject: [PATCH] Fix several problems (#8032) ## Fix problems with lifetime of `QByteArray` Fix a problem with the lifetime of a `QByteArray` in `attemptToReconnectOutput` and `attemptToReconnectInput`. The method `constData` was called on a temporary which gets destroyed before `targetName` and `sourceName` are used, thus leading to undefined behavior. ## Fix index checks Fix index checks in `attemptToReconnectOutput` and `attemptToReconnectInput`. The previous code would have allowed an index to be `size` of the underlying vector which would access out-of-bounds memory. ## Braces for one-liner if statements Add braces for one-liner if statements. --- src/core/audio/AudioJack.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/core/audio/AudioJack.cpp b/src/core/audio/AudioJack.cpp index ac4e34365..c07c10ffe 100644 --- a/src/core/audio/AudioJack.cpp +++ b/src/core/audio/AudioJack.cpp @@ -261,7 +261,7 @@ void AudioJack::attemptToConnect(size_t index, const char *lmms_port_type, const void AudioJack::attemptToReconnectOutput(size_t outputIndex, const QString& targetPort) { - if (outputIndex > m_outputPorts.size()) return; + if (outputIndex >= m_outputPorts.size()) { return; } if (targetPort == disconnectedRepresentation) { @@ -270,14 +270,14 @@ void AudioJack::attemptToReconnectOutput(size_t outputIndex, const QString& targ } auto outputName = jack_port_name(m_outputPorts[outputIndex]); - auto targetName = targetPort.toLatin1().constData(); + auto targetName = targetPort.toLatin1(); - attemptToConnect(outputIndex, "output", outputName, targetName); + attemptToConnect(outputIndex, "output", outputName, targetName.constData()); } void AudioJack::attemptToReconnectInput(size_t inputIndex, const QString& sourcePort) { - if (inputIndex > m_inputPorts.size()) return; + if (inputIndex >= m_inputPorts.size()) { return; } if (sourcePort == disconnectedRepresentation) { @@ -286,9 +286,9 @@ void AudioJack::attemptToReconnectInput(size_t inputIndex, const QString& source } auto inputName = jack_port_name(m_inputPorts[inputIndex]); - auto sourceName = sourcePort.toLatin1().constData(); + auto sourceName = sourcePort.toLatin1(); - attemptToConnect(inputIndex, "input", sourceName, inputName); + attemptToConnect(inputIndex, "input", sourceName.constData(), inputName); }