From 6f850f8390356fa6e44030cdf996feb781802fec Mon Sep 17 00:00:00 2001 From: Dave Brosius Date: Fri, 1 Nov 2024 21:14:37 -0400 Subject: [PATCH] rule out PMB modifier calls in private method (allow addition of false negatives for now) #472 --- .../fbcontrib/detect/PossibleMemoryBloat.java | 6 ++++-- src/samples/java/ex/PMB_Sample.java | 9 +++++++++ 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/mebigfatguy/fbcontrib/detect/PossibleMemoryBloat.java b/src/main/java/com/mebigfatguy/fbcontrib/detect/PossibleMemoryBloat.java index e39bc4f0f..fb4f3dbe8 100755 --- a/src/main/java/com/mebigfatguy/fbcontrib/detect/PossibleMemoryBloat.java +++ b/src/main/java/com/mebigfatguy/fbcontrib/detect/PossibleMemoryBloat.java @@ -93,6 +93,7 @@ public class PossibleMemoryBloat extends BytecodeScanningDetector { private Set threadLocalNonStaticFields; private Map userValues; private Map jaxbContextRegs; + private boolean isPrivateMethod; /** * constructs a PMB detector given the reporter to report bugs on @@ -179,6 +180,7 @@ private void parseFields(ClassContext classContext) { @Override public void visitMethod(Method obj) { methodName = obj.getName(); + isPrivateMethod = (obj.getAccessFlags() & Const.ACC_PRIVATE) != 0; } /** @@ -195,7 +197,7 @@ public void visitCode(Code obj) { if (Values.STATIC_INITIALIZER.equals(methodName) || Values.CONSTRUCTOR.equals(methodName)) { return; } - + super.visitCode(obj); for (Integer pc : jaxbContextRegs.values()) { @@ -313,7 +315,7 @@ protected void checkMethodAsDecreasingOrIncreasing(XField field) { if (decreasingMethods.contains(mName)) { bloatableCandidates.remove(field); bloatableFields.remove(field); - } else if (increasingMethods.contains(mName)) { + } else if (increasingMethods.contains(mName) && !isPrivateMethod) { FieldAnnotation fieldAn = bloatableCandidates.get(field); if (fieldAn != null) { bloatableFields.put(field, fieldAn); diff --git a/src/samples/java/ex/PMB_Sample.java b/src/samples/java/ex/PMB_Sample.java index b16dedf38..fc5d49e15 100755 --- a/src/samples/java/ex/PMB_Sample.java +++ b/src/samples/java/ex/PMB_Sample.java @@ -25,6 +25,8 @@ public class PMB_Sample { private static Map fp_data = new WeakHashMap<>(); private static Map fpEmptyWithIterator = new HashMap<>(); + + private static Set fpGrowsInPrivate = new HashSet<>(); private static final Set bloatableSigs = new HashSet<>(); @@ -125,6 +127,13 @@ public static void fpCleanUpWithIterator276(String key) { fpEmptyWithIterator.put(key, "foo"); } } + + private void fpGrowsInPrivate() { + String sample = "Sample"; + for (int i = 0; i < sample.length(); i++) { + fpGrowsInPrivate.add(sample.substring(i, i+1)); + } + } class X { }