-
Notifications
You must be signed in to change notification settings - Fork 2k
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
cpu/sam0_common: fix potential undefined result with sercom_id #12576
Conversation
return -1; | ||
assert(ret < SERCOM_INST_NUM); | ||
|
||
return ret; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could just return SERCOM_INST_NUM;
here.
#endif | ||
|
||
return -1; | ||
assert(ret < SERCOM_INST_NUM); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
or assert(false)
if you don't introduce a ret
variable.
@@ -342,40 +342,51 @@ void gpio_init_mux(gpio_t pin, gpio_mux_t mux); | |||
*/ | |||
static inline int sercom_id(const void *sercom) | |||
{ | |||
int ret = SERCOM_INST_NUM; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you don't need this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right. I also added the assert(false);
before returning. Not that there's an increase of code size by 48 bytes with this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would assume the increase in size is only with DEVHELP = 1
Looks good, please squash. |
Scan-build detected that sercom_id could return -1 and the value of this function is affected to uint8_t variables. Since these variables are used for shitfing bit in registers, this could lead to undefined behavior
c663171
to
1c41618
Compare
Done! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adds an assert()
to the failure case that should never be reached.
No functional changes.
Contribution description
As reported in #11852 there are tons of scan-build warning when building an application for a sam0 based boards.
Scan-build is detecting that sercom_id could return -1 and the value of this function is affected to uint8_t variables. Since these variables are used for shitfing bit in registers, this could lead to undefined results.
This PR is fixing that by using the SERCOM_INST_NUM macro defined in the CMSIS instead of -1. There's an assert added to check that the value returned is not SERCOM_INST_NUM.
This change also adds 16 extra bytes in ROM.
Testing procedure
Run
BOARD=samr21-xpro TOOLCHAIN=llvm make -C examples/hello-world/ scan-build
on master it raises tons of warning, with this PR the warning are gone
Trigger CI: run tests on Murdock and verify they work (on samr21-xpro which is sam0 based).
Issues/PRs references
Tick one item in #11852