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

Issue-637 #1802

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open

Issue-637 #1802

wants to merge 9 commits into from

Conversation

ramraval
Copy link
Contributor

Addresses Issue #637 by adding new detector & associated test case classes for bad combinations of format strings used with SimpleDateFormat

(Apologies for any confusion, I know @arnab44 had been assigned to this issue about a month ago, but I am a first-time contributor who took up this issue for a university project)


Make sure these boxes are checked before submitting your PR -- thank you!

  • Added an entry into CHANGELOG.md if you have changed SpotBugs code

-Split "Issue637" class into two separate classes, one expecting errors and one expecting no errors
-Refactored "Issue637Test" file to reflect new, simplified test format
-Added JavaDocs for public functions
…d constructors to DateFormatStringChecker

-Added tests for new functionality to corresponding test classes
@@ -0,0 +1,137 @@
/*
* FindBugs - Find bugs in Java programs
Copy link
Contributor

Choose a reason for hiding this comment

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

New files should not use a FindBugs copyright notice.

Copy link
Member

@KengoTODA KengoTODA 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 your contribution!
Please check my feedback to keep the code maintainable. Also, run ./gradlew spotlessApply to format java code.

spotbugs/etc/findbugs.xml Outdated Show resolved Hide resolved
spotbugs/etc/messages.xml Outdated Show resolved Hide resolved
spotbugs/etc/findbugs.xml Outdated Show resolved Hide resolved
AM_PM_HOURS_FLAG_1, AM_PM_HOURS_FLAG_2, MILITARY_HOURS_FLAG_1, MILITARY_HOURS_FLAG_2, WEEK_YEAR_FLAG_1
));

if (CONST_ARRAY_LIST.contains(seen) && stack.getStackDepth() > 0) {
Copy link
Member

Choose a reason for hiding this comment

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

better to apply the early return, to keep nests in the code less.

if (!CONST_ARRAY_LIST.contains(seen) || stack.getStackDepth() == 0) {
  return;
}


private static final String BUG_TYPE = "FS_BAD_DATE_FORMAT_FLAG_COMBO";

String dateFormatString;
Copy link
Member

Choose a reason for hiding this comment

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

This field is used only by one method, then better to replace it with a local variable.

return result;
}

private boolean checkStringForBadCombo(String stringToCheck, String missingFlag, List<String> requiredFlags) {
Copy link
Member

Choose a reason for hiding this comment

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

For me runCheckStringForBadCombo and checkStringForBadCombo sounds the same.
Could you consider renaming them, or adding documentation comments to clarify the difference?

Comment on lines 7324 to 7326
This format stream includes a bad combination of format flags which may lead to unexpected behavior. Examples include
using a week year ("Y") without specifying week in year ("w") or using an AM/PM hour ("h" or "K") without specifying
an AM/PM marker ("a").
Copy link
Member

Choose a reason for hiding this comment

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

Consider providing the list of full examples.
Users will not read SpotBugs source code, we need a document or URL to tell 'what is the problem' and 'how you can fix it'.

private static final List<String> AM_PM_HOURS_FLAG_2 = new ArrayList<>(Arrays.asList("a", "K"));
private static final List<String> MILITARY_HOURS_FLAG_1 = new ArrayList<>(Arrays.asList(null,"H","a"));
private static final List<String> MILITARY_HOURS_FLAG_2 = new ArrayList<>(Arrays.asList(null,"k","a"));
private static final List<String> WEEK_YEAR_FLAG_1 = new ArrayList<>(Arrays.asList("w","Y","M","d"));
Copy link
Member

@KengoTODA KengoTODA Nov 15, 2021

Choose a reason for hiding this comment

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

In this list Arrays.asList("w","Y","M","d") the first element has different meaning. It would be better to introduce a class to replace this list.

private static final class DateFormatRule {
  /** Required flag to run verification by this rule. Always apply rule if this field is {@code null}. */
  @Nullable
  String precondition;
  /** List of required flags in the pattern. */
  @NonNull
  List<String> requiredFlags;

  /** @return {@code true} if precondition matches and all required flags exist in the given pattern. */
  boolean verify(String dateFormatString) { ... }
}

ramraval and others added 3 commits November 16, 2021 11:43
Co-authored-by: Kengo TODA <skypencil+github@gmail.com>
Co-authored-by: Kengo TODA <skypencil+github@gmail.com>
Co-authored-by: Kengo TODA <skypencil+github@gmail.com>
ThrawnCA
ThrawnCA previously approved these changes Nov 17, 2021
-Updates description for FS_BAD_DATE_FORMAT_FLAG_COMBO in "messages.xml"
-Reflects suggested changes to DateFormatStringChecker, including adding a nested DateFormatRule object and other smaller updates
@ramraval
Copy link
Contributor Author

Thank you for the feedback, @KengoTODA! I have reflected your suggestions in the latest commit. Please let me know what you think.

private static final String BUG_TYPE = "FS_BAD_DATE_FORMAT_FLAG_COMBO";

String dateFormatString;
final BugReporter bugReporter;
Copy link
Collaborator

Choose a reason for hiding this comment

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

This field could be private.

if (this.dateFormatString != null && (sig.indexOf(SIG_CONSTANT_OPERAND_1) >= 0 ||
sig.indexOf(SIG_CONSTANT_OPERAND_2) >= 0 || sig.indexOf(SIG_CONSTANT_OPERAND_3) >= 0 )
&& CLASS_CONSTANT_OPERAND.equals(cl) && (NAME_CONSTANT_OPERAND_1.equals(nm)
|| NAME_CONSTANT_OPERAND_2.equals(nm) || NAME_CONSTANT_OPERAND_3.equals(nm))) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

To enhance readability, it's better to have the related parts in one line, and have the line break consistently before the || and && signs.

BugInstanceMatcher bugTypeMatcher = new BugInstanceMatcherBuilder()
.bugType(DESIRED_BUG_TYPE)
.build();
assertThat(getBugCollection(), containsExactly(25, bugTypeMatcher));
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you please add asserts for every bug checking the information you added to the BugInstance (e.g. containing method, source line)?

@github-actions github-actions bot added the Stale label Mar 4, 2024
@github-actions github-actions bot closed this Apr 8, 2024
@hazendaz hazendaz reopened this Apr 12, 2024
@github-actions github-actions bot removed the Stale label Apr 13, 2024
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.

None yet

5 participants