Program: 187771
Project: 187771
Buggy source:
/** 
 * Adds a new item to a specific list and returns its id. If the item exists already, the existing id is returned.
 * @param itemId The id of the new item.
 * @param listId The id of the shopping list the item is added.
 * @param quantity
 * @param itemType The type of the new item
 * @return id of the "contains" table entry, or -1 if insert failed.
 */
public static long addItemToList(Context context,final long itemId,final long listId,String quantity){
  long id=-1;
  long status=Status.WANT_TO_BUY;
  Cursor existingItems=context.getContentResolver().query(Shopping.Contains.CONTENT_URI,new String[]{Shopping.Contains._ID,Shopping.Contains.STATUS},"String_Node_Str",new String[]{String.valueOf(listId),String.valueOf(itemId)},null);
  if (existingItems.getCount() > 0) {
    existingItems.moveToFirst();
    id=existingItems.getLong(0);
    long oldstatus=existingItems.getLong(1);
    if (oldstatus == Status.WANT_TO_BUY) {
      status=Status.BOUGHT;
    }
    ContentValues values=new ContentValues(2);
    values.put(Shopping.Contains.STATUS,status);
    values.put(Shopping.Contains.QUANTITY,quantity);
    try {
      Uri uri=Uri.withAppendedPath(Shopping.Contains.CONTENT_URI,String.valueOf(id));
      context.getContentResolver().update(uri,values,null,null);
      Log.i(TAG,"String_Node_Str" + uri);
    }
 catch (    Exception e) {
      Log.i(TAG,"String_Node_Str",e);
    }
  }
 else {
    ContentValues values=new ContentValues(2);
    values.put(Shopping.Contains.ITEM_ID,itemId);
    values.put(Shopping.Contains.LIST_ID,listId);
    values.put(Shopping.Contains.STATUS,status);
    values.put(Shopping.Contains.QUANTITY,quantity);
    try {
      Uri uri=context.getContentResolver().insert(Shopping.Contains.CONTENT_URI,values);
      Log.i(TAG,"String_Node_Str" + uri);
      id=Long.parseLong(uri.getPathSegments().get(1));
    }
 catch (    Exception e) {
      Log.i(TAG,"String_Node_Str",e);
    }
  }
  existingItems.close();
  return id;
}

Fixed version (ground truth):
/** 
 * Adds a new item to a specific list and returns its id. If the item exists already, the existing id is returned.
 * @param itemId The id of the new item.
 * @param listId The id of the shopping list the item is added.
 * @param quantity
 * @param itemType The type of the new item
 * @return id of the "contains" table entry, or -1 if insert failed.
 */
public static long addItemToList(Context context,final long itemId,final long listId,String quantity){
  long id=-1;
  long status=Status.WANT_TO_BUY;
  Cursor existingItems=context.getContentResolver().query(Shopping.Contains.CONTENT_URI,new String[]{Shopping.Contains._ID,Shopping.Contains.STATUS},"String_Node_Str",new String[]{String.valueOf(listId),String.valueOf(itemId)},null);
  if (existingItems.getCount() > 0) {
    existingItems.moveToFirst();
    id=existingItems.getLong(0);
    long oldstatus=existingItems.getLong(1);
    if (oldstatus == Status.WANT_TO_BUY) {
      status=Status.BOUGHT;
    }
    ContentValues values=new ContentValues(2);
    values.put(Shopping.Contains.STATUS,status);
    if (quantity != null) {
      values.put(Shopping.Contains.QUANTITY,quantity);
    }
    try {
      Uri uri=Uri.withAppendedPath(Shopping.Contains.CONTENT_URI,String.valueOf(id));
      context.getContentResolver().update(uri,values,null,null);
      Log.i(TAG,"String_Node_Str" + uri);
    }
 catch (    Exception e) {
      Log.i(TAG,"String_Node_Str",e);
    }
  }
 else {
    ContentValues values=new ContentValues(2);
    values.put(Shopping.Contains.ITEM_ID,itemId);
    values.put(Shopping.Contains.LIST_ID,listId);
    values.put(Shopping.Contains.STATUS,status);
    values.put(Shopping.Contains.QUANTITY,quantity);
    try {
      Uri uri=context.getContentResolver().insert(Shopping.Contains.CONTENT_URI,values);
      Log.i(TAG,"String_Node_Str" + uri);
      id=Long.parseLong(uri.getPathSegments().get(1));
    }
 catch (    Exception e) {
      Log.i(TAG,"String_Node_Str",e);
    }
  }
  existingItems.close();
  return id;
}