Program: 5520
Project: 5520
Buggy source:
private void readLines(ELFSection sec){
  if (DEBUG) {
    System.out.println("String_Node_Str" + sec.getSize());
  }
  sec.reset();
  int endPos=0;
  ArrayList<LineEntry> lineData=new ArrayList<LineEntry>();
  while (sec.getPosition() < sec.getSize()) {
    if (DEBUG)     System.out.println("String_Node_Str");
    int totLen=sec.readElf32();
    int version=sec.readElf16();
    int proLen=sec.readElf32();
    int minOpLen=sec.readElf8();
    int defaultIsStmt=sec.readElf8();
    int lineBase=sec.readElf8();
    int lineRange=sec.readElf8();
    int opcodeBase=sec.readElf8();
    endPos+=4 + totLen;
    if (DEBUG) {
      System.out.println("String_Node_Str" + totLen + "String_Node_Str"+ endPos);
      System.out.println("String_Node_Str" + proLen);
      System.out.println("String_Node_Str" + version);
    }
    if (lineBase > 127) {
      lineBase=lineBase - 256;
    }
    if (DEBUG) {
      System.out.println("String_Node_Str" + lineBase);
      System.out.println("String_Node_Str" + lineRange);
      System.out.println("String_Node_Str" + opcodeBase);
    }
    for (int i=0; i < opcodeBase - 1; i++) {
      sec.readElf8();
    }
    if (DEBUG)     System.out.println("String_Node_Str");
    ArrayList<String> directories=new ArrayList<String>();
    directories.add("String_Node_Str");
    ArrayList<String> files=new ArrayList<String>();
    StringBuilder sb=new StringBuilder();
    int c;
    while ((c=sec.readElf8()) != 0) {
      sb.append((char)c);
      while ((c=sec.readElf8()) != 0)       sb.append((char)c);
      if (DEBUG)       System.out.println("String_Node_Str" + sb.toString());
      directories.add(sb.toString());
      sb.setLength(0);
    }
    if (DEBUG)     System.out.println("String_Node_Str");
    long dirIndex=0;
    long time=0;
    long size=0;
    while ((c=sec.readElf8()) != 0) {
      sb.append((char)c);
      while ((c=sec.readElf8()) != 0)       sb.append((char)c);
      dirIndex=sec.readLEB128();
      time=sec.readLEB128();
      size=sec.readLEB128();
      if (DEBUG)       System.out.println("String_Node_Str" + sb.toString() + "String_Node_Str"+ dirIndex+ "String_Node_Str"+ size);
      files.add(directories.get((int)dirIndex) + "String_Node_Str" + sb.toString());
      sb.setLength(0);
    }
    if (DEBUG) {
      System.out.println("String_Node_Str" + sec.getPosition() + "String_Node_Str"+ endPos);
      System.out.println("String_Node_Str");
      System.out.print("String_Node_Str");
    }
    while (sec.getPosition() < endPos) {
      lineAddress=0;
      lineFile=1;
      lineLine=1;
      lineColumn=0;
      endSequence=false;
      isStatement=defaultIsStmt != 0;
      isBasicBlock=false;
      lineData.clear();
      while (!endSequence) {
        int ins=sec.readElf8();
        if (DEBUG)         System.out.print("String_Node_Str" + sec.getPosition() + "String_Node_Str"+ Utils.hex8(ins)+ "String_Node_Str");
switch (ins) {
case DW_LNS_EXT:
          int len=sec.readElf8();
        int extIns=sec.readElf8();
      if (DEBUG)       System.out.println("String_Node_Str" + Utils.hex8(extIns));
switch (extIns) {
case DW_LNE_end_sequence:
      endSequence=true;
    if (DEBUG)     System.out.println("String_Node_Str");
  break;
case DW_LNE_define_file:
if (DEBUG) System.out.println("String_Node_Str");
break;
case DW_LNE_set_address:
if (len == 2) lineAddress=sec.readElf8();
if (len == 3) lineAddress=sec.readElf16();
if (len == 5) lineAddress=sec.readElf32();
if (DEBUG) System.out.println("String_Node_Str" + Utils.hex16(lineAddress) + "String_Node_Str"+ len+ "String_Node_Str");
break;
case DW_LNE_set_discriminator:
sec.readLEB128();
break;
}
break;
case DW_LNS_copy:
if (DEBUG) System.out.println("String_Node_Str" + lineLine + "String_Node_Str"+ Utils.hex16(lineAddress)+ "String_Node_Str");
lineData.add(new LineEntry(lineLine,lineAddress));
isBasicBlock=false;
break;
case DW_LNS_advance_pc:
long add=sec.readLEB128();
lineAddress+=add * minOpLen;
if (DEBUG) System.out.println("String_Node_Str" + Utils.hex16(lineAddress));
break;
case DW_LNS_advance_line:
long addLine=sec.readLEB128S();
lineLine+=addLine;
if (DEBUG) System.out.println("String_Node_Str" + lineLine + "String_Node_Str"+ addLine+ "String_Node_Str");
break;
case DW_LNS_set_file:
lineFile=(int)sec.readLEB128();
if (DEBUG) System.out.println("String_Node_Str" + lineFile);
break;
case DW_LNS_set_column:
lineColumn=(int)sec.readLEB128();
if (DEBUG) System.out.println("String_Node_Str" + lineColumn);
break;
case DW_LNS_negate_stmt:
isStatement=!isStatement;
if (DEBUG) System.out.println("String_Node_Str");
break;
case DW_LNS_set_basic_block:
isBasicBlock=true;
if (DEBUG) System.out.println("String_Node_Str");
break;
case DW_LNS_const_add_pc:
if (DEBUG) System.out.println("String_Node_Str");
break;
case DW_LNS_fixed_advance_pc:
int incr=sec.readElf16();
lineAddress+=incr;
if (DEBUG) System.out.println("String_Node_Str" + Utils.hex16(lineAddress));
break;
default :
if (DEBUG) {
System.out.println("String_Node_Str" + Utils.hex8(ins));
}
int lineInc=lineBase + ((ins - opcodeBase) % lineRange);
int addrInc=(ins - opcodeBase) / lineRange;
lineAddress+=addrInc * minOpLen;
lineLine+=lineInc;
lineData.add(new LineEntry(lineLine,lineAddress));
isBasicBlock=false;
if (DEBUG) System.out.println("String_Node_Str" + Utils.hex16(lineAddress) + "String_Node_Str"+ lineLine+ "String_Node_Str"+ lineInc);
}
}
if (DEBUG) System.out.println("String_Node_Str" + sec.getPosition() + "String_Node_Str"+ totLen+ "String_Node_Str"+ endPos);
if (lineData.size() > 0) {
LineData lineTable=new LineData();
lineTable.lineEntries=lineData.toArray(new LineEntry[0]);
lineTable.includeDirs=directories.toArray(new String[0]);
lineTable.sourceFiles=files.toArray(new String[0]);
lineInfo.add(lineTable);
}
}
}
if (DEBUG) {
for (LineData data : lineInfo) {
System.out.println("String_Node_Str" + data.sourceFiles[0]);
System.out.println("String_Node_Str" + Utils.hex16(data.lineEntries[0].address));
System.out.println("String_Node_Str" + Utils.hex16(data.lineEntries[data.lineEntries.length - 1].address));
System.out.println("String_Node_Str" + Utils.hex16(data.lineEntries[data.lineEntries.length - 1].address - data.lineEntries[0].address));
}
}
}

Fixed version (ground truth):
private void readLines(ELFSection sec){
  if (DEBUG) {
    System.out.println("String_Node_Str" + sec.getSize());
  }
  sec.reset();
  int endPos=0;
  ArrayList<LineEntry> lineData=new ArrayList<LineEntry>();
  while (sec.getPosition() < sec.getSize()) {
    if (DEBUG)     System.out.println("String_Node_Str");
    int totLen=sec.readElf32();
    int version=sec.readElf16();
    int proLen=sec.readElf32();
    int minOpLen=sec.readElf8();
    int defaultIsStmt=sec.readElf8();
    int lineBase=Integer.valueOf(sec.readElf8()).byteValue();
    int lineRange=sec.readElf8();
    int opcodeBase=sec.readElf8();
    endPos+=4 + totLen;
    if (DEBUG) {
      System.out.println("String_Node_Str" + totLen + "String_Node_Str"+ endPos);
      System.out.println("String_Node_Str" + proLen);
      System.out.println("String_Node_Str" + version);
    }
    if (DEBUG) {
      System.out.println("String_Node_Str" + lineBase);
      System.out.println("String_Node_Str" + lineRange);
      System.out.println("String_Node_Str" + opcodeBase);
    }
    for (int i=0; i < opcodeBase - 1; i++) {
      sec.readElf8();
    }
    if (DEBUG)     System.out.println("String_Node_Str");
    ArrayList<String> directories=new ArrayList<String>();
    directories.add("String_Node_Str");
    ArrayList<String> files=new ArrayList<String>();
    StringBuilder sb=new StringBuilder();
    int c;
    while ((c=sec.readElf8()) != 0) {
      sb.append((char)c);
      while ((c=sec.readElf8()) != 0) {
        sb.append((char)c);
      }
      if (DEBUG)       System.out.println("String_Node_Str" + sb.toString());
      directories.add(sb.toString());
      sb.setLength(0);
    }
    if (DEBUG)     System.out.println("String_Node_Str");
    long dirIndex=0;
    long time=0;
    long size=0;
    while ((c=sec.readElf8()) != 0) {
      sb.append((char)c);
      while ((c=sec.readElf8()) != 0) {
        sb.append((char)c);
      }
      dirIndex=sec.readLEB128();
      time=sec.readLEB128();
      size=sec.readLEB128();
      if (DEBUG)       System.out.println("String_Node_Str" + sb.toString() + "String_Node_Str"+ dirIndex+ "String_Node_Str"+ size);
      files.add(directories.get((int)dirIndex) + "String_Node_Str" + sb.toString());
      sb.setLength(0);
    }
    if (DEBUG) {
      System.out.println("String_Node_Str" + sec.getPosition() + "String_Node_Str"+ endPos);
      System.out.println("String_Node_Str");
      System.out.print("String_Node_Str");
    }
    while (sec.getPosition() < endPos) {
      lineAddress=0;
      lineFile=1;
      lineLine=1;
      lineColumn=0;
      endSequence=false;
      isStatement=defaultIsStmt != 0;
      isBasicBlock=false;
      lineData.clear();
      while (!endSequence) {
        int opCode=sec.readElf8();
        if (DEBUG)         System.out.print("String_Node_Str" + sec.getPosition() + "String_Node_Str"+ Utils.hex8(opCode)+ "String_Node_Str");
switch (opCode) {
case DW_LNS_EXT:
          int len=(int)sec.readLEB128();
        int extIns=sec.readElf8();
      if (DEBUG)       System.out.println("String_Node_Str" + Utils.hex8(extIns));
switch (extIns) {
case DW_LNE_end_sequence:
      endSequence=true;
    lineData.add(new LineEntry(lineLine,lineAddress,lineFile));
  lineAddress=0;
lineFile=1;
lineLine=1;
lineColumn=0;
isStatement=defaultIsStmt != 0;
isBasicBlock=false;
if (DEBUG) System.out.println("String_Node_Str");
break;
case DW_LNE_set_address:
lineAddress=sec.readElf16();
if (DEBUG) System.out.println("String_Node_Str" + Utils.hex16(lineAddress) + "String_Node_Str"+ len+ "String_Node_Str");
break;
case DW_LNE_define_file:
if (DEBUG) System.out.println("String_Node_Str");
break;
case DW_LNE_set_discriminator:
sec.readElf8();
break;
default :
}
break;
case DW_LNS_copy:
if (DEBUG) System.out.println("String_Node_Str" + lineLine + "String_Node_Str"+ Utils.hex16(lineAddress)+ "String_Node_Str");
lineData.add(new LineEntry(lineLine,lineAddress,lineFile));
isBasicBlock=false;
break;
case DW_LNS_advance_pc:
long add=sec.readLEB128();
lineAddress+=minOpLen * add;
if (DEBUG) System.out.println("String_Node_Str" + Utils.hex16(lineAddress));
break;
case DW_LNS_advance_line:
long addLine=sec.readLEB128S();
lineLine+=addLine;
if (DEBUG) System.out.println("String_Node_Str" + lineLine + "String_Node_Str"+ addLine+ "String_Node_Str");
break;
case DW_LNS_set_file:
lineFile=(int)sec.readLEB128();
if (DEBUG) System.out.println("String_Node_Str" + lineFile);
break;
case DW_LNS_set_column:
lineColumn=(int)sec.readLEB128();
if (DEBUG) System.out.println("String_Node_Str" + lineColumn);
break;
case DW_LNS_negate_stmt:
isStatement=!isStatement;
if (DEBUG) System.out.println("String_Node_Str");
break;
case DW_LNS_set_basic_block:
isBasicBlock=true;
if (DEBUG) System.out.println("String_Node_Str");
break;
case DW_LNS_const_add_pc:
if (DEBUG) System.out.println("String_Node_Str");
{
int adjustedOpcode=255 - opcodeBase;
int operationAdvance=adjustedOpcode / lineRange;
lineAddress+=minOpLen * operationAdvance;
}
break;
case DW_LNS_fixed_advance_pc:
int incr=sec.readElf16();
lineAddress+=incr;
if (DEBUG) System.out.println("String_Node_Str" + Utils.hex16(lineAddress));
break;
case DW_LNS_set_prologue_end:
break;
case DW_LNS_set_epilogue_begin:
break;
case DW_LNS_set_isa:
sec.readLEB128();
break;
default :
if (DEBUG) {
System.out.println("String_Node_Str" + Utils.hex8(opCode));
}
int adjustedOpcode=opCode - opcodeBase;
int operationAdvance=adjustedOpcode / lineRange;
int lineInc=lineBase + (adjustedOpcode % lineRange);
lineLine+=lineInc;
lineAddress+=minOpLen * operationAdvance;
lineData.add(new LineEntry(lineLine,lineAddress,lineFile));
isBasicBlock=false;
if (DEBUG) System.out.println("String_Node_Str" + Utils.hex16(lineAddress) + "String_Node_Str"+ lineLine+ "String_Node_Str"+ lineInc);
}
}
if (DEBUG) System.out.println("String_Node_Str" + sec.getPosition() + "String_Node_Str"+ totLen+ "String_Node_Str"+ endPos);
if (lineData.size() > 0) {
LineData lineTable=new LineData();
lineTable.lineEntries=lineData.toArray(new LineEntry[0]);
lineTable.includeDirs=directories.toArray(new String[0]);
lineTable.sourceFiles=files.toArray(new String[0]);
lineInfo.add(lineTable);
}
}
}
if (DEBUG) {
for (LineData data : lineInfo) {
System.out.println("String_Node_Str" + data.sourceFiles[0]);
System.out.println("String_Node_Str" + Utils.hex16(data.lineEntries[0].address));
System.out.println("String_Node_Str" + Utils.hex16(data.lineEntries[data.lineEntries.length - 1].address));
System.out.println("String_Node_Str" + Utils.hex16(data.lineEntries[data.lineEntries.length - 1].address - data.lineEntries[0].address));
}
}
}