mirror of
https://github.com/kavishdevar/librepods.git
synced 2026-05-02 02:50:35 +00:00
android: fix ControlCommand.parseFromBytes outofbounds crash
This commit is contained in:
@@ -207,10 +207,7 @@ class AACPManager {
|
|||||||
identifier: ControlCommandIdentifiers, value: ByteArray
|
identifier: ControlCommandIdentifiers, value: ByteArray
|
||||||
) {
|
) {
|
||||||
val existingStatus = getControlCommandStatus(identifier)
|
val existingStatus = getControlCommandStatus(identifier)
|
||||||
if (existingStatus == value) {
|
if (existingStatus?.value.contentEquals(value)) {
|
||||||
controlCommandStatusList.remove(existingStatus)
|
|
||||||
}
|
|
||||||
if (existingStatus != null) {
|
|
||||||
controlCommandStatusList.remove(existingStatus)
|
controlCommandStatusList.remove(existingStatus)
|
||||||
}
|
}
|
||||||
controlCommandListeners[identifier]?.forEach { listener ->
|
controlCommandListeners[identifier]?.forEach { listener ->
|
||||||
@@ -414,7 +411,13 @@ class AACPManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Opcodes.CONTROL_COMMAND -> {
|
Opcodes.CONTROL_COMMAND -> {
|
||||||
val controlCommand = ControlCommand.fromByteArray(packet)
|
val controlCommand = try {
|
||||||
|
ControlCommand.fromByteArray(packet)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Log.w(TAG, "Failed to parse control command: ${e.message}")
|
||||||
|
callback?.onUnknownPacketReceived(packet)
|
||||||
|
return
|
||||||
|
}
|
||||||
setControlCommandStatusValue(
|
setControlCommandStatusValue(
|
||||||
ControlCommandIdentifiers.fromByte(controlCommand.identifier) ?: return,
|
ControlCommandIdentifiers.fromByte(controlCommand.identifier) ?: return,
|
||||||
controlCommand.value
|
controlCommand.value
|
||||||
@@ -1078,25 +1081,25 @@ class AACPManager {
|
|||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
fun fromByteArray(data: ByteArray): ControlCommand {
|
fun fromByteArray(data: ByteArray): ControlCommand {
|
||||||
if (data.size < 4) {
|
var offset = 0
|
||||||
throw IllegalArgumentException("Data array too short to parse ControlCommand")
|
while (data.size - offset >= 4 &&
|
||||||
|
data[offset] == 0x04.toByte() &&
|
||||||
|
data[offset + 1] == 0x00.toByte() &&
|
||||||
|
data[offset + 2] == 0x04.toByte() &&
|
||||||
|
data[offset + 3] == 0x00.toByte()
|
||||||
|
) {
|
||||||
|
offset += 4
|
||||||
}
|
}
|
||||||
if (data[0] == 0x04.toByte() && data[1] == 0x00.toByte() && data[2] == 0x04.toByte() && data[3] == 0x00.toByte()) {
|
if (data.size - offset < 7) {
|
||||||
val newData = ByteArray(data.size - 4)
|
throw IllegalArgumentException("Too short for ControlCommand")
|
||||||
System.arraycopy(data, 4, newData, 0, data.size - 4)
|
|
||||||
return fromByteArray(newData)
|
|
||||||
}
|
}
|
||||||
if (data[0] != Opcodes.CONTROL_COMMAND) {
|
if (data[offset] != Opcodes.CONTROL_COMMAND) {
|
||||||
throw IllegalArgumentException("Data array does not start with CONTROL_COMMAND opcode")
|
throw IllegalArgumentException("Invalid opcode")
|
||||||
}
|
}
|
||||||
val identifier = data[2]
|
val identifier = data[offset + 2]
|
||||||
|
val value = data.copyOfRange(offset + 3, offset + 7)
|
||||||
val value = ByteArray(4)
|
val trimmed = value.dropLastWhile { it == 0x00.toByte() }.toByteArray()
|
||||||
System.arraycopy(data, 3, value, 0, 4)
|
return ControlCommand(identifier, if (trimmed.isEmpty()) byteArrayOf(0x00) else trimmed)
|
||||||
|
|
||||||
val trimmedValue = value.dropLastWhile { it == 0x00.toByte() }.toByteArray()
|
|
||||||
val finalValue = if (trimmedValue.isEmpty()) byteArrayOf(0x00) else trimmedValue
|
|
||||||
return ControlCommand(identifier, finalValue)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1122,7 +1125,13 @@ class AACPManager {
|
|||||||
Log.d(TAG, "Sending packet: ${packet.joinToString(" ") { "%02X".format(it) }}")
|
Log.d(TAG, "Sending packet: ${packet.joinToString(" ") { "%02X".format(it) }}")
|
||||||
|
|
||||||
if (packet[4] == Opcodes.CONTROL_COMMAND) {
|
if (packet[4] == Opcodes.CONTROL_COMMAND) {
|
||||||
val controlCommand = ControlCommand.fromByteArray(packet)
|
val controlCommand = try {
|
||||||
|
ControlCommand.fromByteArray(packet)
|
||||||
|
} catch (e: Exception) {
|
||||||
|
Log.w(TAG, "Invalid control command: ${e.message}")
|
||||||
|
callback?.onUnknownPacketReceived(packet)
|
||||||
|
return false
|
||||||
|
}
|
||||||
Log.d(
|
Log.d(
|
||||||
TAG, "Control command: ${controlCommand.identifier.toHexString()} - ${
|
TAG, "Control command: ${controlCommand.identifier.toHexString()} - ${
|
||||||
controlCommand.value.joinToString(" ") { "%02X".format(it) }
|
controlCommand.value.joinToString(" ") { "%02X".format(it) }
|
||||||
|
|||||||
Reference in New Issue
Block a user