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

Mitigate possible NSE exception in PrivateAccessValidator.java #2015

Conversation

jyoo980
Copy link
Contributor

@jyoo980 jyoo980 commented Oct 21, 2023

Issue #, if available: n/a

Description of changes:

Within PrivateAccessValidator#getPrivateAccessValiationEvent, an unchecked call to Optional#get occurs. This might throw a NoSuchElementException if the optional is indeed empty.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@jyoo980 jyoo980 requested a review from a team as a code owner October 21, 2023 20:48
Copy link
Member

@mtdowling mtdowling left a comment

Choose a reason for hiding this comment

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

Thanks for submitting this. Did you run into this issue in practice? By the time this validator is invoked, all model references have been validated. Given this, I think a more tactical fix is to change the call to .get() the neighbor to use expectNeighborShape instead. This way it throws a more useful error message and doesn’t need the extra complexity. See

Using `Relationship#expectNeighborShape` in place of the optional .get
defined for `Relationship`.
@jyoo980
Copy link
Contributor Author

jyoo980 commented Oct 23, 2023

Thanks for the review, @mtdowling; I didn't mean for you to look at this over the weekend, apologies.

I haven't seen the error in-practice, but it was reported by a static analysis tool after I ran a compilation locally. I'm happy to help improve the codebase if this is something you would find useful. Are there any places in the code in particular that actually might be at risk of NoSuchElementException?

@mtdowling
Copy link
Member

Thanks for updating. Generally any place that we use .get() right now is probably fine and rather than convert to returning optionals or additional conditionals, we could switch to using these kinds of expect* style methods when available or using something like orElseThrow.

@jyoo980
Copy link
Contributor Author

jyoo980 commented Oct 24, 2023

Got it, thanks! I'll fix the checkstyle errors shortly.

@jyoo980 jyoo980 requested a review from mtdowling October 25, 2023 16:33
@mtdowling mtdowling merged commit 6d1a7cd into smithy-lang:main Oct 26, 2023
10 checks passed
@jyoo980 jyoo980 deleted the yoo/mitigate-possible-no-such-element-exception branch October 26, 2023 17:04
@mtdowling
Copy link
Member

Thanks!

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.

2 participants