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

Switch order of String literals to prevent NullPointerException #3491

Merged

Conversation

pixeebot[bot]
Copy link

@pixeebot pixeebot bot commented Oct 7, 2023

This change defensively switches the order of literals in comparison expressions to ensure that no null pointer exceptions are unexpectedly thrown. Runtime exceptions especially can cause exceptional and unexpected code paths to be taken, and this can result in unexpected behavior.

Both simple vulnerabilities (like information disclosure) and complex vulnerabilities (like business logic flaws) can take advantage of these unexpected code paths.

Our changes look something like this:

  String fieldName = header.getFieldName();
  String fieldValue = header.getFieldValue();
- if(fieldName.equals("requestId")) {
+ if("requestId".equals(fieldName)) {
    logRequest(fieldValue);
  }
More reading

Powered by: pixeebot (codemod ID: pixee:java/switch-literal-first)

@marcphilipp marcphilipp merged commit 1301818 into main Oct 7, 2023
13 checks passed
@marcphilipp marcphilipp deleted the pixeebot/drip-2023-10-07-pixee-java/switch-literal-first branch October 7, 2023 09:28
@@ -56,15 +56,15 @@ private static boolean validateReservedIds(TestEngine testEngine) {
if (!engineId.startsWith("junit-")) {
return true;
}
if (engineId.equals("junit-jupiter")) {
if ("junit-jupiter".equals(engineId)) {
Copy link
Member

Choose a reason for hiding this comment

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

Although I understand the intention of this PR, this particular change cannot possibly avoid a NullPointerException, since the preceding if-statement has already invoked a method on engineId.

Instead of making changes like this one, I think we should rather focus on specifying that org.junit.platform.engine.TestEngine.getId() may never return null, and we can consider introducing a Preconditions.notNull(testEngine.getId()) check if we are concerned about that.

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

@pixeebot @ryandens Here's some feedback on the java/switch-literal-first codemod

@sbrannen sbrannen changed the title Switch order of literals to prevent NullPointerException Switch order of String literals to prevent NullPointerException Oct 8, 2023
@sbrannen sbrannen added this to the 5.11 M1 milestone Oct 8, 2023
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.

3 participants