Grasscutters / Grasscutter

A server software reimplementation for a certain anime game.
https://grasscutter.io/
GNU Affero General Public License v3.0
16.2k stars 4.47k forks source link

Switch order of literals to prevent NullPointerException #2495

Closed pixeeai closed 5 months ago

pixeeai commented 6 months ago

Description

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 * [https://cwe.mitre.org/data/definitions/476.html](https://cwe.mitre.org/data/definitions/476.html) * [https://en.wikibooks.org/wiki/Java_Programming/Preventing_NullPointerException](https://en.wikibooks.org/wiki/Java_Programming/Preventing_NullPointerException) * [https://rules.sonarsource.com/java/RSPEC-1132/](https://rules.sonarsource.com/java/RSPEC-1132/)

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

Type of changes

Checklist:

pixeeai commented 6 months ago

FYI - This change was autogenerated from a new trending GitHub app - called Pixeebot. A code-quality GitHub App; like Dependabot, but for source code.