Program: 26478
Project: 26478
Buggy source:
/** 
 * Prepares the legend and calculates all needed forms, labels and colors.
 * @param data
 */
public void computeLegend(ChartData<?> data){
  if (!mLegend.isLegendCustom()) {
    List<String> labels=new ArrayList<String>();
    List<Integer> colors=new ArrayList<Integer>();
    for (int i=0; i < data.getDataSetCount(); i++) {
      DataSet<? extends Entry> dataSet=data.getDataSetByIndex(i);
      List<Integer> clrs=dataSet.getColors();
      int entryCount=dataSet.getEntryCount();
      if (dataSet instanceof BarDataSet && ((BarDataSet)dataSet).isStacked()) {
        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));
        }
        if (bds.getLabel() != null) {
          colors.add(ColorTemplate.COLOR_SKIP);
          labels.add(bds.getLabel());
        }
      }
 else       if (dataSet instanceof PieDataSet) {
        List<String> xVals=data.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));
        }
        if (pds.getLabel() != null) {
          colors.add(ColorTemplate.COLOR_SKIP);
          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=data.getDataSetByIndex(i).getLabel();
            labels.add(label);
          }
          colors.add(clrs.get(j));
        }
      }
    }
    if (mLegend.getExtraColors() != null && mLegend.getExtraLabels() != null) {
      for (      int color : mLegend.getExtraColors())       colors.add(color);
      Collections.addAll(labels,mLegend.getExtraLabels());
    }
    mLegend.setColors(colors);
    mLegend.setLabels(labels);
  }
  Typeface tf=mLegend.getTypeface();
  if (tf != null)   mLegendLabelPaint.setTypeface(tf);
  mLegendLabelPaint.setTextSize(mLegend.getTextSize());
  mLegendLabelPaint.setColor(mLegend.getTextColor());
  mLegend.calculateDimensions(mLegendLabelPaint,mViewPortHandler);
}

Fixed version (ground truth):
/** 
 * Prepares the legend and calculates all needed forms, labels and colors.
 * @param data
 */
public void computeLegend(ChartData<?> data){
  if (!mLegend.isLegendCustom()) {
    List<String> labels=new ArrayList<String>();
    List<Integer> colors=new ArrayList<Integer>();
    for (int i=0; i < data.getDataSetCount(); i++) {
      DataSet<? extends Entry> dataSet=data.getDataSetByIndex(i);
      List<Integer> clrs=dataSet.getColors();
      int entryCount=dataSet.getEntryCount();
      if (dataSet instanceof BarDataSet && ((BarDataSet)dataSet).isStacked()) {
        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));
        }
        if (bds.getLabel() != null) {
          colors.add(ColorTemplate.COLOR_SKIP);
          labels.add(bds.getLabel());
        }
      }
 else       if (dataSet instanceof PieDataSet) {
        List<String> xVals=data.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));
        }
        if (pds.getLabel() != null) {
          colors.add(ColorTemplate.COLOR_SKIP);
          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=data.getDataSetByIndex(i).getLabel();
            labels.add(label);
          }
          colors.add(clrs.get(j));
        }
      }
    }
    if (mLegend.getExtraColors() != null && mLegend.getExtraLabels() != null) {
      for (      int color : mLegend.getExtraColors())       colors.add(color);
      Collections.addAll(labels,mLegend.getExtraLabels());
    }
    mLegend.setComputedColors(colors);
    mLegend.setComputedLabels(labels);
  }
  Typeface tf=mLegend.getTypeface();
  if (tf != null)   mLegendLabelPaint.setTypeface(tf);
  mLegendLabelPaint.setTextSize(mLegend.getTextSize());
  mLegendLabelPaint.setColor(mLegend.getTextColor());
  mLegend.calculateDimensions(mLegendLabelPaint,mViewPortHandler);
}