Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LE Audio shell strtoX fixes #54165

Merged
merged 19 commits into from
Feb 20, 2023

Conversation

Thalley
Copy link
Collaborator

@Thalley Thalley commented Jan 27, 2023

Ensures that all strtoX calls are handled correctly, and moves to use the shell_strtoX functions

Fixes most of the bugs reported in #54101

Fixes #54101

int result;
int step = strtol(argv[1], NULL, 0);
unsigned long step;
int result = 0;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should use the same name for error in all functions, err.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I disagree; I think it's better to just use the already defined value to keep the number of lines changed to a minimum.


if (step > UINT8_MAX || step == 0) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks like you changed logic, no check for step == 0 ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch - Thanks!

return -ENOEXEC;
}

if (location > UINT32_MAX) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the logic is changed, it was UINT16_MAX.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, perhaps that should have been a separate commit. Should I add another commit for that?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a commit to fix this in the PR

count = strtoul(argv[1], NULL, 10);
count = shell_strtoul(argv[5], 0, &ret);
if (ret != 0) {
shell_error(sh, "Could not parse count: %d", ret);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better use the same variable for error, err.

unsigned long order;
int err = 0;

order = shell_strtol(argv[1], 0, &err);
Copy link
Collaborator

@finikorg finikorg Jan 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shell_strtoul ?


if (start_scan != 0 && start_scan != 1) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shell_strtobool has different logic, at the end it converts unsigned long to bool, so any number > 0 is true, right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right. I was unsure about whether I should just use shell_strtobool directly, or actually check if the value was > 1, but decided on the former, as just using shell_strtobool would be more consistent with other shell commands that use that.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, looks reasonable.

@Thalley Thalley requested a review from finikorg January 31, 2023 09:00
finikorg
finikorg previously approved these changes Jan 31, 2023
Copy link
Collaborator

@finikorg finikorg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, although I would use the same error code err instead of result, etc.

@Thalley
Copy link
Collaborator Author

Thalley commented Jan 31, 2023

LGTM, although I would use the same error code err instead of result, etc.

Making all the audio shell implementations more consistent can be postponed to a separate PR :)

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

Signed-off-by: Emil Gydesen <emil.gydesen@nordicsemi.no>
Verify all values returned by calls to strtoX and convert
to the shell_strtoX functions.

Signed-off-by: Emil Gydesen <emil.gydesen@nordicsemi.no>
Verify all values returned by calls to strtoX and convert
to the shell_strtoX functions.

Signed-off-by: Emil Gydesen <emil.gydesen@nordicsemi.no>
Verify all values returned by calls to strtoX and convert
to the shell_strtoX functions.

Signed-off-by: Emil Gydesen <emil.gydesen@nordicsemi.no>
Verify all values returned by calls to strtoX and convert
to the shell_strtoX functions.

Signed-off-by: Emil Gydesen <emil.gydesen@nordicsemi.no>
Verify all values returned by calls to strtoX and convert
to the shell_strtoX functions.

Signed-off-by: Emil Gydesen <emil.gydesen@nordicsemi.no>
@Thalley
Copy link
Collaborator Author

Thalley commented Feb 6, 2023

Found a few more with incorrect type

subsys/bluetooth/shell/mcc.c Outdated Show resolved Hide resolved
subsys/bluetooth/shell/mcc.c Outdated Show resolved Hide resolved
subsys/bluetooth/shell/mcc.c Outdated Show resolved Hide resolved
unsigned long order;
int result = 0;

order = shell_strtol(argv[1], 0, &result);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
order = shell_strtol(argv[1], 0, &result);
order = shell_strtoul(argv[1], 0, &result);

subsys/bluetooth/shell/mcc.c Outdated Show resolved Hide resolved
subsys/bluetooth/shell/tbs.c Outdated Show resolved Hide resolved
subsys/bluetooth/shell/tbs.c Outdated Show resolved Hide resolved
inst_index = strtol(argv[1], NULL, 0);
if (inst_index < 0 || inst_index > UINT8_MAX) {
shell_error(sh, "Invalid index");
inst_index = shell_strtoul(argv[1], 0, &result);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a comment/TODO that this can be pulled out as a function since this code is repeated many times?

return -ENOEXEC;
}

if (offset > BT_VOCS_MAX_OFFSET || offset < BT_VOCS_MIN_OFFSET) {
shell_error(sh, "Offset shall be %d-%d, was %d",
shell_error(sh, "Offset shall be %d-%d, was %ld",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missed opportunity for using IN_RANGE

if (gain > INT8_MAX || gain < INT8_MIN) {
shell_error(sh, "Offset shall be %d-%d, was %d",
shell_error(sh, "Gain shall be %d-%d, was %d",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use IN_RANGE?

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

Signed-off-by: Emil Gydesen <emil.gydesen@nordicsemi.no>
Verify all values returned by calls to strtoX and convert
to the shell_strtoX functions.

Signed-off-by: Emil Gydesen <emil.gydesen@nordicsemi.no>
Verify all values returned by calls to strtoX and convert
to the shell_strtoX functions.

Signed-off-by: Emil Gydesen <emil.gydesen@nordicsemi.no>
Verify all values returned by calls to strtoX and convert
to the shell_strtoX functions.

Signed-off-by: Emil Gydesen <emil.gydesen@nordicsemi.no>
Verify all values returned by calls to strtoX and convert
to the shell_strtoX functions.

Signed-off-by: Emil Gydesen <emil.gydesen@nordicsemi.no>
Verify all values returned by calls to strtoX and convert
to the shell_strtoX functions.

Signed-off-by: Emil Gydesen <emil.gydesen@nordicsemi.no>
Verify all values returned by calls to strtoX and convert
to the shell_strtoX functions.

Signed-off-by: Emil Gydesen <emil.gydesen@nordicsemi.no>
The location value is a uint32_t, and not uint16_t.

Signed-off-by: Emil Gydesen <emil.gydesen@nordicsemi.no>
Verify all values returned by calls to strtoX and convert
to the shell_strtoX functions.

Signed-off-by: Emil Gydesen <emil.gydesen@nordicsemi.no>
Verify all values returned by calls to strtoX and convert
to the shell_strtoX functions.

Signed-off-by: Emil Gydesen <emil.gydesen@nordicsemi.no>
@Thalley Thalley assigned Thalley and unassigned jhedberg Feb 13, 2023
@carlescufi carlescufi merged commit 9756aea into zephyrproject-rtos:main Feb 20, 2023
Bluetooth LE Audio automation moved this from In Review to Done Feb 20, 2023
@Thalley Thalley deleted the audio_shell_strtoX_fixes branch March 8, 2023 12:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: Bluetooth Audio area: Bluetooth bug The issue is a bug, or the PR is fixing a bug
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

bluetooth: shell: Lots of checks of type (unsigned < 0) which is bogus
7 participants