1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-11-24 10:56:50 +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) {
validateId(id, LefunConstants.CMD_ALARM);
if (params.limit() - params.position() < 2)
int paramsLength = params.limit() - params.position();
if (paramsLength < 2)
throwUnexpectedLength();
op = params.get();
index = params.get();
if (op == OP_GET) {
if (params.limit() - params.position() != 8)
if (paramsLength != 8)
throwUnexpectedLength();
enabled = params.get() == 1;
@ -147,7 +148,7 @@ public class AlarmCommand extends BaseCommand {
hour = params.get();
minute = params.get();
} else if (op == OP_SET) {
if (params.limit() - params.position() != 3)
if (paramsLength != 3)
throwUnexpectedLength();
setSuccess = params.get() == 1;

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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