Program: 187789
Project: 187789
Buggy source:
/** 
 * Insert textToInsert at current position. Optionally, if textBefore or textAfter are non-null, replace the text before or after the current selection.
 * @author isaac
 * @author Peli
 */
private void insertAtPoint(String textBefore,String textToInsert,String textAfter){
  String originalText=mText.getText().toString();
  int startPos=mText.getSelectionStart();
  int endPos=mText.getSelectionEnd();
  int newStartPos=startPos;
  int newEndPos=endPos;
  ContentValues values=new ContentValues();
  String newNote="String_Node_Str";
  StringBuffer sb=new StringBuffer();
  if (textBefore != null) {
    sb.append(textBefore);
    newStartPos=textBefore.length();
  }
 else {
    sb.append(originalText.substring(0,startPos));
  }
  if (textToInsert != null) {
    sb.append(textToInsert);
    newEndPos=newStartPos + textToInsert.length();
  }
 else {
    String text=originalText.substring(startPos,endPos);
    sb.append(text);
    newEndPos=newStartPos + text.length();
  }
  if (textAfter != null) {
    sb.append(textAfter);
  }
 else {
    sb.append(originalText.substring(endPos));
  }
  newNote=sb.toString();
  if (mState == STATE_EDIT_NOTE_FROM_SDCARD) {
    mFileContent=newNote;
    mSelectionStart=newStartPos;
    mSelectionStop=newEndPos;
  }
 else {
    if (!mNoteOnly) {
      values.put(Notes.MODIFIED_DATE,System.currentTimeMillis());
      String title=ExtractTitle.extractTitle(newNote);
      values.put(Notes.TITLE,title);
    }
    values.put(Notes.NOTE,newNote);
    getContentResolver().update(mUri,values,null,null);
  }
  mText.setTextKeepState(newNote);
  mText.setSelection(newStartPos,newEndPos);
}

Fixed version (ground truth):
/** 
 * Insert textToInsert at current position. Optionally, if textBefore or textAfter are non-null, replace the text before or after the current selection.
 * @author isaac
 * @author Peli
 */
private void insertAtPoint(String textBefore,String textToInsert,String textAfter){
  String originalText=mText.getText().toString();
  int startPos=mText.getSelectionStart();
  int endPos=mText.getSelectionEnd();
  if (mDecryptedText != null) {
    originalText=mDecryptedText;
    startPos=mSelectionStart;
    endPos=mSelectionStop;
  }
  int newStartPos=startPos;
  int newEndPos=endPos;
  ContentValues values=new ContentValues();
  String newNote="String_Node_Str";
  StringBuffer sb=new StringBuffer();
  if (textBefore != null) {
    sb.append(textBefore);
    newStartPos=textBefore.length();
  }
 else {
    sb.append(originalText.substring(0,startPos));
  }
  if (textToInsert != null) {
    sb.append(textToInsert);
    newEndPos=newStartPos + textToInsert.length();
  }
 else {
    String text=originalText.substring(startPos,endPos);
    sb.append(text);
    newEndPos=newStartPos + text.length();
  }
  if (textAfter != null) {
    sb.append(textAfter);
  }
 else {
    sb.append(originalText.substring(endPos));
  }
  newNote=sb.toString();
  if (mState == STATE_EDIT_NOTE_FROM_SDCARD) {
    mFileContent=newNote;
    mSelectionStart=newStartPos;
    mSelectionStop=newEndPos;
  }
 else   if (mDecryptedText != null) {
    mDecryptedText=newNote;
  }
 else {
    if (!mNoteOnly) {
      values.put(Notes.MODIFIED_DATE,System.currentTimeMillis());
      String title=ExtractTitle.extractTitle(newNote);
      values.put(Notes.TITLE,title);
    }
    values.put(Notes.NOTE,newNote);
    getContentResolver().update(mUri,values,null,null);
  }
  mText.setTextKeepState(newNote);
  mText.setSelection(newStartPos,newEndPos);
}