Skip to content

Fix Coverity issue: Initialize FlashIAP non-static member in constructor #11494

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

Merged
merged 1 commit into from
Sep 24, 2019

Conversation

hugueskamba
Copy link
Collaborator

Description

Issue fixed:
FlashIAP.cpp:52 Non-static class member field _flash.dummy is not initialized in this constructor nor in any functions that it calls.

Pull request type

[X] Fix
[ ] Refactor
[ ] Target update
[ ] Functionality change
[ ] Docs update
[ ] Test update
[ ] Breaking change

Reviewers

Release Notes

@ciarmcom ciarmcom requested review from a team September 16, 2019 19:00
@ciarmcom
Copy link
Member

@hugueskamba, thank you for your changes.
@ARMmbed/mbed-os-core @ARMmbed/mbed-os-maintainers please review.

@kjbracey
Copy link
Contributor

To some extent, addressing some of these "uninitialized" warnings may add code size without actually changing anything - structures like that don't need to be initialised.

So I'd be interested in making changes at the same time to potentially trim out some code to balance it. Here you could remove the empty destructor, and make the constructor inline by putting it in the class body (it could also be constexpr, given that the real init happens later).

@hugueskamba
Copy link
Collaborator Author

To some extent, addressing some of these "uninitialized" warnings may add code size without actually changing anything - structures like that don't need to be initialised.

So I'd be interested in making changes at the same time to potentially trim out some code to balance it. Here you could remove the empty destructor, and make the constructor inline by putting it in the class body (it could also be constexpr, given that the real init happens later).

I get the following error if constexpr is used.

./mbed-os/drivers/FlashIAP.h:66:15: note: non-constexpr constructor 'NonCopyable' cannot be used in a constant expression
./mbed-os/platform/NonCopyable.h:175:5: note: declared here
    NonCopyable() { }

@kjbracey
Copy link
Contributor

Hmm, NonCopyable is kind of in need of some C++11 loving. At a minimum, just changing that to

 NonCopyable() = default;
 ~NonCopyable() = default;

should silence that error without really changing anything else about it.

@hugueskamba
Copy link
Collaborator Author

Hmm, NonCopyable is kind of in need of some C++11 loving. At a minimum, just changing that to

 NonCopyable() = default;
 ~NonCopyable() = default;

should silence that error without really changing anything else about it.

The code size is no different before my change, after my change or with the changes you are suggesting. I will leave as is because there is no meaningful benefits in making more changes.

@kjbracey
Copy link
Contributor

The code size is no different before my change, after my change or with the changes you are suggesting.

Are you sure you're building an image that actually uses FlashIAP somewhere? (And all toolchains?)

I can believe the constructor changes might have no effect in some or even all toolchains, but I'm confident the destructor removal really must make a code size difference, at least the way FlashIAP is used in nvstore and kvstore.

@hugueskamba
Copy link
Collaborator Author

The code size is no different before my change, after my change or with the changes you are suggesting.

Are you sure you're building an image that actually uses FlashIAP somewhere? (And all toolchains?)

I can believe the constructor changes might have no effect in some or even all toolchains, but I'm confident the destructor removal really must make a code size difference, at least the way FlashIAP is used in nvstore and kvstore.

Good point...I am using k64f. How can I find out if a target uses FlashIAP? Is it from targets/targets.json? If so, what should I be looking for?

@kjbracey
Copy link
Contributor

kjbracey commented Sep 17, 2019

It'll need to be a combo of a target and an application that uses its flash for storage. Sorry, I'm not really an expert. You'd want something like https://github.com/ARMmbed/mbed-os-example-kvstore for a real example.

It might be easier just to have a dummy test program - add this to blinky:

static void my_flash_test()
{
    FlashIAP flash;
}

main()
{
     my_flash_test();
}

That runs the construction/destruction cycle like kvstore does, albeit in a rather synthetic context.

(Possibly too synthetic - it might overoptimise the new form, realising it doesn't even need to bother doing any construction. Might want to add a flash.init(). That will work on K64F)

@hugueskamba hugueskamba force-pushed the hk-fix-coverity-iotcore-1334 branch from 4389fed to 8ea9aca Compare September 17, 2019 08:56
@hugueskamba
Copy link
Collaborator Author

hugueskamba commented Sep 17, 2019

This force-push removes FlashIAP destructor and inlines the constructor. It also uses C++ syntax in NonCopyable.h to avoid an error caused by the use of constexpr for FlashIAP constructor.

@0xc0170 0xc0170 self-requested a review September 17, 2019 09:05
constexpr FlashIAP() : _flash(), _page_buf(nullptr)
{

}
Copy link
Contributor

@kjbracey kjbracey Sep 17, 2019

Choose a reason for hiding this comment

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

Just going to mention in passing that C++11 gives you another option here, which is kind of neater, but maybe less familiar.

You could do

// no need to declare constructor (it will be automatically constexpr)

private:
    flash_t _flash{};
    uint8_t *_page_buf = nullptr;

C++ Core Guidelines recommends that (see C.48 and C.49), but obviously that syntax is unfamiliar in our tree, what with us only just switching to C++11/14.

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 we should first update our guidelines and then do this for the codebase (good mention !)

@evedon
Copy link
Contributor

evedon commented Sep 17, 2019

@hugueskamba what is the difference in code size before and after this PR with the dummy test program?

@hugueskamba
Copy link
Collaborator Author

hugueskamba commented Sep 17, 2019

@hugueskamba what is the difference in code size before and after this PR with the dummy test program?

Using Blinky with the changes suggested by Kevin in main.cpp and the ARM toolchain.

With the changes in this PR

Total Static RAM memory (data + bss): 206034(+0) bytes
Total Flash memory (text + data): 49223(+0) bytes

Without the changes in this PR

Total Static RAM memory (data + bss): 206034(+0) bytes
Total Flash memory (text + data): 49243(+20) bytes

@kjbracey
Copy link
Contributor

Not sure I'd file this for 5.14.1 - there's a small chance of a binary compatibility glitch due to the eliminated out-of-line methods, and it's not really a bug fix.

But if you're keen to get the 5.14 branch warning-free, it's fine.

@0xc0170
Copy link
Contributor

0xc0170 commented Sep 24, 2019

CI started

@mbed-ci
Copy link

mbed-ci commented Sep 24, 2019

Test run: FAILED

Summary: 1 of 11 test jobs failed
Build number : 1
Build artifacts

Failed test jobs:

  • jenkins-ci/mbed-os-ci_exporter

@0xc0170
Copy link
Contributor

0xc0170 commented Sep 24, 2019

exporters restarted

@0xc0170 0xc0170 merged commit 07ebd92 into ARMmbed:master Sep 24, 2019
@hugueskamba hugueskamba deleted the hk-fix-coverity-iotcore-1334 branch November 18, 2019 15:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants