Skip to content

Switch order of literals to prevent NullPointerException#3

Open
pixeebot[bot] wants to merge 1 commit into
masterfrom
pixeebot/drip-2023-12-30-pixee-java/switch-literal-first
Open

Switch order of literals to prevent NullPointerException#3
pixeebot[bot] wants to merge 1 commit into
masterfrom
pixeebot/drip-2023-12-30-pixee-java/switch-literal-first

Conversation

@pixeebot

@pixeebot pixeebot Bot commented Dec 30, 2023

Copy link
Copy Markdown

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

I have additional improvements ready for this repo! If you want to see them, leave the comment:

@pixeebot next

... and I will open a new PR right away!

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

@pixeebot pixeebot Bot mentioned this pull request Dec 30, 2023
@pixeebot

pixeebot Bot commented Jan 2, 2024

Copy link
Copy Markdown
Author

I'm confident in this change, but I'm not a maintainer of this project. Do you see any reason not to merge it?

If this change was not helpful, or you have suggestions for improvements, please let me know!

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