Program: 146891
Project: 146891
Buggy source:
public boolean install(Resource r,ResourceLocation location,Reference ref,ResourceTable table,CommCareInstance instance,boolean upgrade) throws UnresolvedResourceException {
  if (location.getAuthority() == Resource.RESOURCE_AUTHORITY_LOCAL) {
    try {
      if (ref.doesBinaryExist()) {
        localReference=ref.getURI();
        table.commit(r,Resource.RESOURCE_STATUS_INSTALLED);
        return true;
      }
 else {
        return false;
      }
    }
 catch (    IOException e) {
      e.printStackTrace();
      return false;
    }
  }
 else   if (location.getAuthority() == Resource.RESOURCE_AUTHORITY_REMOTE) {
    try {
      if (!ref.doesBinaryExist()) {
        return false;
      }
      InputStream incoming=ref.getStream();
      if (incoming == null) {
        return false;
      }
      String uri=ref.getURI();
      int lastslash=uri.lastIndexOf('/');
      uri=uri.substring(lastslash == -1 ? 0 : lastslash + 1);
      int copy=0;
      try {
        Reference destination=ReferenceManager._().DeriveReference("String_Node_Str" + uri);
        while (destination.doesBinaryExist()) {
          copy++;
          String newUri=uri + "String_Node_Str" + copy;
          destination=ReferenceManager._().DeriveReference("String_Node_Str" + newUri);
        }
        if (destination.isReadOnly()) {
          return cache(incoming,r,table);
        }
        OutputStream output=destination.getOutputStream();
        StreamsUtil.writeFromInputToOutput(incoming,output);
        output.close();
        this.localReference=destination.getURI();
        if (upgrade) {
          table.commit(r,Resource.RESOURCE_STATUS_UPGRADE);
        }
 else {
          table.commit(r,Resource.RESOURCE_STATUS_INSTALLED);
        }
        return true;
      }
 catch (      InvalidReferenceException e) {
        return cache(incoming,r,table);
      }
    }
 catch (    IOException e) {
      Logger.exception(e);
      return false;
    }
  }
  return false;
}

Fixed version (ground truth):
public boolean install(Resource r,ResourceLocation location,Reference ref,ResourceTable table,CommCareInstance instance,boolean upgrade) throws UnresolvedResourceException {
  if (location.getAuthority() == Resource.RESOURCE_AUTHORITY_LOCAL) {
    try {
      if (ref.doesBinaryExist()) {
        localReference=ref.getURI();
        table.commit(r,Resource.RESOURCE_STATUS_INSTALLED);
        return true;
      }
 else {
        return false;
      }
    }
 catch (    IOException e) {
      e.printStackTrace();
      return false;
    }
  }
 else   if (location.getAuthority() == Resource.RESOURCE_AUTHORITY_REMOTE) {
    try {
      if (!ref.doesBinaryExist()) {
        return false;
      }
      InputStream incoming=ref.getStream();
      if (incoming == null) {
        return false;
      }
      String uri=ref.getURI();
      int lastslash=uri.lastIndexOf('/');
      uri=uri.substring(lastslash == -1 ? 0 : lastslash + 1);
      String cleanUri="String_Node_Str";
      for (int i=0; i < uri.length(); ++i) {
        char c=uri.charAt(i);
        if (valid.indexOf(c) == -1) {
          cleanUri+="String_Node_Str";
        }
 else {
          cleanUri+=c;
        }
      }
      uri=cleanUri;
      int copy=0;
      try {
        Reference destination=ReferenceManager._().DeriveReference("String_Node_Str" + uri);
        while (destination.doesBinaryExist()) {
          copy++;
          String newUri=uri + "String_Node_Str" + copy;
          destination=ReferenceManager._().DeriveReference("String_Node_Str" + newUri);
        }
        if (destination.isReadOnly()) {
          return cache(incoming,r,table);
        }
        OutputStream output=destination.getOutputStream();
        StreamsUtil.writeFromInputToOutput(incoming,output);
        output.close();
        this.localReference=destination.getURI();
        if (upgrade) {
          table.commit(r,Resource.RESOURCE_STATUS_UPGRADE);
        }
 else {
          table.commit(r,Resource.RESOURCE_STATUS_INSTALLED);
        }
        return true;
      }
 catch (      InvalidReferenceException e) {
        return cache(incoming,r,table);
      }
    }
 catch (    IOException e) {
      Logger.exception(e);
      return false;
    }
  }
  return false;
}