Program: 5945
Project: 5945
Buggy source:
/** 
 * Scan all files in the local system artifact directory, looking for jar files and adding them as system artifacts. If the artifact already exists it will not be added again unless it is a snapshot version.
 * @throws IOException if there was some IO error adding the system artifacts
 */
public void addSystemArtifacts() throws Exception {
  Principal principal=authenticationContext.getPrincipal();
  authorizationEnforcer.enforce(NamespaceId.SYSTEM,principal,Action.WRITE);
  List<SystemArtifactInfo> systemArtifacts=new ArrayList<>();
  for (  File systemArtifactDir : systemArtifactDirs) {
    for (    File jarFile : DirUtils.listFiles(systemArtifactDir,"String_Node_Str")) {
      Id.Artifact artifactId;
      try {
        artifactId=Id.Artifact.parse(Id.Namespace.SYSTEM,jarFile.getName());
      }
 catch (      IllegalArgumentException e) {
        LOG.warn(String.format("String_Node_Str",e.getMessage()));
        continue;
      }
      co.cask.cdap.proto.id.ArtifactId artifact=artifactId.toEntityId();
      privilegesManager.revoke(artifact);
      privilegesManager.grant(artifact,principal,Collections.singleton(Action.ALL));
      String artifactFileName=jarFile.getName();
      String configFileName=artifactFileName.substring(0,artifactFileName.length() - "String_Node_Str".length()) + "String_Node_Str";
      File configFile=new File(systemArtifactDir,configFileName);
      try {
        ArtifactConfig artifactConfig=configFile.isFile() ? configReader.read(artifactId.getNamespace(),configFile) : new ArtifactConfig();
        validateParentSet(artifactId,artifactConfig.getParents());
        validatePluginSet(artifactConfig.getPlugins());
        systemArtifacts.add(new SystemArtifactInfo(artifactId,jarFile,artifactConfig));
      }
 catch (      InvalidArtifactException e) {
        LOG.warn(String.format("String_Node_Str",artifactFileName),e);
        privilegesManager.revoke(artifact);
      }
    }
  }
  Set<Id.Artifact> parents=new HashSet<>();
  for (  SystemArtifactInfo child : systemArtifacts) {
    Id.Artifact childId=child.getArtifactId();
    for (    SystemArtifactInfo potentialParent : systemArtifacts) {
      Id.Artifact potentialParentId=potentialParent.getArtifactId();
      if (childId.equals(potentialParentId)) {
        continue;
      }
      if (child.getConfig().hasParent(potentialParentId)) {
        parents.add(potentialParentId);
      }
    }
  }
  for (  SystemArtifactInfo systemArtifact : systemArtifacts) {
    if (parents.contains(systemArtifact.getArtifactId())) {
      addSystemArtifact(systemArtifact);
    }
  }
  for (  SystemArtifactInfo systemArtifact : systemArtifacts) {
    if (!parents.contains(systemArtifact.getArtifactId())) {
      addSystemArtifact(systemArtifact);
    }
  }
}

Fixed version (ground truth):
/** 
 * Scan all files in the local system artifact directory, looking for jar files and adding them as system artifacts. If the artifact already exists it will not be added again unless it is a snapshot version.
 * @throws IOException if there was some IO error adding the system artifacts
 */
public void addSystemArtifacts() throws Exception {
  Principal principal=authenticationContext.getPrincipal();
  authorizationEnforcer.enforce(NamespaceId.SYSTEM,principal,Action.WRITE);
  List<SystemArtifactInfo> systemArtifacts=new ArrayList<>();
  for (  File systemArtifactDir : systemArtifactDirs) {
    for (    File jarFile : DirUtils.listFiles(systemArtifactDir,"String_Node_Str")) {
      Id.Artifact artifactId;
      try {
        artifactId=Id.Artifact.parse(Id.Namespace.SYSTEM,jarFile.getName());
      }
 catch (      IllegalArgumentException e) {
        LOG.warn(String.format("String_Node_Str",e.getMessage()));
        continue;
      }
      co.cask.cdap.proto.id.ArtifactId artifact=artifactId.toEntityId();
      privilegesManager.revoke(artifact);
      privilegesManager.grant(artifact,principal,EnumSet.allOf(Action.class));
      String artifactFileName=jarFile.getName();
      String configFileName=artifactFileName.substring(0,artifactFileName.length() - "String_Node_Str".length()) + "String_Node_Str";
      File configFile=new File(systemArtifactDir,configFileName);
      try {
        ArtifactConfig artifactConfig=configFile.isFile() ? configReader.read(artifactId.getNamespace(),configFile) : new ArtifactConfig();
        validateParentSet(artifactId,artifactConfig.getParents());
        validatePluginSet(artifactConfig.getPlugins());
        systemArtifacts.add(new SystemArtifactInfo(artifactId,jarFile,artifactConfig));
      }
 catch (      InvalidArtifactException e) {
        LOG.warn(String.format("String_Node_Str",artifactFileName),e);
        privilegesManager.revoke(artifact);
      }
    }
  }
  Set<Id.Artifact> parents=new HashSet<>();
  for (  SystemArtifactInfo child : systemArtifacts) {
    Id.Artifact childId=child.getArtifactId();
    for (    SystemArtifactInfo potentialParent : systemArtifacts) {
      Id.Artifact potentialParentId=potentialParent.getArtifactId();
      if (childId.equals(potentialParentId)) {
        continue;
      }
      if (child.getConfig().hasParent(potentialParentId)) {
        parents.add(potentialParentId);
      }
    }
  }
  for (  SystemArtifactInfo systemArtifact : systemArtifacts) {
    if (parents.contains(systemArtifact.getArtifactId())) {
      addSystemArtifact(systemArtifact);
    }
  }
  for (  SystemArtifactInfo systemArtifact : systemArtifacts) {
    if (!parents.contains(systemArtifact.getArtifactId())) {
      addSystemArtifact(systemArtifact);
    }
  }
}