1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-28 04:46:51 +01:00

Lefun: Fix command length checks, again

This commit is contained in:
Yukai Li 2020-10-04 21:31:37 -06:00 committed by Gitea
parent 61039f1749
commit 4992e4c15b
12 changed files with 48 additions and 36 deletions

View File

@ -131,13 +131,14 @@ public class AlarmCommand extends BaseCommand {
protected void deserializeParams(byte id, ByteBuffer params) { protected void deserializeParams(byte id, ByteBuffer params) {
validateId(id, LefunConstants.CMD_ALARM); validateId(id, LefunConstants.CMD_ALARM);
if (params.limit() - params.position() < 2) int paramsLength = params.limit() - params.position();
if (paramsLength < 2)
throwUnexpectedLength(); throwUnexpectedLength();
op = params.get(); op = params.get();
index = params.get(); index = params.get();
if (op == OP_GET) { if (op == OP_GET) {
if (params.limit() - params.position() != 8) if (paramsLength != 8)
throwUnexpectedLength(); throwUnexpectedLength();
enabled = params.get() == 1; enabled = params.get() == 1;
@ -147,7 +148,7 @@ public class AlarmCommand extends BaseCommand {
hour = params.get(); hour = params.get();
minute = params.get(); minute = params.get();
} else if (op == OP_SET) { } else if (op == OP_SET) {
if (params.limit() - params.position() != 3) if (paramsLength != 3)
throwUnexpectedLength(); throwUnexpectedLength();
setSuccess = params.get() == 1; setSuccess = params.get() == 1;

View File

@ -54,17 +54,18 @@ public class Cmd22Command extends BaseCommand {
protected void deserializeParams(byte id, ByteBuffer params) { protected void deserializeParams(byte id, ByteBuffer params) {
validateId(id, LefunConstants.CMD_UNKNOWN_22); validateId(id, LefunConstants.CMD_UNKNOWN_22);
if (params.limit() - params.position() < 1) int paramsLength = params.limit() - params.position();
if (paramsLength < 1)
throwUnexpectedLength(); throwUnexpectedLength();
op = params.get(); op = params.get();
if (op == OP_GET) { if (op == OP_GET) {
if (params.limit() - params.position() != 3) if (paramsLength != 3)
throwUnexpectedLength(); throwUnexpectedLength();
unknown = params.getShort(); unknown = params.getShort();
} else if (op == OP_SET) { } else if (op == OP_SET) {
if (params.limit() - params.position() != 2) if (paramsLength != 2)
throwUnexpectedLength(); throwUnexpectedLength();
setSuccess = params.get() == 1; setSuccess = params.get() == 1;

View File

@ -56,17 +56,18 @@ public class Cmd25Command extends BaseCommand {
protected void deserializeParams(byte id, ByteBuffer params) { protected void deserializeParams(byte id, ByteBuffer params) {
validateId(id, LefunConstants.CMD_UNKNOWN_25); validateId(id, LefunConstants.CMD_UNKNOWN_25);
if (params.limit() - params.position() < 1) int paramsLength = params.limit() - params.position();
if (paramsLength < 1)
throwUnexpectedLength(); throwUnexpectedLength();
op = params.get(); op = params.get();
if (op == OP_GET) { if (op == OP_GET) {
if (params.limit() - params.position() != 5) if (paramsLength != 5)
throwUnexpectedLength(); throwUnexpectedLength();
unknown = params.getInt(); unknown = params.getInt();
} else if (op == OP_SET) { } else if (op == OP_SET) {
if (params.limit() - params.position() != 2) if (paramsLength != 2)
throwUnexpectedLength(); throwUnexpectedLength();
setSuccess = params.get() == 1; setSuccess = params.get() == 1;

View File

@ -65,17 +65,18 @@ public class FeaturesCommand extends BaseCommand {
protected void deserializeParams(byte id, ByteBuffer params) { protected void deserializeParams(byte id, ByteBuffer params) {
validateId(id, LefunConstants.CMD_FEATURES); validateId(id, LefunConstants.CMD_FEATURES);
if (params.limit() - params.position() < 1) int paramsLength = params.limit() - params.position();
if (paramsLength < 1)
throwUnexpectedLength(); throwUnexpectedLength();
op = params.get(); op = params.get();
if (op == OP_GET) { if (op == OP_GET) {
if (params.limit() - params.position() != 3) if (paramsLength != 3)
throwUnexpectedLength(); throwUnexpectedLength();
features = params.getShort(); features = params.getShort();
} else if (op == OP_SET) { } else if (op == OP_SET) {
if (params.limit() - params.position() != 2) if (paramsLength != 2)
throwUnexpectedLength(); throwUnexpectedLength();
setSuccess = params.get() == 1; setSuccess = params.get() == 1;

View File

@ -66,7 +66,8 @@ public class GetPpgDataCommand extends BaseCommand {
protected void deserializeParams(byte id, ByteBuffer params) { protected void deserializeParams(byte id, ByteBuffer params) {
validateId(id, LefunConstants.CMD_PPG_DATA); validateId(id, LefunConstants.CMD_PPG_DATA);
if (params.limit() - params.position() < 9) int paramsLength = params.limit() - params.position();
if (paramsLength < 9)
throwUnexpectedLength(); throwUnexpectedLength();
ppgType = params.get(); ppgType = params.get();
@ -93,19 +94,19 @@ public class GetPpgDataCommand extends BaseCommand {
throw new IllegalArgumentException("Unknown PPG type"); throw new IllegalArgumentException("Unknown PPG type");
} }
if (params.limit() - params.position() < dataLength + 9) if (paramsLength < dataLength + 9)
throwUnexpectedLength(); throwUnexpectedLength();
ppgData = new byte[dataLength]; ppgData = new byte[dataLength];
params.get(ppgData); params.get(ppgData);
// Extended count/index // Extended count/index
if (params.limit() - params.position() == dataLength + 11) if (paramsLength == dataLength + 11)
{ {
totalRecords |= params.get() << 8; totalRecords |= params.get() << 8;
currentRecord |= params.get() << 8; currentRecord |= params.get() << 8;
} }
else if (params.limit() - params.position() > dataLength + 11) { else if (paramsLength > dataLength + 11) {
throwUnexpectedLength(); throwUnexpectedLength();
} }
} }

View File

@ -56,17 +56,18 @@ public class HydrationReminderIntervalCommand extends BaseCommand {
protected void deserializeParams(byte id, ByteBuffer params) { protected void deserializeParams(byte id, ByteBuffer params) {
validateId(id, LefunConstants.CMD_HYDRATION_REMINDER_INTERVAL); validateId(id, LefunConstants.CMD_HYDRATION_REMINDER_INTERVAL);
if (params.limit() - params.position() < 1) int paramsLength = params.limit() - params.position();
if (paramsLength < 1)
throwUnexpectedLength(); throwUnexpectedLength();
op = params.get(); op = params.get();
if (op == OP_GET) { if (op == OP_GET) {
if (params.limit() - params.position() != 2) if (paramsLength != 2)
throwUnexpectedLength(); throwUnexpectedLength();
hydrationReminderInterval = params.get(); hydrationReminderInterval = params.get();
} else if (op == OP_SET) { } else if (op == OP_SET) {
if (params.limit() - params.position() != 2) if (paramsLength != 2)
throwUnexpectedLength(); throwUnexpectedLength();
setSuccess = params.get() == 1; setSuccess = params.get() == 1;

View File

@ -38,7 +38,8 @@ public class PpgResultCommand extends BaseCommand {
protected void deserializeParams(byte id, ByteBuffer params) { protected void deserializeParams(byte id, ByteBuffer params) {
validateId(id, LefunConstants.CMD_PPG_RESULT); validateId(id, LefunConstants.CMD_PPG_RESULT);
if (params.limit() - params.position() < 1) int paramsLength = params.limit() - params.position();
if (paramsLength < 1)
throwUnexpectedLength(); throwUnexpectedLength();
ppgType = params.get(); ppgType = params.get();
@ -57,7 +58,7 @@ public class PpgResultCommand extends BaseCommand {
throw new IllegalArgumentException("Unknown PPG type"); throw new IllegalArgumentException("Unknown PPG type");
} }
if (params.limit() - params.position() != dataLength + 1) if (paramsLength != dataLength + 1)
throwUnexpectedLength(); throwUnexpectedLength();
ppgData = new byte[dataLength]; ppgData = new byte[dataLength];

View File

@ -94,12 +94,13 @@ public class ProfileCommand extends BaseCommand {
protected void deserializeParams(byte id, ByteBuffer params) { protected void deserializeParams(byte id, ByteBuffer params) {
validateId(id, LefunConstants.CMD_PROFILE); validateId(id, LefunConstants.CMD_PROFILE);
if (params.limit() - params.position() < 1) int paramsLength = params.limit() - params.position();
if (paramsLength < 1)
throwUnexpectedLength(); throwUnexpectedLength();
op = params.get(); op = params.get();
if (op == OP_GET) { if (op == OP_GET) {
if (params.limit() - params.position() != 5) if (paramsLength != 5)
throwUnexpectedLength(); throwUnexpectedLength();
gender = params.get(); gender = params.get();
@ -107,7 +108,7 @@ public class ProfileCommand extends BaseCommand {
weight = params.get(); weight = params.get();
age = params.get(); age = params.get();
} else if (op == OP_SET) { } else if (op == OP_SET) {
if (params.limit() - params.position() != 2) if (paramsLength != 2)
throwUnexpectedLength(); throwUnexpectedLength();
setSuccess = params.get() == 1; setSuccess = params.get() == 1;

View File

@ -56,17 +56,18 @@ public class SedentaryReminderIntervalCommand extends BaseCommand {
protected void deserializeParams(byte id, ByteBuffer params) { protected void deserializeParams(byte id, ByteBuffer params) {
validateId(id, LefunConstants.CMD_SEDENTARY_REMINDER_INTERVAL); validateId(id, LefunConstants.CMD_SEDENTARY_REMINDER_INTERVAL);
if (params.limit() - params.position() < 1) int paramsLength = params.limit() - params.position();
if (paramsLength < 1)
throwUnexpectedLength(); throwUnexpectedLength();
op = params.get(); op = params.get();
if (op == OP_GET) { if (op == OP_GET) {
if (params.limit() - params.position() != 2) if (paramsLength != 2)
throwUnexpectedLength(); throwUnexpectedLength();
sedentaryReminderInterval = params.get(); sedentaryReminderInterval = params.get();
} else if (op == OP_SET) { } else if (op == OP_SET) {
if (params.limit() - params.position() != 2) if (paramsLength != 2)
throwUnexpectedLength(); throwUnexpectedLength();
setSuccess = params.get() == 1; setSuccess = params.get() == 1;

View File

@ -83,19 +83,20 @@ public class SettingsCommand extends BaseCommand {
protected void deserializeParams(byte id, ByteBuffer params) { protected void deserializeParams(byte id, ByteBuffer params) {
validateId(id, LefunConstants.CMD_SETTINGS); validateId(id, LefunConstants.CMD_SETTINGS);
if (params.limit() - params.position() < 1) int paramsLength = params.limit() - params.position();
if (paramsLength < 1)
throwUnexpectedLength(); throwUnexpectedLength();
op = params.get(); op = params.get();
if (op == OP_GET) { if (op == OP_GET) {
if (params.limit() - params.position() != 4) if (paramsLength != 4)
throwUnexpectedLength(); throwUnexpectedLength();
option1 = params.get(); option1 = params.get();
amPmIndicator = params.get(); amPmIndicator = params.get();
measurementUnit = params.get(); measurementUnit = params.get();
} else if (op == OP_SET) { } else if (op == OP_SET) {
if (params.limit() - params.position() != 2) if (paramsLength != 2)
throwUnexpectedLength(); throwUnexpectedLength();
setSuccess = params.get() == 1; setSuccess = params.get() == 1;

View File

@ -109,12 +109,13 @@ public class TimeCommand extends BaseCommand {
protected void deserializeParams(byte id, ByteBuffer params) { protected void deserializeParams(byte id, ByteBuffer params) {
validateId(id, LefunConstants.CMD_TIME); validateId(id, LefunConstants.CMD_TIME);
if (params.limit() - params.position() < 1) int paramsLength = params.limit() - params.position();
if (paramsLength < 1)
throwUnexpectedLength(); throwUnexpectedLength();
op = params.get(); op = params.get();
if (op == OP_GET) { if (op == OP_GET) {
if (params.limit() - params.position() != 7) if (paramsLength != 7)
throwUnexpectedLength(); throwUnexpectedLength();
year = params.get(); year = params.get();
@ -124,7 +125,7 @@ public class TimeCommand extends BaseCommand {
minute = params.get(); minute = params.get();
second = params.get(); second = params.get();
} else if (op == OP_SET) { } else if (op == OP_SET) {
if (params.limit() - params.position() != 2) if (paramsLength != 2)
throwUnexpectedLength(); throwUnexpectedLength();
setSuccess = params.get() == 1; setSuccess = params.get() == 1;

View File

@ -61,17 +61,18 @@ public class UiPagesCommand extends BaseCommand {
protected void deserializeParams(byte id, ByteBuffer params) { protected void deserializeParams(byte id, ByteBuffer params) {
validateId(id, LefunConstants.CMD_UI_PAGES); validateId(id, LefunConstants.CMD_UI_PAGES);
if (params.limit() - params.position() < 1) int paramsLength = params.limit() - params.position();
if (paramsLength < 1)
throwUnexpectedLength(); throwUnexpectedLength();
op = params.get(); op = params.get();
if (op == OP_GET) { if (op == OP_GET) {
if (params.limit() - params.position() != 3) if (paramsLength != 3)
throwUnexpectedLength(); throwUnexpectedLength();
pages = params.getShort(); pages = params.getShort();
} else if (op == OP_SET) { } else if (op == OP_SET) {
if (params.limit() - params.position() != 2) if (paramsLength != 2)
throwUnexpectedLength(); throwUnexpectedLength();
setSuccess = params.get() == 1; setSuccess = params.get() == 1;