Skip to content

Commit 385f6e4

Browse files
committed
fix(errorprone): flag Math.class literal to close reflection bypass
matchMemberSelect only handled VarSymbol members, so the class literal Math.class (and java.lang.Math.class) resolved to the Math ClassSymbol and slipped through, leaving a reflective back door to java.lang.Math. Detect .class selects whose qualifier is java.lang.Math explicitly. Addresses codeant-ai review on ForbidJavaLangMath.java.
1 parent 5d66743 commit 385f6e4

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

errorprone/src/main/java/errorprone/ForbidJavaLangMath.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
* <li>method references: {@code Math::max}</li>
3434
* <li>field access: {@code Math.PI}, {@code Math.E}</li>
3535
* <li>statically-imported fields used bare: {@code import static java.lang.Math.PI; ... PI}</li>
36+
* <li>class literals (reflection back door): {@code Math.class}</li>
3637
* </ul>
3738
*
3839
* <p>{@code java.lang.StrictMath} itself is intentionally allowed — it is the deterministic
@@ -68,6 +69,13 @@ public Description matchMemberReference(MemberReferenceTree tree, VisitorState s
6869

6970
@Override
7071
public Description matchMemberSelect(MemberSelectTree tree, VisitorState state) {
72+
// Type-level references such as the class literal `Math.class` resolve to the Math
73+
// ClassSymbol rather than a member, and are a back door to java.lang.Math via reflection
74+
// (e.g. Math.class.getMethod("sin").invoke(...)). Flag them explicitly.
75+
if (tree.getIdentifier().contentEquals("class")
76+
&& isJavaLangMathClass(ASTHelpers.getSymbol(tree.getExpression()))) {
77+
return describeMatch(tree);
78+
}
7179
// Method selects (Math.max) are already reported via matchMethodInvocation /
7280
// matchMemberReference; only flag field selects (Math.PI, Math.E) here to avoid
7381
// double-reporting the same usage.
@@ -90,6 +98,11 @@ public Description matchIdentifier(IdentifierTree tree, VisitorState state) {
9098
return flagIfMathMember(sym, tree);
9199
}
92100

101+
private static boolean isJavaLangMathClass(Symbol sym) {
102+
return sym instanceof Symbol.ClassSymbol
103+
&& ((Symbol.ClassSymbol) sym).getQualifiedName().contentEquals(JAVA_LANG_MATH);
104+
}
105+
93106
private Description flagIfMathMember(Symbol sym, Tree tree) {
94107
if (sym == null) {
95108
return Description.NO_MATCH;

0 commit comments

Comments
 (0)