Program: 26737
Project: 26737
Buggy source:
/** 
 * Generates an automatically prepared legend depending on the DataSets in the chart and their colors.
 */
public void prepareLegend(){
  ArrayList<String> labels=new ArrayList<String>();
  ArrayList<Integer> colors=new ArrayList<Integer>();
  for (int i=0; i < mData.getDataSetCount(); i++) {
    DataSet<? extends Entry> dataSet=mData.getDataSetByIndex(i);
    ArrayList<Integer> clrs=dataSet.getColors();
    int entryCount=dataSet.getEntryCount();
    if (dataSet instanceof BarDataSet && ((BarDataSet)dataSet).getStackSize() > 1) {
      BarDataSet bds=(BarDataSet)dataSet;
      String[] sLabels=bds.getStackLabels();
      for (int j=0; j < clrs.size() && j < entryCount && j < bds.getStackSize(); j++) {
        labels.add(sLabels[j % sLabels.length]);
        colors.add(clrs.get(j));
      }
      colors.add(-2);
      labels.add(bds.getLabel());
    }
 else     if (dataSet instanceof PieDataSet) {
      ArrayList<String> xVals=mData.getXVals();
      PieDataSet pds=(PieDataSet)dataSet;
      for (int j=0; j < clrs.size() && j < entryCount && j < xVals.size(); j++) {
        labels.add(xVals.get(j));
        colors.add(clrs.get(j));
      }
      colors.add(-2);
      labels.add(pds.getLabel());
    }
 else {
      for (int j=0; j < clrs.size() && j < entryCount; j++) {
        if (j < clrs.size() - 1 && j < entryCount - 1) {
          labels.add(null);
        }
 else {
          String label=mData.getDataSetByIndex(i).getLabel();
          labels.add(label);
        }
        colors.add(clrs.get(j));
      }
    }
  }
  Legend l=new Legend(colors,labels);
  if (mLegend != null) {
    l.apply(mLegend);
  }
  mLegend=l;
}

Fixed version (ground truth):
/** 
 * Generates an automatically prepared legend depending on the DataSets in the chart and their colors.
 */
public void prepareLegend(){
  ArrayList<String> labels=new ArrayList<String>();
  ArrayList<Integer> colors=new ArrayList<Integer>();
  for (int i=0; i < mData.getDataSetCount(); i++) {
    DataSet<? extends Entry> dataSet=mData.getDataSetByIndex(i);
    ArrayList<Integer> clrs=dataSet.getColors();
    int entryCount=dataSet.getEntryCount();
    if (dataSet instanceof BarDataSet && ((BarDataSet)dataSet).getStackSize() > 1) {
      BarDataSet bds=(BarDataSet)dataSet;
      String[] sLabels=bds.getStackLabels();
      for (int j=0; j < clrs.size() && j < bds.getStackSize(); j++) {
        labels.add(sLabels[j % sLabels.length]);
        colors.add(clrs.get(j));
      }
      colors.add(-2);
      labels.add(bds.getLabel());
    }
 else     if (dataSet instanceof PieDataSet) {
      ArrayList<String> xVals=mData.getXVals();
      PieDataSet pds=(PieDataSet)dataSet;
      for (int j=0; j < clrs.size() && j < entryCount && j < xVals.size(); j++) {
        labels.add(xVals.get(j));
        colors.add(clrs.get(j));
      }
      colors.add(-2);
      labels.add(pds.getLabel());
    }
 else {
      for (int j=0; j < clrs.size() && j < entryCount; j++) {
        if (j < clrs.size() - 1 && j < entryCount - 1) {
          labels.add(null);
        }
 else {
          String label=mData.getDataSetByIndex(i).getLabel();
          labels.add(label);
        }
        colors.add(clrs.get(j));
      }
    }
  }
  Legend l=new Legend(colors,labels);
  if (mLegend != null) {
    l.apply(mLegend);
  }
  mLegend=l;
}