-
Notifications
You must be signed in to change notification settings - Fork 6
feat(errorprone): enable production-only safety checks #143
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
base: develop
Are you sure you want to change the base?
Changes from all commits
ee0eb8f
c60d565
b6d7bcf
8e90ade
cd313fb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -125,15 +125,49 @@ subprojects { | |
| errorprone "com.google.errorprone:error_prone_core:${errorproneVersion}" | ||
| errorprone rootProject.project(':errorprone') | ||
| } | ||
| // Keep test compilation free of Error Prone so existing test code does not block builds. | ||
| tasks.withType(JavaCompile).configureEach { | ||
| options.errorprone.enabled = false | ||
| } | ||
| tasks.named(sourceSets.main.compileJavaTaskName, JavaCompile).configure { | ||
| options.errorprone { | ||
| enabled = true | ||
| disableWarningsInGeneratedCode = true | ||
| disableAllChecks = true | ||
| excludedPaths = '.*/generated/.*' | ||
| errorproneArgs.addAll([ | ||
| // Project-specific checks. | ||
| '-Xep:BigDecimalFloatingPointConstructor:ERROR', | ||
| '-Xep:SelfAssignment:ERROR', | ||
| '-Xep:StringCaseLocaleUsage:ERROR', | ||
| '-Xep:StringCaseLocaleUsageMethodRef:ERROR', | ||
| '-Xep:ForbidJavaLangMath:ERROR', | ||
|
Comment on lines
+140
to
+144
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
These checks are added inside the existing Useful? React with 👍 / 👎. |
||
| '-Xep:ComparatorNeverReturnsZero:ERROR', | ||
|
|
||
| // High-signal checks. | ||
| '-Xep:ArrayEquals:ERROR', | ||
| '-Xep:ArrayHashCode:ERROR', | ||
| '-Xep:ArrayToString:ERROR', | ||
| '-Xep:ArraysAsListPrimitiveArray:ERROR', | ||
| '-Xep:BadShiftAmount:ERROR', | ||
| '-Xep:CollectionIncompatibleType:ERROR', | ||
| '-Xep:ConstantOverflow:ERROR', | ||
| '-Xep:EqualsHashCode:ERROR', | ||
| '-Xep:EqualsIncompatibleType:ERROR', | ||
| '-Xep:FloatingPointLiteralPrecision:ERROR', | ||
| '-Xep:GuardedBy:ERROR', | ||
| '-Xep:JavaTimeDefaultTimeZone:ERROR', | ||
| '-Xep:MathRoundIntLong:ERROR', | ||
| '-Xep:NonAtomicVolatileUpdate:ERROR', | ||
| '-Xep:ThreadJoinLoop:ERROR', | ||
| '-Xep:UnicodeEscape:ERROR', | ||
| '-Xep:XorPower:ERROR', | ||
|
|
||
| // Small production baseline fixed in this change. | ||
| '-Xep:IntLongMath:ERROR', | ||
| '-Xep:LockNotBeforeTry:ERROR', | ||
| '-Xep:MissingCasesInEnumSwitch:ERROR', | ||
| '-Xep:CatchAndPrintStackTrace:ERROR', | ||
| ]) | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package errorprone; | ||
|
|
||
| import com.google.auto.service.AutoService; | ||
| import com.google.errorprone.BugPattern; | ||
| import com.google.errorprone.VisitorState; | ||
| import com.google.errorprone.bugpatterns.BugChecker; | ||
| import com.google.errorprone.matchers.Description; | ||
| import com.google.errorprone.util.ASTHelpers; | ||
| import com.sun.source.tree.NewClassTree; | ||
| import com.sun.tools.javac.code.Symbol; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Prevents constructing {@link java.math.BigDecimal} from binary floating-point values. | ||
| * | ||
| * <p>This checks the resolved constructor signature, so it also catches {@link Double} and | ||
| * {@link Float} arguments that javac unboxes to the {@code double} constructor. | ||
| */ | ||
| @AutoService(BugChecker.class) | ||
| @BugPattern( | ||
| name = "BigDecimalFloatingPointConstructor", | ||
| summary = "Do not construct BigDecimal from a floating-point value. Use a decimal String, " | ||
| + "for example new BigDecimal(\"0.0001\").", | ||
| severity = BugPattern.SeverityLevel.ERROR | ||
| ) | ||
| public class BigDecimalFloatingPointConstructor extends BugChecker | ||
| implements BugChecker.NewClassTreeMatcher { | ||
|
|
||
| private static final String BIG_DECIMAL = "java.math.BigDecimal"; | ||
|
|
||
| @Override | ||
| public Description matchNewClass(NewClassTree tree, VisitorState state) { | ||
| Symbol symbol = ASTHelpers.getSymbol(tree); | ||
| if (!(symbol instanceof Symbol.MethodSymbol)) { | ||
| return Description.NO_MATCH; | ||
| } | ||
|
|
||
| Symbol.MethodSymbol constructor = (Symbol.MethodSymbol) symbol; | ||
| if (!constructor.owner.getQualifiedName().contentEquals(BIG_DECIMAL)) { | ||
| return Description.NO_MATCH; | ||
| } | ||
|
|
||
| List<Symbol.VarSymbol> parameters = constructor.getParameters(); | ||
| if (parameters.isEmpty() | ||
| || !ASTHelpers.isSameType(parameters.get(0).type, state.getSymtab().doubleType, state)) { | ||
| return Description.NO_MATCH; | ||
| } | ||
|
|
||
| return describeMatch(tree); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: The subtraction is performed before clamping the number of items, so malformed short calldata produces a negative count for
ValidateMultiSign(for example, zero-length input yields-1) and a zero count for shortBatchValidateSigninput. InProgram, any non-positive required energy bypasses therequiredEnergy > suppliedEnergycheck, allowing malformed calls to proceed without being charged for the precompile. Clamp the count to a non-negative value or reject malformed calldata before calculating energy. [incorrect condition logic]Severity Level: Major⚠️
(Use Cmd/Ctrl + Click for best experience)
Prompt for AI Agent 🤖