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

Eliminate defects found by Coverity #16

Merged
merged 3 commits into from Apr 15, 2024
Merged

Eliminate defects found by Coverity #16

merged 3 commits into from Apr 15, 2024

Conversation

martyone
Copy link
Member

No description provided.

Coverity suggests to pass the argument by move instead of copy. Refactor
it to follow the convention of using const ref for such arguments
instead.
Sugggested by Coverity (CID 449636, CID 449638).
Copy link
Contributor

@attah attah left a comment

Choose a reason for hiding this comment

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

...in case anyone cars for my two cents.

, m_asyncRunning(false)
, m_notified(false)
, m_complete(false)
, m_rowsChanges(false)
Copy link
Contributor

Choose a reason for hiding this comment

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

Not doing this is definitely an improvement.

@@ -117,26 +117,26 @@ public slots:

QString m_asyncErrorString;
QVector<TransferDBRecord> m_asyncRows;
QVector<TransferDBRecord> *m_rows;
QVector<TransferDBRecord> *m_rows{};
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this really the preferred syntax? I find this quite unintuitive.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, search for "C++ uniform initialization".

Copy link
Contributor

Choose a reason for hiding this comment

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

Not a fan of the syntax myself either, though initialization by the declaration is nicer than initialization lists.

One thing I'm pondering, though, is whether we should be using more explicit values on these, e.g. '= nullptr'; which should have the same result. We've been doing that to some extend already. Some characters more but could be nicer to read. Granted that I might get used to these eventually too.

Copy link
Contributor

Choose a reason for hiding this comment

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

Everything is better than initializer lists :)

I tend to prefer:
type myMember = type();
or
type myMember = someDefaultValue;

Copy link
Member Author

Choose a reason for hiding this comment

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

I have been using '= foo' too, because the new syntax was not easy to adapt to. But in general, '= foo' does not do the same as '{foo}', and the latter is less likely to cause issues, so it should be better choice when consistency is desired. It is also suggested by the C++ Core Guidelines https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#es23-prefer-the--initializer-syntax.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sure there are differences in going in depth of these, but what's the effective difference when talking about default values for POD member variables? More complex types should be anyway handled in the .cpp side.

The linked guidelines don't seem to explicitly cover the class declaration, but rather the generic instantiation of variables.

Pardon a bit of bike-shedding here, but maybe better do it once and not in all the forthcoming places getting coverity treatment :)

Copy link
Contributor

Choose a reason for hiding this comment

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

@martyone: Right, thus my first preference of type().

Also: can't aggregate initialization (if this is indeed a sub-case of that) accidentally bypass use of proper/public constructors?
https://stackoverflow.com/questions/65580725/aggregate-initialization-by-passes-private-class-constructor

Copy link
Member Author

Choose a reason for hiding this comment

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

@pvuorela, actually it is so that the linked guidelines simply make no exception with class declarations, they cover all cases with single rule. An example related to member initialization exists a bit more down, following that same rule https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#c48-prefer-default-member-initializers-to-member-initializers-in-constructors-for-constant-initializers.

@attah, this is quite a special use case and addressed in C++20 as mentioned in the answer.

OTOH, QtC's coding style, which is closer to us, prefers assignment for simple types, so updated.

Copy link
Contributor

Choose a reason for hiding this comment

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

Alright, I'm good with this.

Few cases of uninitialized members discovered by Coverity (CID 449633,
CID 449640) and there was already one related fix few commits back, so
better uniformly use default member initializers to ensure all members
get reasonable defaults.  The less exceptions the better, so even d_ptr
etc. Also drop some code the compiler can generate automatically.
@martyone martyone merged commit 8567aeb into master Apr 15, 2024
@martyone martyone deleted the jb61927 branch April 15, 2024 05:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants