Program: 41166
Project: 41166
Buggy source:
/** 
 * Called when a context menu option for the text view is selected.  Currently this will be one of:   {@link android.R.id#selectAll},  {@link android.R.id#startSelectingText},  {@link android.R.id#cut},   {@link android.R.id#copy},  {@link android.R.id#paste},   {@link android.R.id#copyUrl}, or   {@link android.R.id#switchInputMethod}.
 */
public boolean onTextContextMenuItem(int id){
  int min=0;
  int max=mText.length();
  if (isFocused()) {
    final int selStart=getSelectionStart();
    final int selEnd=getSelectionEnd();
    min=Math.max(0,Math.min(selStart,selEnd));
    max=Math.max(0,Math.max(selStart,selEnd));
  }
  ClipboardManager clip=(ClipboardManager)getContext().getSystemService(Context.CLIPBOARD_SERVICE);
switch (id) {
case ID_SELECT_ALL:
    Selection.setSelection((Spannable)mText,0,mText.length());
  startTextSelectionMode();
return true;
case ID_START_SELECTING_TEXT:
startTextSelectionMode();
return true;
case ID_CUT:
clip.setText(mTransformed.subSequence(min,max));
((Editable)mText).delete(min,max);
stopTextSelectionMode();
return true;
case ID_COPY:
clip.setText(mTransformed.subSequence(min,max));
stopTextSelectionMode();
return true;
case ID_PASTE:
CharSequence paste=clip.getText();
if (paste != null && paste.length() > 0) {
long minMax=prepareSpacesAroundPaste(min,max,paste);
min=extractRangeStartFromLong(minMax);
max=extractRangeEndFromLong(minMax);
Selection.setSelection((Spannable)mText,max);
((Editable)mText).replace(min,max,paste);
stopTextSelectionMode();
}
return true;
case ID_COPY_URL:
URLSpan[] urls=((Spanned)mText).getSpans(min,max,URLSpan.class);
if (urls.length == 1) {
clip.setText(urls[0].getURL());
}
return true;
case ID_SWITCH_INPUT_METHOD:
InputMethodManager imm=InputMethodManager.peekInstance();
if (imm != null) {
imm.showInputMethodPicker();
}
return true;
case ID_ADD_TO_DICTIONARY:
String word=getWordForDictionary();
if (word != null) {
Intent i=new Intent("String_Node_Str");
i.putExtra("String_Node_Str",word);
i.setFlags(i.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
getContext().startActivity(i);
}
return true;
}
return false;
}

Fixed version (ground truth):
/** 
 * Called when a context menu option for the text view is selected.  Currently this will be one of:   {@link android.R.id#selectAll},  {@link android.R.id#startSelectingText},  {@link android.R.id#cut},   {@link android.R.id#copy},  {@link android.R.id#paste},   {@link android.R.id#copyUrl}, or   {@link android.R.id#switchInputMethod}.
 */
public boolean onTextContextMenuItem(int id){
  int min=0;
  int max=mText.length();
  if (isFocused()) {
    final int selStart=getSelectionStart();
    final int selEnd=getSelectionEnd();
    min=Math.max(0,Math.min(selStart,selEnd));
    max=Math.max(0,Math.max(selStart,selEnd));
  }
  ClipboardManager clip=(ClipboardManager)getContext().getSystemService(Context.CLIPBOARD_SERVICE);
switch (id) {
case ID_SELECT_ALL:
    Selection.setSelection((Spannable)mText,0,mText.length());
  startTextSelectionMode();
getSelectionController().show();
return true;
case ID_START_SELECTING_TEXT:
startTextSelectionMode();
getSelectionController().show();
return true;
case ID_CUT:
clip.setText(mTransformed.subSequence(min,max));
((Editable)mText).delete(min,max);
stopTextSelectionMode();
return true;
case ID_COPY:
clip.setText(mTransformed.subSequence(min,max));
stopTextSelectionMode();
return true;
case ID_PASTE:
CharSequence paste=clip.getText();
if (paste != null && paste.length() > 0) {
long minMax=prepareSpacesAroundPaste(min,max,paste);
min=extractRangeStartFromLong(minMax);
max=extractRangeEndFromLong(minMax);
Selection.setSelection((Spannable)mText,max);
((Editable)mText).replace(min,max,paste);
stopTextSelectionMode();
}
return true;
case ID_COPY_URL:
URLSpan[] urls=((Spanned)mText).getSpans(min,max,URLSpan.class);
if (urls.length == 1) {
clip.setText(urls[0].getURL());
}
return true;
case ID_SWITCH_INPUT_METHOD:
InputMethodManager imm=InputMethodManager.peekInstance();
if (imm != null) {
imm.showInputMethodPicker();
}
return true;
case ID_ADD_TO_DICTIONARY:
String word=getWordForDictionary();
if (word != null) {
Intent i=new Intent("String_Node_Str");
i.putExtra("String_Node_Str",word);
i.setFlags(i.getFlags() | Intent.FLAG_ACTIVITY_NEW_TASK);
getContext().startActivity(i);
}
return true;
}
return false;
}