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

cpu/sam0_common: fix potential undefined result with sercom_id #12576

Merged
merged 1 commit into from
Oct 27, 2019

Conversation

aabadie
Copy link
Contributor

@aabadie aabadie commented Oct 25, 2019

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

@aabadie aabadie added Platform: ARM Platform: This PR/issue effects ARM-based platforms Type: cleanup The issue proposes a clean-up / The PR cleans-up parts of the codebase / documentation CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR CI: run tests If set, CI server will run tests on hardware for the labeled PR labels Oct 25, 2019
return -1;
assert(ret < SERCOM_INST_NUM);

return ret;
Copy link
Contributor

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);
Copy link
Contributor

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;
Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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

@benpicco
Copy link
Contributor

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
@aabadie aabadie force-pushed the pr/cpu/sam0_scan_build_fix branch from c663171 to 1c41618 Compare October 27, 2019 11:03
@aabadie
Copy link
Contributor Author

aabadie commented Oct 27, 2019

please squash.

Done!

Copy link
Contributor

@benpicco benpicco left a 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.

@benpicco benpicco merged commit 7072696 into RIOT-OS:master Oct 27, 2019
@aabadie aabadie deleted the pr/cpu/sam0_scan_build_fix branch December 6, 2019 20:35
@fjmolinas fjmolinas added this to the Release 2020.01 milestone Dec 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CI: ready for build If set, CI server will compile all applications for all available boards for the labeled PR CI: run tests If set, CI server will run tests on hardware for the labeled PR Platform: ARM Platform: This PR/issue effects ARM-based platforms Type: cleanup The issue proposes a clean-up / The PR cleans-up parts of the codebase / documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants