Bluetooth: Audio: Ensure strtoX correctness in vcp_vol_ctlr.c

Verify all values returned by calls to strtoX and convert
to the shell_strtoX functions.

Signed-off-by: Emil Gydesen <emil.gydesen@nordicsemi.no>
This commit is contained in:
Emil Gydesen 2023-01-29 12:30:45 +01:00 committed by Carles Cufí
parent 668ed75a22
commit fe1d5a8177

View file

@ -464,16 +464,24 @@ static int cmd_vcp_vol_ctlr_volume_set(const struct shell *sh, size_t argc,
char **argv)
{
int result;
int volume = strtol(argv[1], NULL, 0);
unsigned long volume;
int result = 0;
if (default_conn == NULL) {
shell_error(sh, "Not connected");
return -ENOEXEC;
}
volume = shell_strtoul(argv[1], 0, &result);
if (result != 0) {
shell_error(sh, "Failed to parse volume: %d", result);
return -ENOEXEC;
}
if (volume > UINT8_MAX) {
shell_error(sh, "Volume shall be 0-255, was %d", volume);
shell_error(sh, "Volume shall be 0-255, was %lu", volume);
return -ENOEXEC;
}
@ -525,17 +533,25 @@ static int cmd_vcp_vol_ctlr_mute(const struct shell *sh, size_t argc,
static int cmd_vcp_vol_ctlr_vocs_state_get(const struct shell *sh, size_t argc,
char **argv)
{
int result;
int index = strtol(argv[1], NULL, 0);
unsigned long index;
int result = 0;
if (default_conn == NULL) {
shell_error(sh, "Not connected");
return -ENOEXEC;
}
index = shell_strtoul(argv[1], 0, &result);
if (result != 0) {
shell_error(sh, "Failed to parse index: %d", result);
return -ENOEXEC;
}
if (index >= vcp_included.vocs_cnt) {
shell_error(sh, "Index shall be less than %u, was %u",
shell_error(sh, "Index shall be less than %u, was %lu",
vcp_included.vocs_cnt, index);
return -ENOEXEC;
}
@ -550,17 +566,25 @@ static int cmd_vcp_vol_ctlr_vocs_state_get(const struct shell *sh, size_t argc,
static int cmd_vcp_vol_ctlr_vocs_location_get(const struct shell *sh,
size_t argc, char **argv)
{
int result;
int index = strtol(argv[1], NULL, 0);
unsigned long index;
int result = 0;
if (default_conn == NULL) {
shell_error(sh, "Not connected");
return -ENOEXEC;
}
index = shell_strtoul(argv[1], 0, &result);
if (result != 0) {
shell_error(sh, "Failed to parse index: %d", result);
return -ENOEXEC;
}
if (index >= vcp_included.vocs_cnt) {
shell_error(sh, "Index shall be less than %u, was %u",
shell_error(sh, "Index shall be less than %u, was %lu",
vcp_included.vocs_cnt, index);
return -ENOEXEC;
}
@ -575,30 +599,43 @@ static int cmd_vcp_vol_ctlr_vocs_location_get(const struct shell *sh,
static int cmd_vcp_vol_ctlr_vocs_location_set(const struct shell *sh,
size_t argc, char **argv)
{
int result;
int index = strtol(argv[1], NULL, 0);
int location = strtol(argv[2], NULL, 0);
unsigned long location;
unsigned long index;
int result = 0;
if (default_conn == NULL) {
shell_error(sh, "Not connected");
return -ENOEXEC;
}
index = shell_strtoul(argv[1], 0, &result);
if (result != 0) {
shell_error(sh, "Failed to parse index: %d", result);
return -ENOEXEC;
}
if (index >= vcp_included.vocs_cnt) {
shell_error(sh, "Index shall be less than %u, was %u",
shell_error(sh, "Index shall be less than %u, was %lu",
vcp_included.vocs_cnt, index);
return -ENOEXEC;
}
if (location > UINT32_MAX || location < 0) {
shell_error(sh, "Invalid location (%u-%u), was %u",
0, UINT32_MAX, location);
location = shell_strtoul(argv[2], 0, &result);
if (result != 0) {
shell_error(sh, "Failed to parse location: %d", result);
return -ENOEXEC;
}
if (location > UINT32_MAX) {
shell_error(sh, "Invalid location %lu", location);
return -ENOEXEC;
}
result = bt_vocs_location_set(vcp_included.vocs[index],
location);
result = bt_vocs_location_set(vcp_included.vocs[index], location);
if (result != 0) {
shell_print(sh, "Fail: %d", result);
}
@ -609,23 +646,38 @@ static int cmd_vcp_vol_ctlr_vocs_location_set(const struct shell *sh,
static int cmd_vcp_vol_ctlr_vocs_offset_set(const struct shell *sh,
size_t argc, char **argv)
{
int result;
int index = strtol(argv[1], NULL, 0);
int offset = strtol(argv[2], NULL, 0);
unsigned long index;
int result = 0;
long offset;
if (default_conn == NULL) {
shell_error(sh, "Not connected");
return -ENOEXEC;
}
if (index >= vcp_included.vocs_cnt) {
shell_error(sh, "Index shall be less than %u, was %u",
vcp_included.vocs_cnt, index);
index = shell_strtoul(argv[1], 0, &result);
if (result != 0) {
shell_error(sh, "Failed to parse index: %d", result);
return -ENOEXEC;
}
if (offset > BT_VOCS_MAX_OFFSET || offset < BT_VOCS_MIN_OFFSET) {
shell_error(sh, "Offset shall be %d-%d, was %d",
if (index >= vcp_included.vocs_cnt) {
shell_error(sh, "Index shall be less than %u, was %lu",
vcp_included.vocs_cnt, index);
return -ENOEXEC;
}
offset = shell_strtol(argv[2], 0, &result);
if (result != 0) {
shell_error(sh, "Failed to parse offset: %d", result);
return -ENOEXEC;
}
if (!IN_RANGE(offset, BT_VOCS_MIN_OFFSET, BT_VOCS_MAX_OFFSET)) {
shell_error(sh, "Offset shall be %d-%d, was %ld",
BT_VOCS_MIN_OFFSET, BT_VOCS_MAX_OFFSET, offset);
return -ENOEXEC;
}
@ -642,17 +694,25 @@ static int cmd_vcp_vol_ctlr_vocs_offset_set(const struct shell *sh,
static int cmd_vcp_vol_ctlr_vocs_output_description_get(const struct shell *sh,
size_t argc, char **argv)
{
int result;
int index = strtol(argv[1], NULL, 0);
unsigned long index;
int result = 0;
if (default_conn == NULL) {
shell_error(sh, "Not connected");
return -ENOEXEC;
}
index = shell_strtoul(argv[1], 0, &result);
if (result != 0) {
shell_error(sh, "Failed to parse index: %d", result);
return -ENOEXEC;
}
if (index >= vcp_included.vocs_cnt) {
shell_error(sh, "Index shall be less than %u, was %u",
shell_error(sh, "Index shall be less than %u, was %lu",
vcp_included.vocs_cnt, index);
return -ENOEXEC;
}
@ -667,23 +727,29 @@ static int cmd_vcp_vol_ctlr_vocs_output_description_get(const struct shell *sh,
static int cmd_vcp_vol_ctlr_vocs_output_description_set(const struct shell *sh,
size_t argc, char **argv)
{
int result;
int index = strtol(argv[1], NULL, 0);
char *description = argv[2];
unsigned long index;
int result = 0;
if (default_conn == NULL) {
shell_error(sh, "Not connected");
return -ENOEXEC;
}
if (index >= vcp_included.vocs_cnt) {
shell_error(sh, "Index shall be less than %u, was %u",
vcp_included.vocs_cnt, index);
index = shell_strtoul(argv[1], 0, &result);
if (result != 0) {
shell_error(sh, "Failed to parse index: %d", result);
return -ENOEXEC;
}
result = bt_vocs_description_set(vcp_included.vocs[index],
description);
if (index >= vcp_included.vocs_cnt) {
shell_error(sh, "Index shall be less than %u, was %lu",
vcp_included.vocs_cnt, index);
return -ENOEXEC;
}
result = bt_vocs_description_set(vcp_included.vocs[index], argv[2]);
if (result != 0) {
shell_print(sh, "Fail: %d", result);
}
@ -694,17 +760,25 @@ static int cmd_vcp_vol_ctlr_vocs_output_description_set(const struct shell *sh,
static int cmd_vcp_vol_ctlr_aics_input_state_get(const struct shell *sh,
size_t argc, char **argv)
{
int result;
int index = strtol(argv[1], NULL, 0);
unsigned long index;
int result = 0;
if (default_conn == NULL) {
shell_error(sh, "Not connected");
return -ENOEXEC;
}
index = shell_strtoul(argv[1], 0, &result);
if (result != 0) {
shell_error(sh, "Failed to parse index: %d", result);
return -ENOEXEC;
}
if (index >= vcp_included.aics_cnt) {
shell_error(sh, "Index shall be less than %u, was %u",
vcp_included.aics_cnt, index);
shell_error(sh, "Index shall be less than %u, was %lu",
vcp_included.vocs_cnt, index);
return -ENOEXEC;
}
@ -719,17 +793,25 @@ static int cmd_vcp_vol_ctlr_aics_input_state_get(const struct shell *sh,
static int cmd_vcp_vol_ctlr_aics_gain_setting_get(const struct shell *sh,
size_t argc, char **argv)
{
int result;
int index = strtol(argv[1], NULL, 0);
unsigned long index;
int result = 0;
if (default_conn == NULL) {
shell_error(sh, "Not connected");
return -ENOEXEC;
}
index = shell_strtoul(argv[1], 0, &result);
if (result != 0) {
shell_error(sh, "Failed to parse index: %d", result);
return -ENOEXEC;
}
if (index >= vcp_included.aics_cnt) {
shell_error(sh, "Index shall be less than %u, was %u",
vcp_included.aics_cnt, index);
shell_error(sh, "Index shall be less than %u, was %lu",
vcp_included.vocs_cnt, index);
return -ENOEXEC;
}
@ -744,17 +826,25 @@ static int cmd_vcp_vol_ctlr_aics_gain_setting_get(const struct shell *sh,
static int cmd_vcp_vol_ctlr_aics_input_type_get(const struct shell *sh,
size_t argc, char **argv)
{
int result;
int index = strtol(argv[1], NULL, 0);
unsigned long index;
int result = 0;
if (default_conn == NULL) {
shell_error(sh, "Not connected");
return -ENOEXEC;
}
index = shell_strtoul(argv[1], 0, &result);
if (result != 0) {
shell_error(sh, "Failed to parse index: %d", result);
return -ENOEXEC;
}
if (index >= vcp_included.aics_cnt) {
shell_error(sh, "Index shall be less than %u, was %u",
vcp_included.aics_cnt, index);
shell_error(sh, "Index shall be less than %u, was %lu",
vcp_included.vocs_cnt, index);
return -ENOEXEC;
}
@ -769,17 +859,25 @@ static int cmd_vcp_vol_ctlr_aics_input_type_get(const struct shell *sh,
static int cmd_vcp_vol_ctlr_aics_input_status_get(const struct shell *sh,
size_t argc, char **argv)
{
int result;
int index = strtol(argv[1], NULL, 0);
unsigned long index;
int result = 0;
if (default_conn == NULL) {
shell_error(sh, "Not connected");
return -ENOEXEC;
}
index = shell_strtoul(argv[1], 0, &result);
if (result != 0) {
shell_error(sh, "Failed to parse index: %d", result);
return -ENOEXEC;
}
if (index >= vcp_included.aics_cnt) {
shell_error(sh, "Index shall be less than %u, was %u",
vcp_included.aics_cnt, index);
shell_error(sh, "Index shall be less than %u, was %lu",
vcp_included.vocs_cnt, index);
return -ENOEXEC;
}
@ -794,17 +892,25 @@ static int cmd_vcp_vol_ctlr_aics_input_status_get(const struct shell *sh,
static int cmd_vcp_vol_ctlr_aics_input_unmute(const struct shell *sh,
size_t argc, char **argv)
{
int result;
int index = strtol(argv[1], NULL, 0);
unsigned long index;
int result = 0;
if (default_conn == NULL) {
shell_error(sh, "Not connected");
return -ENOEXEC;
}
index = shell_strtoul(argv[1], 0, &result);
if (result != 0) {
shell_error(sh, "Failed to parse index: %d", result);
return -ENOEXEC;
}
if (index >= vcp_included.aics_cnt) {
shell_error(sh, "Index shall be less than %u, was %u",
vcp_included.aics_cnt, index);
shell_error(sh, "Index shall be less than %u, was %lu",
vcp_included.vocs_cnt, index);
return -ENOEXEC;
}
@ -819,17 +925,25 @@ static int cmd_vcp_vol_ctlr_aics_input_unmute(const struct shell *sh,
static int cmd_vcp_vol_ctlr_aics_input_mute(const struct shell *sh,
size_t argc, char **argv)
{
int result;
int index = strtol(argv[1], NULL, 0);
unsigned long index;
int result = 0;
if (default_conn == NULL) {
shell_error(sh, "Not connected");
return -ENOEXEC;
}
index = shell_strtoul(argv[1], 0, &result);
if (result != 0) {
shell_error(sh, "Failed to parse index: %d", result);
return -ENOEXEC;
}
if (index >= vcp_included.aics_cnt) {
shell_error(sh, "Index shall be less than %u, was %u",
vcp_included.aics_cnt, index);
shell_error(sh, "Index shall be less than %u, was %lu",
vcp_included.vocs_cnt, index);
return -ENOEXEC;
}
@ -844,17 +958,25 @@ static int cmd_vcp_vol_ctlr_aics_input_mute(const struct shell *sh,
static int cmd_vcp_vol_ctlr_aics_manual_input_gain_set(const struct shell *sh,
size_t argc, char **argv)
{
int result;
int index = strtol(argv[1], NULL, 0);
unsigned long index;
int result = 0;
if (default_conn == NULL) {
shell_error(sh, "Not connected");
return -ENOEXEC;
}
index = shell_strtoul(argv[1], 0, &result);
if (result != 0) {
shell_error(sh, "Failed to parse index: %d", result);
return -ENOEXEC;
}
if (index >= vcp_included.aics_cnt) {
shell_error(sh, "Index shall be less than %u, was %u",
vcp_included.aics_cnt, index);
shell_error(sh, "Index shall be less than %u, was %lu",
vcp_included.vocs_cnt, index);
return -ENOEXEC;
}
@ -869,17 +991,25 @@ static int cmd_vcp_vol_ctlr_aics_manual_input_gain_set(const struct shell *sh,
static int cmd_vcp_vol_ctlr_aics_auto_input_gain_set(const struct shell *sh,
size_t argc, char **argv)
{
int result;
int index = strtol(argv[1], NULL, 0);
unsigned long index;
int result = 0;
if (default_conn == NULL) {
shell_error(sh, "Not connected");
return -ENOEXEC;
}
index = shell_strtoul(argv[1], 0, &result);
if (result != 0) {
shell_error(sh, "Failed to parse index: %d", result);
return -ENOEXEC;
}
if (index >= vcp_included.aics_cnt) {
shell_error(sh, "Index shall be less than %u, was %u",
vcp_included.aics_cnt, index);
shell_error(sh, "Index shall be less than %u, was %lu",
vcp_included.vocs_cnt, index);
return -ENOEXEC;
}
@ -894,23 +1024,35 @@ static int cmd_vcp_vol_ctlr_aics_auto_input_gain_set(const struct shell *sh,
static int cmd_vcp_vol_ctlr_aics_gain_set(const struct shell *sh, size_t argc,
char **argv)
{
int result;
int index = strtol(argv[1], NULL, 0);
int gain = strtol(argv[2], NULL, 0);
unsigned long index;
int result = 0;
long gain;
if (default_conn == NULL) {
shell_error(sh, "Not connected");
return -ENOEXEC;
}
index = shell_strtoul(argv[1], 0, &result);
if (result != 0) {
shell_error(sh, "Could not parse index: %d", result);
return -ENOEXEC;
}
if (index >= vcp_included.aics_cnt) {
shell_error(sh, "Index shall be less than %u, was %u",
vcp_included.aics_cnt, index);
return -ENOEXEC;
}
if (gain > INT8_MAX || gain < INT8_MIN) {
shell_error(sh, "Offset shall be %d-%d, was %d",
gain = shell_strtol(argv[2], 0, &result);
if (result != 0) {
shell_error(sh, "Could not parse gain: %d", result);
return -ENOEXEC;
}
if (!IN_RANGE(gain, INT8_MIN, INT8_MAX)) {
shell_error(sh, "Gain shall be %d-%d, was %d",
INT8_MIN, INT8_MAX, gain);
return -ENOEXEC;
}
@ -926,17 +1068,25 @@ static int cmd_vcp_vol_ctlr_aics_gain_set(const struct shell *sh, size_t argc,
static int cmd_vcp_vol_ctlr_aics_input_description_get(const struct shell *sh,
size_t argc, char **argv)
{
int result;
int index = strtol(argv[1], NULL, 0);
unsigned long index;
int result = 0;
if (default_conn == NULL) {
shell_error(sh, "Not connected");
return -ENOEXEC;
}
index = shell_strtoul(argv[1], 0, &result);
if (result != 0) {
shell_error(sh, "Failed to parse index: %d", result);
return -ENOEXEC;
}
if (index >= vcp_included.aics_cnt) {
shell_error(sh, "Index shall be less than %u, was %u",
vcp_included.aics_cnt, index);
shell_error(sh, "Index shall be less than %u, was %lu",
vcp_included.vocs_cnt, index);
return -ENOEXEC;
}
@ -951,23 +1101,29 @@ static int cmd_vcp_vol_ctlr_aics_input_description_get(const struct shell *sh,
static int cmd_vcp_vol_ctlr_aics_input_description_set(const struct shell *sh,
size_t argc, char **argv)
{
int result;
int index = strtol(argv[1], NULL, 0);
char *description = argv[2];
unsigned long index;
int result = 0;
if (default_conn == NULL) {
shell_error(sh, "Not connected");
return -ENOEXEC;
}
if (index >= vcp_included.aics_cnt) {
shell_error(sh, "Index shall be less than %u, was %u",
vcp_included.aics_cnt, index);
index = shell_strtoul(argv[1], 0, &result);
if (result != 0) {
shell_error(sh, "Failed to parse index: %d", result);
return -ENOEXEC;
}
result = bt_aics_description_set(vcp_included.aics[index],
description);
if (index >= vcp_included.aics_cnt) {
shell_error(sh, "Index shall be less than %u, was %lu",
vcp_included.vocs_cnt, index);
return -ENOEXEC;
}
result = bt_aics_description_set(vcp_included.aics[index], argv[2]);
if (result != 0) {
shell_print(sh, "Fail: %d", result);
}