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

Extend detector FindOverridableMethodCall to detect indirect cases #1716

Conversation

baloghadamsoftware
Copy link
Contributor

An overridable method may be called indirectly from a constructor or from a clone() method. This patch extends the FindOverridableMathodCall to also these cases. (Only thos cases are detected where the non-overridable caller calls the overridable method (directrly or indirectly) on itself.


Make sure these boxes are checked before submitting your PR -- thank you!

  • Added an entry into CHANGELOG.md if you have changed SpotBugs code

Ádám Balogh added 2 commits September 16, 2021 11:31
This new detector detects invocation of overridable method in constructors
(`MC_OVERRIDABLE_METHOD_CALL_IN_CONSTRUCTOR`) and clone() method
(`MC_OVERRIDABLE_METHOD_CALL_IN_CLONE`), according to SEI CERT rules
[MET05-J. Ensure that constructors do not call overridable methods](https://wiki.sei.cmu.edu/confluence/display/java/MET05-J.+Ensure+that+constructors+do+not+call+overridable+methods)
and [MET06-J. Do not invoke overridable methods in clone()](https://wiki.sei.cmu.edu/confluence/pages/viewpage.action?pageId=88487921)
An overridable method may be called indirectly from a constructor or from a `clone()` method. This patch extends the `FindOverridableMathodCall`  to also these cases. (Only thos cases are detected where the non-overridable caller calls the overridable method (directrly or indirectly) on itself.
@baloghadamsoftware
Copy link
Contributor Author

This is a PR over PR #1711.

@baloghadamsoftware
Copy link
Contributor Author

Only a few additional findings compared to the previous PR: Diff on SpotBugs itself, Jenkins, OpenGrok, MATSim, NanoHTTPD, Bt, Ttorrent

<p>Detector for patterns where a constructor or a clone() method calls an overridable
method.</p>
<p>
Calling an overridalbe method from a constructor may result in the use of
Copy link
Contributor

Choose a reason for hiding this comment

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

Misspelled "overridable".

<Details>
<![CDATA[<p>
Calling an overridable method during in a constructor may result in the use of uninitialized data. It may also
leak the this reference of the partially constructed object. Only static, final or private methods shoud be
Copy link
Contributor

Choose a reason for hiding this comment

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

Misspelled "should".

ThrawnCA
ThrawnCA previously approved these changes Sep 23, 2021
@baloghadamsoftware
Copy link
Contributor Author

@KengoTODA?

Copy link
Member

@KengoTODA KengoTODA left a comment

Choose a reason for hiding this comment

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

Consider to add a test case to cover public Object clone() method, current implementation seems having a problem in the sawOpcode(int seen) method.

And please merge the latest master branch to resolve conflict in the CHANGELOG.md. Thanks in advance!

checkAndRecordCallFromConstructor(ctor.method, method, ctor.sourceLine);
}
}
if (clone != null) {
Copy link
Contributor

Choose a reason for hiding this comment

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

These two conditions could be combined.

}
if (callers != null) {
for (XMethod caller : callers) {
if (!method.isPrivate() && !method.isFinal()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

"if not...else" is a double negative, please refactor.

if (cp.getClassIndex() != getThisClass().getClassNameIndex()) {
return;
}
ConstantNameAndType cnt = (ConstantNameAndType) getConstantPool()
Copy link
Contributor

Choose a reason for hiding this comment

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

Please change this variable name, it's too close to inappropriate.

}
XMethod method = getXClass().findMethod(metOpt.get().getName(), metOpt.get().getSignature(),
metOpt.get().isStatic());
if (ctor != null) {
Copy link
Contributor

Choose a reason for hiding this comment

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

These two conditions could be combined.

ThrawnCA
ThrawnCA previously approved these changes Nov 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants