Program: 113463
Project: 113463
Buggy source:
@Override public DirectoryEntries readDir(UserCredentials userCredentials,String path,int offset,int count,boolean namesOnly) throws IOException, PosixErrorException, AddressToUUIDNotFoundException {
  DirectoryEntries result=null;
  result=metadataCache.getDirEntries(path,offset,count);
  if (result != null) {
    return result;
  }
  DirectoryEntries.Builder dirEntriesBuilder=DirectoryEntries.newBuilder();
  for (int currentOffset=offset; currentOffset < offset + count; offset+=volumeOptions.getReaddirChunkSize()) {
    int limitDirEntriesCount=(currentOffset > offset + count) ? (currentOffset - offset - count) : volumeOptions.getReaddirChunkSize();
    readdirRequest request=readdirRequest.newBuilder().setPath(path).setVolumeName(volumeName).setNamesOnly(namesOnly).setKnownEtag(0).setSeenDirectoryEntriesCount(currentOffset).setLimitDirectoryEntriesCount(limitDirEntriesCount).build();
    result=RPCCaller.<readdirRequest,DirectoryEntries>syncCall(SERVICES.MRC,userCredentials,authBogus,volumeOptions,uuidResolver,mrcUUIDIterator,false,request,new CallGenerator<readdirRequest,DirectoryEntries>(){
      @Override public RPCResponse<DirectoryEntries> executeCall(      InetSocketAddress server,      Auth authHeader,      UserCredentials userCreds,      readdirRequest input) throws IOException {
        return mrcServiceClient.readdir(server,authHeader,userCreds,input);
      }
    }
);
    assert(result != null);
    dirEntriesBuilder.addAllEntries(result.getEntriesList());
    if (result.getEntriesCount() < (currentOffset + volumeOptions.getReaddirChunkSize())) {
      break;
    }
  }
  int minimum=(volumeOptions.getMetadataCacheSize() > result.getEntriesCount()) ? result.getEntriesCount() : volumeOptions.getMetadataCacheSize();
  for (int i=0; i < minimum; i++) {
    if (result.getEntries(i).hasStbuf()) {
      if (result.getEntries(i).getStbuf().getNlink() > 1) {
        metadataCache.invalidate(path);
      }
 else {
        metadataCache.updateStat(Helper.concatenatePath(path,result.getEntries(i).getName()),result.getEntries(i).getStbuf());
      }
    }
  }
  if (!namesOnly && offset == 0 && result.getEntriesCount() < count) {
    metadataCache.updateDirEntries(path,result);
  }
  return dirEntriesBuilder.build();
}

Fixed version (ground truth):
@Override public DirectoryEntries readDir(UserCredentials userCredentials,String path,int offset,int count,boolean namesOnly) throws IOException, PosixErrorException, AddressToUUIDNotFoundException {
  DirectoryEntries result=null;
  if (count == 0) {
    count=Integer.MAX_VALUE - offset - 1;
  }
  result=metadataCache.getDirEntries(path,offset,count);
  if (result != null) {
    return result;
  }
  DirectoryEntries.Builder dirEntriesBuilder=DirectoryEntries.newBuilder();
  for (int currentOffset=offset; currentOffset < offset + count; offset+=volumeOptions.getReaddirChunkSize()) {
    int limitDirEntriesCount=(currentOffset > offset + count) ? (currentOffset - offset - count) : volumeOptions.getReaddirChunkSize();
    readdirRequest request=readdirRequest.newBuilder().setPath(path).setVolumeName(volumeName).setNamesOnly(namesOnly).setKnownEtag(0).setSeenDirectoryEntriesCount(currentOffset).setLimitDirectoryEntriesCount(limitDirEntriesCount).build();
    result=RPCCaller.<readdirRequest,DirectoryEntries>syncCall(SERVICES.MRC,userCredentials,authBogus,volumeOptions,uuidResolver,mrcUUIDIterator,false,request,new CallGenerator<readdirRequest,DirectoryEntries>(){
      @Override public RPCResponse<DirectoryEntries> executeCall(      InetSocketAddress server,      Auth authHeader,      UserCredentials userCreds,      readdirRequest input) throws IOException {
        return mrcServiceClient.readdir(server,authHeader,userCreds,input);
      }
    }
);
    assert(result != null);
    dirEntriesBuilder.addAllEntries(result.getEntriesList());
    if (result.getEntriesCount() < (currentOffset + volumeOptions.getReaddirChunkSize())) {
      break;
    }
  }
  int minimum=(volumeOptions.getMetadataCacheSize() > dirEntriesBuilder.getEntriesCount()) ? dirEntriesBuilder.getEntriesCount() : volumeOptions.getMetadataCacheSize();
  for (int i=0; i < minimum; i++) {
    if (dirEntriesBuilder.getEntries(i).hasStbuf()) {
      if (dirEntriesBuilder.getEntries(i).getStbuf().getNlink() > 1) {
        metadataCache.invalidate(path);
      }
 else {
        metadataCache.updateStat(Helper.concatenatePath(path,dirEntriesBuilder.getEntries(i).getName()),dirEntriesBuilder.getEntries(i).getStbuf());
      }
    }
  }
  if (!namesOnly && offset == 0 && dirEntriesBuilder.getEntriesCount() < count) {
    metadataCache.updateDirEntries(path,result);
  }
  return dirEntriesBuilder.build();
}