Program: 61008
Project: 61008
Buggy source:
public ResourceOperations update(SyncopeUser user,UserMod userMod) throws SyncopeClientCompositeErrorException {
  SyncopeClientCompositeErrorException scce=new SyncopeClientCompositeErrorException(HttpStatus.BAD_REQUEST);
  if (userMod.getPassword() != null) {
    int passwordHistorySize=0;
    try {
      Policy policy=policyDAO.getGlobalPasswordPolicy();
      PasswordPolicy passwordPolicy=policy.getSpecification();
      passwordHistorySize=passwordPolicy.getHistoryLength();
    }
 catch (    Throwable ignore) {
    }
    user.setPassword(userMod.getPassword(),getCipherAlgoritm(),passwordHistorySize);
  }
  ResourceOperations resourceOperations=fill(user,userMod,AttributableUtil.USER,scce);
  Set<Long> membershipToBeAddedRoleIds=new HashSet<Long>();
  for (  MembershipMod membershipToBeAdded : userMod.getMembershipsToBeAdded()) {
    membershipToBeAddedRoleIds.add(membershipToBeAdded.getRole());
  }
  Membership membership=null;
  for (  Long membershipToBeRemovedId : userMod.getMembershipsToBeRemoved()) {
    if (LOG.isDebugEnabled()) {
      LOG.debug("String_Node_Str" + membershipToBeRemovedId);
    }
    membership=membershipDAO.find(membershipToBeRemovedId);
    if (membership == null) {
      if (LOG.isDebugEnabled()) {
        LOG.debug("String_Node_Str" + membershipToBeRemovedId);
      }
    }
 else {
      for (      TargetResource resource : membership.getSyncopeRole().getTargetResources()) {
        if (!membershipToBeAddedRoleIds.contains(membership.getSyncopeRole().getId())) {
          resourceOperations.add(ResourceOperationType.DELETE,resource);
        }
      }
      membership=user.getMembership(membership.getSyncopeRole().getId());
      if (membershipToBeAddedRoleIds.contains(membership.getSyncopeRole().getId())) {
        Set<Long> attributeIds=new HashSet<Long>(membership.getAttributes().size());
        for (        AbstractAttr attribute : membership.getAttributes()) {
          attributeIds.add(attribute.getId());
        }
        for (        Long attributeId : attributeIds) {
          attributeDAO.delete(attributeId,MAttr.class);
        }
        attributeIds.clear();
        for (        AbstractDerAttr derivedAttribute : membership.getDerivedAttributes()) {
          attributeIds.add(derivedAttribute.getId());
        }
        for (        Long derivedAttributeId : attributeIds) {
          derivedAttributeDAO.delete(derivedAttributeId,MDerAttr.class);
        }
        attributeIds.clear();
        for (        AbstractVirAttr virtulaAttribute : membership.getVirtualAttributes()) {
          attributeIds.add(virtulaAttribute.getId());
        }
        for (        Long virtualAttributeId : attributeIds) {
          virtualAttributeDAO.delete(virtualAttributeId,MVirAttr.class);
        }
      }
 else {
        user.removeMembership(membership);
        membershipDAO.delete(membershipToBeRemovedId);
      }
    }
  }
  SyncopeRole role=null;
  for (  MembershipMod membershipMod : userMod.getMembershipsToBeAdded()) {
    if (LOG.isDebugEnabled()) {
      LOG.debug("String_Node_Str" + membershipMod.getRole() + "String_Node_Str");
    }
    role=roleDAO.find(membershipMod.getRole());
    if (role == null) {
      if (LOG.isDebugEnabled()) {
        LOG.debug("String_Node_Str" + membershipMod.getRole());
      }
    }
 else {
      membership=user.getMembership(role.getId());
      if (membership == null) {
        membership=new Membership();
        membership.setSyncopeRole(role);
        membership.setSyncopeUser(user);
        user.addMembership(membership);
        resourceOperations.addAll(ResourceOperationType.UPDATE,role.getTargetResources());
      }
      resourceOperations.merge(fill(membership,membershipMod,AttributableUtil.MEMBERSHIP,scce));
    }
  }
  return resourceOperations;
}

Fixed version (ground truth):
public ResourceOperations update(SyncopeUser user,UserMod userMod) throws SyncopeClientCompositeErrorException {
  ResourceOperations resOps=new ResourceOperations();
  SyncopeClientCompositeErrorException scce=new SyncopeClientCompositeErrorException(HttpStatus.BAD_REQUEST);
  if (userMod.getPassword() != null) {
    int passwordHistorySize=0;
    try {
      Policy policy=policyDAO.getGlobalPasswordPolicy();
      PasswordPolicy passwordPolicy=policy.getSpecification();
      passwordHistorySize=passwordPolicy.getHistoryLength();
    }
 catch (    Throwable ignore) {
    }
    user.setPassword(userMod.getPassword(),getCipherAlgoritm(),passwordHistorySize);
    resOps.addAll(ResourceOperationType.UPDATE,user.getTargetResources());
  }
  resOps.merge(fill(user,userMod,AttributableUtil.USER,scce));
  Set<Long> membershipToBeAddedRoleIds=new HashSet<Long>();
  for (  MembershipMod membershipToBeAdded : userMod.getMembershipsToBeAdded()) {
    membershipToBeAddedRoleIds.add(membershipToBeAdded.getRole());
  }
  Membership membership=null;
  for (  Long membershipId : userMod.getMembershipsToBeRemoved()) {
    LOG.debug("String_Node_Str",membershipId);
    membership=membershipDAO.find(membershipId);
    if (membership == null) {
      LOG.debug("String_Node_Str",membershipId);
    }
 else {
      for (      TargetResource resource : membership.getSyncopeRole().getTargetResources()) {
        if (!membershipToBeAddedRoleIds.contains(membership.getSyncopeRole().getId())) {
          resOps.add(ResourceOperationType.DELETE,resource);
        }
      }
      membership=user.getMembership(membership.getSyncopeRole().getId());
      if (membershipToBeAddedRoleIds.contains(membership.getSyncopeRole().getId())) {
        Set<Long> attributeIds=new HashSet<Long>(membership.getAttributes().size());
        for (        AbstractAttr attribute : membership.getAttributes()) {
          attributeIds.add(attribute.getId());
        }
        for (        Long attributeId : attributeIds) {
          attributeDAO.delete(attributeId,MAttr.class);
        }
        attributeIds.clear();
        for (        AbstractDerAttr derAttr : membership.getDerivedAttributes()) {
          attributeIds.add(derAttr.getId());
        }
        for (        Long derAttrId : attributeIds) {
          derAttrDAO.delete(derAttrId,MDerAttr.class);
        }
        attributeIds.clear();
        for (        AbstractVirAttr virAttr : membership.getVirtualAttributes()) {
          attributeIds.add(virAttr.getId());
        }
        for (        Long virAttrId : attributeIds) {
          virAttrDAO.delete(virAttrId,MVirAttr.class);
        }
        attributeIds.clear();
      }
 else {
        user.removeMembership(membership);
        membershipDAO.delete(membershipId);
      }
    }
  }
  SyncopeRole role=null;
  for (  MembershipMod membershipMod : userMod.getMembershipsToBeAdded()) {
    LOG.debug("String_Node_Str",membershipMod.getRole());
    role=roleDAO.find(membershipMod.getRole());
    if (role == null) {
      LOG.debug("String_Node_Str",membershipMod.getRole());
    }
 else {
      membership=user.getMembership(role.getId());
      if (membership == null) {
        membership=new Membership();
        membership.setSyncopeRole(role);
        membership.setSyncopeUser(user);
        user.addMembership(membership);
        resOps.addAll(ResourceOperationType.UPDATE,role.getTargetResources());
      }
      resOps.merge(fill(membership,membershipMod,AttributableUtil.MEMBERSHIP,scce));
    }
  }
  return resOps;
}