Program: 60925
Project: 60925
Buggy source:
public PropagationByResource update(final SyncopeUser user,final UserMod userMod) throws SyncopeClientCompositeErrorException {
  PropagationByResource propByRes=new PropagationByResource();
  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);
    propByRes.addAll(PropagationOperation.UPDATE,user.getExternalResources());
  }
  propByRes.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 (      ExternalResource resource : membership.getSyncopeRole().getExternalResources()) {
        if (!membershipToBeAddedRoleIds.contains(membership.getSyncopeRole().getId())) {
          propByRes.add(PropagationOperation.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);
        propByRes.addAll(PropagationOperation.UPDATE,role.getExternalResources());
      }
      propByRes.merge(fill(membership,membershipMod,AttributableUtil.MEMBERSHIP,scce));
    }
  }
  return propByRes;
}

Fixed version (ground truth):
/** 
 * Update user, given UserMod.
 * @param user to be updated
 * @param userMod bean containing update request
 * @return updated user + propagation by resource
 * @throws SyncopeClientCompositeErrorException if anything goes wrong
 * @see PropagationByResource
 */
public PropagationByResource update(final SyncopeUser user,final UserMod userMod) throws SyncopeClientCompositeErrorException {
  PropagationByResource propByRes=new PropagationByResource();
  SyncopeClientCompositeErrorException scce=new SyncopeClientCompositeErrorException(HttpStatus.BAD_REQUEST);
  Set<String> currentResources=user.getExternalResourceNames();
  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);
    propByRes.addAll(PropagationOperation.UPDATE,user.getExternalResources());
  }
  propByRes.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 (      ExternalResource resource : membership.getSyncopeRole().getExternalResources()) {
        if (!membershipToBeAddedRoleIds.contains(membership.getSyncopeRole().getId())) {
          propByRes.add(PropagationOperation.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);
        propByRes.addAll(PropagationOperation.UPDATE,role.getExternalResources());
      }
      propByRes.merge(fill(membership,membershipMod,AttributableUtil.MEMBERSHIP,scce));
    }
  }
  Set<String> updatedResources=user.getExternalResourceNames();
  updatedResources.removeAll(currentResources);
  if (!updatedResources.isEmpty() && StringUtils.isBlank(userMod.getPassword())) {
    SyncopeClientException sce=new SyncopeClientException(SyncopeClientExceptionType.RequiredValuesMissing);
    sce.addElement("String_Node_Str" + "String_Node_Str");
    scce.addException(sce);
    throw scce;
  }
  return propByRes;
}