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

Fix/memory leak in package case checker #685

Merged
merged 2 commits into from
Jun 16, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 37 additions & 16 deletions src/main/java/com/cflint/plugins/core/PackageCaseChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.HashSet;

import com.cflint.BugList;
import com.cflint.CFLint;
Expand All @@ -20,7 +21,7 @@
@Extension
public class PackageCaseChecker extends CFLintScannerAdapter implements CFLintSet {

private final Map<String, List<String>> componentRegister = new HashMap<>();
private final Map<String, HashSet<String[]>> componentRegister = new HashMap<>();
private final Map<String, List<PackageCaseCheckerEntry>> expressionCheckRegister = new HashMap<>();
private CFLint cflintRef;

Expand All @@ -43,47 +44,67 @@ public void expression(final CFExpression expression, final Context context, fin
}

private boolean checkComponentRegister(final Context context, final String componentPath, final String componentName) {
if (componentRegister.containsKey(componentName.toLowerCase())) {
List<String> filePathOfComponents = componentRegister.get(componentName.toLowerCase());
for (String filePathOfComponent : filePathOfComponents) {
if (filePathOfComponent.toLowerCase().endsWith(componentPath.toLowerCase())) {
if (!filePathOfComponent.endsWith(componentPath)) {
final String expectedPath = filePathOfComponent.substring(filePathOfComponent.length() - componentPath.length());
return checkComponentRegister(context, componentPath, componentName, false);
}

private boolean checkComponentRegister(final Context context, final String componentPath, final String componentName, Boolean fromExpressionRegister) {
String lowerComponentName = componentName.toLowerCase();
String lowerComponentPath = componentPath.toLowerCase();
if (componentRegister.containsKey(lowerComponentName)) {
HashSet<String[]> filePathOfComponents = componentRegister.get(lowerComponentName);
for (String[] filePathOfComponent : filePathOfComponents) {
if (filePathOfComponent[1].endsWith(lowerComponentPath)) {
if (!filePathOfComponent[0].endsWith(componentPath)) {
final String expectedPath = filePathOfComponent[0].substring(filePathOfComponent[0].length() - componentPath.length());
context.addMessage("PACKAGE_CASE_MISMATCH", expectedPath);
}
return true;
}
}
}
//otherwise remember the component use for when component is first registered.
final String key = componentName.toLowerCase();
if (!expressionCheckRegister.containsKey(key)) {
expressionCheckRegister.put(key, new ArrayList<PackageCaseCheckerEntry>());
if (!fromExpressionRegister) {
Copy link
Author

Choose a reason for hiding this comment

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

The most important part of the fix, is here, to avoid injecting the same expression multiple times, the rest was just "optimization while trying to understand what was going on"

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for the hard work. Looks good at a high level -and I'm trusting the unit tests :)

//otherwise remember the component use for when component is first registered.
final String key = lowerComponentName;
if (!expressionCheckRegister.containsKey(key)) {
expressionCheckRegister.put(key, new ArrayList<PackageCaseCheckerEntry>());
}
expressionCheckRegister.get(key).add(new PackageCaseCheckerEntry(context, componentPath, componentName));
}
expressionCheckRegister.get(key).add(new PackageCaseCheckerEntry(context, componentPath, componentName));
return false;
}

@Override
public void startComponent(final Context context, final BugList bugs) {
final String key = context.getComponentName().toLowerCase();
if (!componentRegister.containsKey(key)) {
componentRegister.put(key, new ArrayList<String>());
componentRegister.put(key, new HashSet<String[]>());
}
componentRegister.get(key).add(normalize(context.getFilename()));
String component_name=normalize(context.getFilename());
componentRegister.get(key).add(new String[]{component_name, component_name.toLowerCase()});

//if an expression already referenced this component, check it here:
boolean matched = false;
if (expressionCheckRegister.containsKey(key)) {
List<PackageCaseCheckerEntry> clonedList = new ArrayList<>();
Map<String, Boolean> lookupCache = new HashMap<String, Boolean>();
List<PackageCaseCheckerEntry> clonedList = new ArrayList<PackageCaseCheckerEntry>();
clonedList.addAll(expressionCheckRegister.get(key));
boolean checkComponentRegisterResult = false;
for (final PackageCaseCheckerEntry expressionEntry : clonedList) {
if (checkComponentRegister(expressionEntry.context, expressionEntry.componentPath, expressionEntry.componentName)) {
String cacheKey = expressionEntry.componentPath + "||" + expressionEntry.componentName;
if (!lookupCache.containsKey(cacheKey)) {
checkComponentRegisterResult = checkComponentRegister(expressionEntry.context, expressionEntry.componentPath, expressionEntry.componentName, true);
lookupCache.put(cacheKey, checkComponentRegisterResult);
} else {
checkComponentRegisterResult = lookupCache.get(cacheKey);
}

if (checkComponentRegisterResult) {
matched = true;
for (ContextMessage message : expressionEntry.context.getMessages()) {
cflintRef.reportRule(expressionEntry.context.getElement(), null, expressionEntry.context, this, message);
}
expressionEntry.context.getMessages().clear();
break;
}
}
}
Expand Down