Skip to content

Commit c1d0cfe

Browse files
committed
Only process receive if packet is valid length
In case there's an error in the API recv function this will keep the library data valid and avoid the callback.
1 parent e6d0448 commit c1d0cfe

1 file changed

Lines changed: 18 additions & 15 deletions

File tree

src/XInput.cpp

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -348,21 +348,24 @@ int XInputGamepad::receive() {
348348
uint8_t rx[8];
349349
int bytesRecv = XInputUSB::recv(rx, sizeof(rx));
350350

351-
const uint8_t PacketType = rx[0];
352-
353-
// Rumble Packet
354-
if(PacketType == (uint8_t) XInputReceiveType::Rumble) {
355-
rumble[RumbleLeft.bufferIndex] = rx[RumbleLeft.rxIndex]; // Big weight (Left grip)
356-
rumble[RumbleRight.bufferIndex] = rx[RumbleRight.rxIndex]; // Small weight (Right grip)
357-
}
358-
// LED Packet
359-
else if (PacketType == (uint8_t) XInputReceiveType::LEDs) {
360-
parseLED(rx[2]);
361-
}
362-
363-
// User-defined receive callback
364-
if (recvCallback != nullptr) {
365-
recvCallback(PacketType);
351+
// Only process if received 3 or more bytes (min valid packet size)
352+
if (bytesRecv >= 3) {
353+
const uint8_t PacketType = rx[0];
354+
355+
// Rumble Packet
356+
if (PacketType == (uint8_t)XInputReceiveType::Rumble) {
357+
rumble[RumbleLeft.bufferIndex] = rx[RumbleLeft.rxIndex]; // Big weight (Left grip)
358+
rumble[RumbleRight.bufferIndex] = rx[RumbleRight.rxIndex]; // Small weight (Right grip)
359+
}
360+
// LED Packet
361+
else if (PacketType == (uint8_t)XInputReceiveType::LEDs) {
362+
parseLED(rx[2]);
363+
}
364+
365+
// User-defined receive callback
366+
if (recvCallback != nullptr) {
367+
recvCallback(PacketType);
368+
}
366369
}
367370

368371
return bytesRecv;

0 commit comments

Comments
 (0)