Program: 30598
Project: 30598
Buggy source:
/** 
 * Perform various validations of SAMRecord. Note that this method deliberately returns null rather than Collections.emptyList() if there are no validation errors, because callers tend to assume that if a non-null list is returned, it is modifiable.
 * @return null if valid.  If invalid, returns a list of error messages.
 */
public List<SAMValidationError> isValid(){
  ArrayList<SAMValidationError> ret=null;
  if (!getReadPairedFlag()) {
    if (getProperPairFlagUnchecked()) {
      if (ret == null)       ret=new ArrayList<SAMValidationError>();
      ret.add(new SAMValidationError(SAMValidationError.Type.INVALID_FLAG_PROPER_PAIR,"String_Node_Str",getReadName()));
    }
    if (getMateUnmappedFlagUnchecked()) {
      if (ret == null)       ret=new ArrayList<SAMValidationError>();
      ret.add(new SAMValidationError(SAMValidationError.Type.INVALID_FLAG_MATE_UNMAPPED,"String_Node_Str",getReadName()));
    }
    if (getMateNegativeStrandFlagUnchecked()) {
      if (ret == null)       ret=new ArrayList<SAMValidationError>();
      ret.add(new SAMValidationError(SAMValidationError.Type.INVALID_FLAG_MATE_NEG_STRAND,"String_Node_Str",getReadName()));
    }
    if (getFirstOfPairFlagUnchecked()) {
      if (ret == null)       ret=new ArrayList<SAMValidationError>();
      ret.add(new SAMValidationError(SAMValidationError.Type.INVALID_FLAG_FIRST_OF_PAIR,"String_Node_Str",getReadName()));
    }
    if (getSecondOfPairFlagUnchecked()) {
      if (ret == null)       ret=new ArrayList<SAMValidationError>();
      ret.add(new SAMValidationError(SAMValidationError.Type.INVALID_FLAG_SECOND_OF_PAIR,"String_Node_Str",getReadName()));
    }
    if (getMateReferenceIndex() != NO_ALIGNMENT_REFERENCE_INDEX) {
      if (ret == null)       ret=new ArrayList<SAMValidationError>();
      ret.add(new SAMValidationError(SAMValidationError.Type.INVALID_MATE_REF_INDEX,"String_Node_Str",getReadName()));
    }
  }
 else {
    final List<SAMValidationError> errors=isValidReferenceIndexAndPosition(mMateReferenceIndex,mMateReferenceName,getMateAlignmentStart(),true);
    if (errors != null) {
      if (ret == null)       ret=new ArrayList<SAMValidationError>();
      ret.addAll(errors);
    }
    if (!hasMateReferenceName() && !getMateUnmappedFlag()) {
      if (ret == null)       ret=new ArrayList<SAMValidationError>();
      ret.add(new SAMValidationError(SAMValidationError.Type.INVALID_FLAG_MATE_UNMAPPED,"String_Node_Str",getReadName()));
    }
    if (!getFirstOfPairFlagUnchecked() && !getSecondOfPairFlagUnchecked()) {
      if (ret == null)       ret=new ArrayList<SAMValidationError>();
      ret.add(new SAMValidationError(SAMValidationError.Type.PAIRED_READ_NOT_MARKED_AS_FIRST_OR_SECOND,"String_Node_Str",getReadName()));
    }
  }
  if (getInferredInsertSize() > MAX_INSERT_SIZE || getInferredInsertSize() < -MAX_INSERT_SIZE) {
    if (ret == null)     ret=new ArrayList<SAMValidationError>();
    ret.add(new SAMValidationError(SAMValidationError.Type.INVALID_INSERT_SIZE,"String_Node_Str",getReadName()));
  }
  if (getReadUnmappedFlag()) {
    if (getNotPrimaryAlignmentFlag()) {
      if (ret == null)       ret=new ArrayList<SAMValidationError>();
      ret.add(new SAMValidationError(SAMValidationError.Type.INVALID_FLAG_NOT_PRIM_ALIGNMENT,"String_Node_Str",getReadName()));
    }
    if (getMappingQuality() != 0) {
      if (ret == null)       ret=new ArrayList<SAMValidationError>();
      ret.add(new SAMValidationError(SAMValidationError.Type.INVALID_MAPPING_QUALITY,"String_Node_Str",getReadName()));
    }
  }
 else {
    if (getMappingQuality() >= 256) {
      if (ret == null)       ret=new ArrayList<SAMValidationError>();
      ret.add(new SAMValidationError(SAMValidationError.Type.INVALID_MAPPING_QUALITY,"String_Node_Str",getReadName()));
    }
    if (getCigarLength() == 0) {
      if (ret == null)       ret=new ArrayList<SAMValidationError>();
      ret.add(new SAMValidationError(SAMValidationError.Type.INVALID_CIGAR,"String_Node_Str",getReadName()));
    }
    if (getHeader().getSequenceDictionary().size() == 0) {
      if (ret == null)       ret=new ArrayList<SAMValidationError>();
      ret.add(new SAMValidationError(SAMValidationError.Type.MISSING_SEQUENCE_DICTIONARY,"String_Node_Str",getReadName()));
    }
    if (!hasReferenceName()) {
      if (ret == null)       ret=new ArrayList<SAMValidationError>();
      ret.add(new SAMValidationError(SAMValidationError.Type.INVALID_FLAG_READ_UNMAPPED,"String_Node_Str",getReadName()));
    }
  }
  final String rgId=(String)getAttribute(SAMTagUtil.getSingleton().RG);
  if (rgId != null && getHeader().getReadGroup(rgId) == null) {
    if (ret == null)     ret=new ArrayList<SAMValidationError>();
    ret.add(new SAMValidationError(SAMValidationError.Type.READ_GROUP_NOT_FOUND,"String_Node_Str" + rgId,getReadName()));
  }
  final List<SAMValidationError> errors=isValidReferenceIndexAndPosition(mReferenceIndex,mReferenceName,getAlignmentStart(),false);
  if (errors != null) {
    if (ret == null)     ret=new ArrayList<SAMValidationError>();
    ret.addAll(errors);
  }
  if (this.getReadLength() == 0 && !this.getNotPrimaryAlignmentFlag()) {
    Object fz=getAttribute(SAMTagUtil.getSingleton().FZ);
    if (fz == null) {
      String cq=(String)getAttribute(SAMTagUtil.getSingleton().CQ);
      String cs=(String)getAttribute(SAMTagUtil.getSingleton().CS);
      if (cq == null || cq.length() == 0 || cs == null || cs.length() == 0) {
        if (ret == null)         ret=new ArrayList<SAMValidationError>();
        ret.add(new SAMValidationError(SAMValidationError.Type.EMPTY_READ,"String_Node_Str",getReadName()));
      }
 else       if (!getReadUnmappedFlag()) {
        boolean hasIndel=false;
        for (        CigarElement cigarElement : getCigar().getCigarElements()) {
          if (cigarElement.getOperator() == CigarOperator.DELETION || cigarElement.getOperator() == CigarOperator.INSERTION) {
            hasIndel=true;
            break;
          }
        }
        if (!hasIndel) {
          if (ret == null)           ret=new ArrayList<SAMValidationError>();
          ret.add(new SAMValidationError(SAMValidationError.Type.EMPTY_READ,"String_Node_Str",getReadName()));
        }
      }
    }
  }
  if (this.getReadLength() != getBaseQualities().length && !Arrays.equals(getBaseQualities(),NULL_QUALS)) {
    if (ret == null)     ret=new ArrayList<SAMValidationError>();
    ret.add(new SAMValidationError(SAMValidationError.Type.MISMATCH_READ_LENGTH_AND_QUALS_LENGTH,"String_Node_Str",getReadName()));
  }
  if (this.getAlignmentStart() != NO_ALIGNMENT_START && this.getIndexingBin() != null && this.computeIndexingBin() != this.getIndexingBin()) {
    ret.add(new SAMValidationError(SAMValidationError.Type.INVALID_INDEXING_BIN,"String_Node_Str",getReadName()));
  }
  if (ret == null || ret.size() == 0) {
    return null;
  }
  return ret;
}

Fixed version (ground truth):
/** 
 * Perform various validations of SAMRecord. Note that this method deliberately returns null rather than Collections.emptyList() if there are no validation errors, because callers tend to assume that if a non-null list is returned, it is modifiable.
 * @return null if valid.  If invalid, returns a list of error messages.
 */
public List<SAMValidationError> isValid(){
  ArrayList<SAMValidationError> ret=null;
  if (!getReadPairedFlag()) {
    if (getProperPairFlagUnchecked()) {
      if (ret == null)       ret=new ArrayList<SAMValidationError>();
      ret.add(new SAMValidationError(SAMValidationError.Type.INVALID_FLAG_PROPER_PAIR,"String_Node_Str",getReadName()));
    }
    if (getMateUnmappedFlagUnchecked()) {
      if (ret == null)       ret=new ArrayList<SAMValidationError>();
      ret.add(new SAMValidationError(SAMValidationError.Type.INVALID_FLAG_MATE_UNMAPPED,"String_Node_Str",getReadName()));
    }
    if (getMateNegativeStrandFlagUnchecked()) {
      if (ret == null)       ret=new ArrayList<SAMValidationError>();
      ret.add(new SAMValidationError(SAMValidationError.Type.INVALID_FLAG_MATE_NEG_STRAND,"String_Node_Str",getReadName()));
    }
    if (getFirstOfPairFlagUnchecked()) {
      if (ret == null)       ret=new ArrayList<SAMValidationError>();
      ret.add(new SAMValidationError(SAMValidationError.Type.INVALID_FLAG_FIRST_OF_PAIR,"String_Node_Str",getReadName()));
    }
    if (getSecondOfPairFlagUnchecked()) {
      if (ret == null)       ret=new ArrayList<SAMValidationError>();
      ret.add(new SAMValidationError(SAMValidationError.Type.INVALID_FLAG_SECOND_OF_PAIR,"String_Node_Str",getReadName()));
    }
    if (getMateReferenceIndex() != NO_ALIGNMENT_REFERENCE_INDEX) {
      if (ret == null)       ret=new ArrayList<SAMValidationError>();
      ret.add(new SAMValidationError(SAMValidationError.Type.INVALID_MATE_REF_INDEX,"String_Node_Str",getReadName()));
    }
  }
 else {
    final List<SAMValidationError> errors=isValidReferenceIndexAndPosition(mMateReferenceIndex,mMateReferenceName,getMateAlignmentStart(),true);
    if (errors != null) {
      if (ret == null)       ret=new ArrayList<SAMValidationError>();
      ret.addAll(errors);
    }
    if (!hasMateReferenceName() && !getMateUnmappedFlag()) {
      if (ret == null)       ret=new ArrayList<SAMValidationError>();
      ret.add(new SAMValidationError(SAMValidationError.Type.INVALID_FLAG_MATE_UNMAPPED,"String_Node_Str",getReadName()));
    }
    if (!getFirstOfPairFlagUnchecked() && !getSecondOfPairFlagUnchecked()) {
      if (ret == null)       ret=new ArrayList<SAMValidationError>();
      ret.add(new SAMValidationError(SAMValidationError.Type.PAIRED_READ_NOT_MARKED_AS_FIRST_OR_SECOND,"String_Node_Str",getReadName()));
    }
  }
  if (getInferredInsertSize() > MAX_INSERT_SIZE || getInferredInsertSize() < -MAX_INSERT_SIZE) {
    if (ret == null)     ret=new ArrayList<SAMValidationError>();
    ret.add(new SAMValidationError(SAMValidationError.Type.INVALID_INSERT_SIZE,"String_Node_Str",getReadName()));
  }
  if (getReadUnmappedFlag()) {
    if (getNotPrimaryAlignmentFlag()) {
      if (ret == null)       ret=new ArrayList<SAMValidationError>();
      ret.add(new SAMValidationError(SAMValidationError.Type.INVALID_FLAG_NOT_PRIM_ALIGNMENT,"String_Node_Str",getReadName()));
    }
    if (getMappingQuality() != 0) {
      if (ret == null)       ret=new ArrayList<SAMValidationError>();
      ret.add(new SAMValidationError(SAMValidationError.Type.INVALID_MAPPING_QUALITY,"String_Node_Str",getReadName()));
    }
  }
 else {
    if (getMappingQuality() >= 256) {
      if (ret == null)       ret=new ArrayList<SAMValidationError>();
      ret.add(new SAMValidationError(SAMValidationError.Type.INVALID_MAPPING_QUALITY,"String_Node_Str",getReadName()));
    }
    if (getCigarLength() == 0) {
      if (ret == null)       ret=new ArrayList<SAMValidationError>();
      ret.add(new SAMValidationError(SAMValidationError.Type.INVALID_CIGAR,"String_Node_Str",getReadName()));
    }
    if (getHeader().getSequenceDictionary().size() == 0) {
      if (ret == null)       ret=new ArrayList<SAMValidationError>();
      ret.add(new SAMValidationError(SAMValidationError.Type.MISSING_SEQUENCE_DICTIONARY,"String_Node_Str",getReadName()));
    }
    if (!hasReferenceName()) {
      if (ret == null)       ret=new ArrayList<SAMValidationError>();
      ret.add(new SAMValidationError(SAMValidationError.Type.INVALID_FLAG_READ_UNMAPPED,"String_Node_Str",getReadName()));
    }
  }
  final String rgId=(String)getAttribute(SAMTagUtil.getSingleton().RG);
  if (rgId != null && getHeader().getReadGroup(rgId) == null) {
    if (ret == null)     ret=new ArrayList<SAMValidationError>();
    ret.add(new SAMValidationError(SAMValidationError.Type.READ_GROUP_NOT_FOUND,"String_Node_Str" + rgId,getReadName()));
  }
  final List<SAMValidationError> errors=isValidReferenceIndexAndPosition(mReferenceIndex,mReferenceName,getAlignmentStart(),false);
  if (errors != null) {
    if (ret == null)     ret=new ArrayList<SAMValidationError>();
    ret.addAll(errors);
  }
  if (this.getReadLength() == 0 && !this.getNotPrimaryAlignmentFlag()) {
    Object fz=getAttribute(SAMTagUtil.getSingleton().FZ);
    if (fz == null) {
      String cq=(String)getAttribute(SAMTagUtil.getSingleton().CQ);
      String cs=(String)getAttribute(SAMTagUtil.getSingleton().CS);
      if (cq == null || cq.length() == 0 || cs == null || cs.length() == 0) {
        if (ret == null)         ret=new ArrayList<SAMValidationError>();
        ret.add(new SAMValidationError(SAMValidationError.Type.EMPTY_READ,"String_Node_Str",getReadName()));
      }
 else       if (!getReadUnmappedFlag()) {
        boolean hasIndel=false;
        for (        CigarElement cigarElement : getCigar().getCigarElements()) {
          if (cigarElement.getOperator() == CigarOperator.DELETION || cigarElement.getOperator() == CigarOperator.INSERTION) {
            hasIndel=true;
            break;
          }
        }
        if (!hasIndel) {
          if (ret == null)           ret=new ArrayList<SAMValidationError>();
          ret.add(new SAMValidationError(SAMValidationError.Type.EMPTY_READ,"String_Node_Str",getReadName()));
        }
      }
    }
  }
  if (this.getReadLength() != getBaseQualities().length && !Arrays.equals(getBaseQualities(),NULL_QUALS)) {
    if (ret == null)     ret=new ArrayList<SAMValidationError>();
    ret.add(new SAMValidationError(SAMValidationError.Type.MISMATCH_READ_LENGTH_AND_QUALS_LENGTH,"String_Node_Str",getReadName()));
  }
  if (this.getAlignmentStart() != NO_ALIGNMENT_START && this.getIndexingBin() != null && this.computeIndexingBin() != this.getIndexingBin()) {
    if (ret == null)     ret=new ArrayList<SAMValidationError>();
    ret.add(new SAMValidationError(SAMValidationError.Type.INVALID_INDEXING_BIN,"String_Node_Str",getReadName()));
  }
  if (ret == null || ret.size() == 0) {
    return null;
  }
  return ret;
}