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

Fix for false positive MC_OVERRIDABLE_METHOD_CALL_IN_CONSTRUCTOR when referencing but not calling an overridable method #2900

Merged
merged 10 commits into from
Mar 27, 2024

Conversation

gtoison
Copy link
Contributor

@gtoison gtoison commented Mar 12, 2024

INVOKEDYNAMIC followed by PUTFIELD means that we assigned the method reference to a field.
I think it's a bit better to avoid false positives here, with the potential for false negatives due to this change

@gtoison gtoison marked this pull request as ready for review March 12, 2024 17:49
Copy link
Collaborator

@JuditKnoll JuditKnoll left a comment

Choose a reason for hiding this comment

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

I agree, it's better to avoid false positives with the potential of having false negatives.
I also prefer your solution here to mine with CT_CONSTRUCTOR_THROW, but it's still a workaround, and several detectors have problems with bootstrap methods like this.

I think it would be worth to work a proper solution sometime (not in this PR) to the lambda handling, not just say that "it's known that the handling of lambdas are not correct."

function = v -> v + "!";

// just to ignore warnings about unused member
System.out.println(function.apply("hello"));
Copy link
Collaborator

Choose a reason for hiding this comment

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

If I understood well, the problem here is that the StringBuilder.append() is reported as overridable, and the error is reported at line 10 without the change.
However, something is a bit off here, since using it inside a stream (Arrays.asList("a", "b").stream().map(v -> v + "!")), doesn't report the error.
With line 13 this one becomes a false negative, since the function is used and called in the constructor - if this line would be moved to another function which is not called inside the constructor, then it would be a true negative.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

My understanding is that this detectors tries to catch a class of vulnerabilities where an attacker extends a class an override a method. For instance you'd override isAdmin(), now if the constructor does this.admin = isAdmin() the object ends up in an invalid state.
In that particular case StringBuilder.append() is not controllable by the attacker, so the bug was a false positive for another reason.

Although this PR seems to fix #1867 (because in the sample the lambda was immediately assigned to a field) the underlying problem with lambdas is still there.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Thank you for your explanation.

gtoison added 2 commits March 13, 2024 18:34
Overridable methods are only reported once, add a 2nd one to make sure
the unit test shows the problem when assigning a method reference to a
field
@hazendaz hazendaz self-assigned this Mar 27, 2024
@hazendaz hazendaz added this to the SpotBugs 4.8.4 milestone Mar 27, 2024
@hazendaz hazendaz merged commit 9c36fdc into spotbugs:master Mar 27, 2024
15 checks passed
PatrikScully pushed a commit to PatrikScully/spotbugs that referenced this pull request Jun 14, 2024
… referencing but not calling an overridable method (spotbugs#2900)

* test: issue spotbugs#2837 reproducer

* fix: do not consider a method reference assigned to a field

* test: added false positive check for issue spotbugs#1867

It looks like the problem for spotbugs#1867 was fixed elsewhere

* test: added a true positive test for a lambda called in constructor

* doc: added changelog entry

* test: fixed tests

* fix: removed unused variable

* test: added a 2nd overridable method for the true positive case

Overridable methods are only reported once, add a 2nd one to make sure
the unit test shows the problem when assigning a method reference to a
field
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.

4 participants