Add check for packet of incorrect length (#629)

Co-authored-by: Matt <matthew.morley.ca@gmail.com>
This commit is contained in:
Drew Williams
2022-12-08 19:22:31 -05:00
committed by GitHub
parent ec7bef7a4b
commit 643db9c435
7 changed files with 860 additions and 6 deletions

View File

@@ -129,6 +129,9 @@ public class Packet {
* @return A decoded byte from the packet.
*/
public byte decodeByte() {
if (packetData.length < readPos) {
return '\0';
}
return packetData[readPos++];
}
@@ -138,6 +141,9 @@ public class Packet {
* @return A decoded int from the packet.
*/
public int decodeInt() {
if (packetData.length < readPos + 3) {
return 0;
}
return (0xff & packetData[readPos++]) << 24
| (0xff & packetData[readPos++]) << 16
| (0xff & packetData[readPos++]) << 8
@@ -150,6 +156,9 @@ public class Packet {
* @return A decoded double from the packet.
*/
public double decodeDouble() {
if (packetData.length < (readPos + 7)) {
return 0;
}
long data =
(long) (0xff & packetData[readPos++]) << 56
| (long) (0xff & packetData[readPos++]) << 48
@@ -168,6 +177,9 @@ public class Packet {
* @return A decoded boolean from the packet.
*/
public boolean decodeBoolean() {
if (packetData.length < readPos) {
return false;
}
return packetData[readPos++] == 1;
}
}