Program: 44925
Project: 44925
Buggy source:
/** 
 * Read the headers of the archive
 * @throws RarException
 */
private void readHeaders(InputStream in) throws IOException, RarException {
  markHead=null;
  newMhd=null;
  headers.clear();
  long position=0;
  long read=0;
  byte[] baseBlockBuffer=new byte[BaseBlock.BaseBlockSize];
  byte[] buff=BufferPool.getByteArray();
  BaseBlock block;
  while ((read=in.read(baseBlockBuffer)) > 0) {
    block=new BaseBlock(baseBlockBuffer);
    block.setPositionInFile(position);
    position+=read;
switch (block.getHeaderType()) {
case UnrarHeadertype.MarkHeaderCode:
      RarDebug.trace("String_Node_Str");
    markHead=new MarkHeader(block);
  if (!markHead.isSignature()) {
    throw new RarException(RarExceptionType.badRarArchive);
  }
headers.add(markHead);
break;
case UnrarHeadertype.MainHeaderCode:
RarDebug.trace("String_Node_Str");
buff=realloc(buff,block.hasEncryptVersion() ? MainHeader.mainHeaderSizeWithEnc : MainHeader.mainHeaderSize);
position+=in.read(buff);
MainHeader mainhead=new MainHeader(block,buff);
headers.add(mainhead);
this.newMhd=mainhead;
if (newMhd.isEncrypted()) {
throw new RarException(RarExceptionType.rarEncryptedException);
}
break;
case UnrarHeadertype.SignHeaderCode:
RarDebug.trace("String_Node_Str");
buff=realloc(buff,SignHeader.signHeaderSize);
position+=in.read(buff);
headers.add(new SignHeader(block,buff));
break;
case UnrarHeadertype.AvHeaderCode:
RarDebug.trace("String_Node_Str");
buff=realloc(buff,AVHeader.avHeaderSize);
position+=in.read(buff);
headers.add(new AVHeader(block,buff));
break;
case UnrarHeadertype.CommHeaderCode:
RarDebug.trace("String_Node_Str");
buff=realloc(buff,CommentHeader.commentHeaderSize);
position+=in.read(buff);
CommentHeader header=new CommentHeader(block,buff);
headers.add(header);
position+=in.skip(header.getHeaderSize());
break;
case UnrarHeadertype.EndArcHeaderCode:
RarDebug.trace("String_Node_Str");
int toRead=0;
toRead+=block.hasArchiveDataCRC() ? EndArcHeader.endArcArchiveDataCrcSize : 0;
toRead+=block.hasVolumeNumber() ? EndArcHeader.endArcVolumeNumberSize : 0;
EndArcHeader endArcHead;
if (toRead > 0) {
buff=realloc(buff,toRead);
position+=in.read(buff);
endArcHead=new EndArcHeader(block,buff);
RarDebug.trace("String_Node_Str");
}
 else {
endArcHead=new EndArcHeader(block,null);
RarDebug.trace("String_Node_Str");
}
headers.add(endArcHead);
return;
default :
buff=realloc(buff,BlockHeader.blockHeaderSize);
position+=in.read(buff);
BlockHeader blockHead=new BlockHeader(block,buff);
switch (blockHead.getHeaderType()) {
case UnrarHeadertype.NewSubHeaderCode:
case UnrarHeadertype.FileHeaderCode:
RarDebug.trace("String_Node_Str");
buff=realloc(buff,blockHead.getHeaderSize() - BlockHeader.blockHeaderSize - BaseBlock.BaseBlockSize);
position+=in.read(buff);
FileHeader fh=new FileHeader(blockHead,buff);
headers.add(fh);
position+=in.skip(fh.getPositionInFile() + fh.getHeaderSize() + fh.getFullPackSize() - position);
break;
case UnrarHeadertype.ProtectHeaderCode:
RarDebug.trace("String_Node_Str");
buff=realloc(buff,blockHead.getHeaderSize() - BaseBlock.BaseBlockSize - BlockHeader.blockHeaderSize);
position+=in.read(buff);
ProtectHeader ph=new ProtectHeader(blockHead,buff);
position+=in.skip(ph.getHeaderSize());
break;
case UnrarHeadertype.SubHeaderCode:
{
RarDebug.trace("String_Node_Str");
buff=realloc(buff,SubBlockHeader.subBlockHeaderSize);
position+=in.read(buff);
SubBlockHeader sh=new SubBlockHeader(blockHead,buff);
switch (sh.getSubType().getSubblocktype()) {
case SubBlockHeaderType.MAC_HEAD_CODE:
{
buff=realloc(buff,MacInfoHeader.MacInfoHeaderSize);
position+=in.read(buff);
MacInfoHeader macHeader=new MacInfoHeader(sh,buff);
headers.add(macHeader);
break;
}
case SubBlockHeaderType.BEEA_HEAD_CODE:
break;
case SubBlockHeaderType.EA_HEAD_CODE:
{
buff=realloc(buff,EAHeader.EAHeaderSize);
position+=in.read(buff);
EAHeader eaHeader=new EAHeader(sh,buff);
headers.add(eaHeader);
break;
}
case SubBlockHeaderType.NTACL_HEAD_CODE:
break;
case SubBlockHeaderType.STREAM_HEAD_CODE:
break;
case SubBlockHeaderType.UO_HEAD_CODE:
toRead=sh.getHeaderSize();
toRead-=BaseBlock.BaseBlockSize;
toRead-=BlockHeader.blockHeaderSize;
toRead-=SubBlockHeader.subBlockHeaderSize;
buff=realloc(buff,toRead);
position+=in.read(buff);
UnixOwnersHeader uoHeader=new UnixOwnersHeader(sh,buff);
headers.add(uoHeader);
break;
default :
break;
}
break;
}
default :
RarDebug.trace("String_Node_Str" + blockHead.getHeaderType());
throw new RarException(RarExceptionType.wrongHeaderType);
}
}
}
BufferPool.releaseByteArray(buff);
if (read == -1) {
RarDebug.trace("String_Node_Str");
throw new RarException(RarExceptionType.badRarArchive);
}
}

Fixed version (ground truth):
/** 
 * Read the headers of the archive
 * @throws RarException
 */
private void readHeaders(InputStream in) throws IOException, RarException {
  markHead=null;
  newMhd=null;
  headers.clear();
  long position=0;
  long read=0;
  byte[] baseBlockBuffer=new byte[BaseBlock.BaseBlockSize];
  byte[] buff=BufferPool.getByteArray();
  BaseBlock block;
  while ((read=in.read(baseBlockBuffer)) > 0) {
    block=new BaseBlock(baseBlockBuffer);
    block.setPositionInFile(position);
    position+=read;
switch (block.getHeaderType()) {
case UnrarHeadertype.MarkHeaderCode:
      RarDebug.trace("String_Node_Str");
    markHead=new MarkHeader(block);
  if (!markHead.isSignature()) {
    throw new RarException(RarExceptionType.badRarArchive);
  }
headers.add(markHead);
break;
case UnrarHeadertype.MainHeaderCode:
RarDebug.trace("String_Node_Str");
buff=realloc(buff,block.hasEncryptVersion() ? MainHeader.mainHeaderSizeWithEnc : MainHeader.mainHeaderSize);
position+=in.read(buff);
MainHeader mainhead=new MainHeader(block,buff);
headers.add(mainhead);
this.newMhd=mainhead;
if (newMhd.isEncrypted()) throw new RarException(RarExceptionType.rarEncryptedException);
break;
case UnrarHeadertype.SignHeaderCode:
RarDebug.trace("String_Node_Str");
buff=realloc(buff,SignHeader.signHeaderSize);
position+=in.read(buff);
headers.add(new SignHeader(block,buff));
break;
case UnrarHeadertype.AvHeaderCode:
RarDebug.trace("String_Node_Str");
buff=realloc(buff,AVHeader.avHeaderSize);
position+=in.read(buff);
headers.add(new AVHeader(block,buff));
break;
case UnrarHeadertype.CommHeaderCode:
RarDebug.trace("String_Node_Str");
buff=realloc(buff,CommentHeader.commentHeaderSize);
position+=in.read(buff);
CommentHeader header=new CommentHeader(block,buff);
headers.add(header);
position+=in.skip(header.getHeaderSize());
break;
case UnrarHeadertype.EndArcHeaderCode:
RarDebug.trace("String_Node_Str");
int toRead=0;
toRead+=block.hasArchiveDataCRC() ? EndArcHeader.endArcArchiveDataCrcSize : 0;
toRead+=block.hasVolumeNumber() ? EndArcHeader.endArcVolumeNumberSize : 0;
EndArcHeader endArcHead;
if (toRead > 0) {
buff=realloc(buff,toRead);
position+=in.read(buff);
endArcHead=new EndArcHeader(block,buff);
RarDebug.trace("String_Node_Str");
}
 else {
endArcHead=new EndArcHeader(block,null);
RarDebug.trace("String_Node_Str");
}
headers.add(endArcHead);
return;
default :
buff=realloc(buff,BlockHeader.blockHeaderSize);
position+=in.read(buff);
BlockHeader blockHead=new BlockHeader(block,buff);
switch (blockHead.getHeaderType()) {
case UnrarHeadertype.NewSubHeaderCode:
case UnrarHeadertype.FileHeaderCode:
RarDebug.trace("String_Node_Str");
buff=realloc(buff,blockHead.getHeaderSize() - BlockHeader.blockHeaderSize - BaseBlock.BaseBlockSize);
position+=in.read(buff);
FileHeader fh=new FileHeader(blockHead,buff);
headers.add(fh);
position+=in.skip(fh.getPositionInFile() + fh.getHeaderSize() + fh.getFullPackSize() - position);
break;
case UnrarHeadertype.ProtectHeaderCode:
RarDebug.trace("String_Node_Str");
buff=realloc(buff,blockHead.getHeaderSize() - BaseBlock.BaseBlockSize - BlockHeader.blockHeaderSize);
position+=in.read(buff);
ProtectHeader ph=new ProtectHeader(blockHead,buff);
position+=in.skip(ph.getHeaderSize());
break;
case UnrarHeadertype.SubHeaderCode:
{
RarDebug.trace("String_Node_Str");
buff=realloc(buff,SubBlockHeader.subBlockHeaderSize);
position+=in.read(buff);
SubBlockHeader sh=new SubBlockHeader(blockHead,buff);
switch (sh.getSubType().getSubblocktype()) {
case SubBlockHeaderType.MAC_HEAD_CODE:
{
buff=realloc(buff,MacInfoHeader.MacInfoHeaderSize);
position+=in.read(buff);
MacInfoHeader macHeader=new MacInfoHeader(sh,buff);
headers.add(macHeader);
break;
}
case SubBlockHeaderType.BEEA_HEAD_CODE:
break;
case SubBlockHeaderType.EA_HEAD_CODE:
{
buff=realloc(buff,EAHeader.EAHeaderSize);
position+=in.read(buff);
EAHeader eaHeader=new EAHeader(sh,buff);
headers.add(eaHeader);
break;
}
case SubBlockHeaderType.NTACL_HEAD_CODE:
break;
case SubBlockHeaderType.STREAM_HEAD_CODE:
break;
case SubBlockHeaderType.UO_HEAD_CODE:
toRead=sh.getHeaderSize();
toRead-=BaseBlock.BaseBlockSize;
toRead-=BlockHeader.blockHeaderSize;
toRead-=SubBlockHeader.subBlockHeaderSize;
buff=realloc(buff,toRead);
position+=in.read(buff);
UnixOwnersHeader uoHeader=new UnixOwnersHeader(sh,buff);
headers.add(uoHeader);
break;
default :
break;
}
break;
}
default :
RarDebug.trace("String_Node_Str" + blockHead.getHeaderType());
throw new RarException(RarExceptionType.wrongHeaderType);
}
}
}
BufferPool.releaseByteArray(buff);
if (read == -1) {
RarDebug.trace("String_Node_Str");
throw new RarException(RarExceptionType.buttomlessArchive);
}
}