Program: 239
Project: 239
Buggy source:
public boolean searchAndReplace(Editor editor,DataContext context,LineRange range,String excmd,String exarg){
  boolean res=true;
  CharPointer cmd=new CharPointer(new StringBuffer(exarg));
  int which_pat;
  if (excmd.equals("String_Node_Str"))   which_pat=RE_LAST;
 else   which_pat=RE_SUBST;
  CharPointer pat=null;
  CharPointer sub=null;
  char delimiter;
  if (excmd.charAt(0) == 's' && !cmd.isNul() && !Character.isWhitespace(cmd.charAt()) && "String_Node_Str".indexOf(cmd.charAt()) == -1) {
    if (CharacterClasses.isAlpha(cmd.charAt())) {
      MessageHelper.EMSG(Msg.E146);
      return false;
    }
    if (cmd.charAt() == '\\') {
      cmd.inc();
      if ("String_Node_Str".indexOf(cmd.charAt()) == -1) {
        MessageHelper.EMSG(Msg.e_backslash);
        return false;
      }
      if (cmd.charAt() != '&') {
        which_pat=RE_SEARCH;
      }
      pat=new CharPointer("String_Node_Str");
      delimiter=cmd.charAt();
      cmd.inc();
    }
 else {
      which_pat=RE_LAST;
      delimiter=cmd.charAt();
      cmd.inc();
      pat=cmd.ref(0);
      cmd=RegExp.skip_regexp(cmd,delimiter,true);
      if (cmd.charAt() == delimiter) {
        cmd.set('\u0000').inc();
      }
    }
    sub=cmd.ref(0);
    while (!cmd.isNul()) {
      if (cmd.charAt() == delimiter) {
        cmd.set('\u0000').inc();
        break;
      }
      if (cmd.charAt(0) == '\\' && cmd.charAt(1) != 0) {
        cmd.inc();
      }
      cmd.inc();
    }
  }
 else {
    if (lastReplace == null) {
      MessageHelper.EMSG(Msg.e_nopresub);
      return false;
    }
    pat=null;
    sub=new CharPointer(lastReplace);
  }
  if (cmd.charAt() == '&') {
    cmd.inc();
  }
 else {
    do_all=Options.getInstance().isSet("String_Node_Str");
    do_ask=false;
    do_error=true;
    do_print=false;
    do_ic=0;
  }
  while (!cmd.isNul()) {
    if (cmd.charAt() == 'g')     do_all=!do_all;
 else     if (cmd.charAt() == 'c')     do_ask=!do_ask;
 else     if (cmd.charAt() == 'e')     do_error=!do_error;
 else     if (cmd.charAt() == 'r')     which_pat=RE_LAST;
 else     if (cmd.charAt() == 'p')     do_print=true;
 else     if (cmd.charAt() == 'i')     do_ic='i';
 else     if (cmd.charAt() == 'I')     do_ic='I';
 else     break;
    cmd.inc();
  }
  int line1=range.getStartLine();
  int line2=range.getEndLine();
  cmd=CharHelper.skipwhite(cmd);
  if (CharacterClasses.isDigit(cmd.charAt())) {
    int i=CharHelper.getdigits(cmd);
    if (i <= 0 && do_error) {
      MessageHelper.EMSG(Msg.e_zerocount);
      return false;
    }
    line1=line2;
    line2=EditorHelper.normalizeLine(editor,line1 + i - 1);
  }
  cmd=CharHelper.skipwhite(cmd);
  if (!cmd.isNul() && cmd.charAt() != '"') {
    MessageHelper.EMSG(Msg.e_trailing);
    return false;
  }
  String pattern="String_Node_Str";
  if (pat == null || pat.isNul()) {
switch (which_pat) {
case RE_LAST:
      pattern=lastPattern;
    break;
case RE_SEARCH:
  pattern=lastSearch;
break;
case RE_SUBST:
pattern=lastSubstitute;
break;
}
}
 else {
pattern=pat.toString();
}
lastSubstitute=pattern;
setLastPattern(editor,context,pattern);
int start=editor.logicalPositionToOffset(new LogicalPosition(line1,0));
int end=editor.logicalPositionToOffset(new LogicalPosition(line2,EditorHelper.getLineLength(editor,line2)));
RegExp sp;
RegExp.regmmatch_T regmatch=new RegExp.regmmatch_T();
sp=new RegExp();
regmatch.regprog=sp.vim_regcomp(pattern,1);
if (regmatch.regprog == null) {
if (do_error) {
MessageHelper.EMSG(Msg.e_invcmd);
}
return false;
}
if (do_ic == 'i') {
regmatch.rmm_ic=true;
}
 else if (do_ic == 'I') {
regmatch.rmm_ic=false;
}
if (!(sub.charAt(0) == '\\' && sub.charAt(1) == '=') && lastReplace != null) {
StringBuffer tmp=new StringBuffer(sub.toString());
int pos=0;
while ((pos=tmp.indexOf("String_Node_Str",pos)) != -1) {
if (pos == 0 || tmp.charAt(pos - 1) != '\\') {
tmp.replace(pos,pos + 1,lastReplace);
pos+=lastReplace.length();
}
pos++;
}
sub=new CharPointer(tmp);
}
lastReplace=sub.toString();
logger.debug("String_Node_Str" + start + "String_Node_Str"+ end+ "String_Node_Str");
logger.debug("String_Node_Str" + pattern + "String_Node_Str"+ sub);
int lastMatch=-1;
boolean found=true;
int lastLine=-1;
int searchcol=0;
boolean firstMatch=true;
boolean got_quit=false;
for (int lnum=line1; lnum <= line2 && !got_quit; ) {
LogicalPosition newpos=null;
int nmatch=sp.vim_regexec_multi(regmatch,editor,lnum,searchcol);
found=nmatch > 0;
if (found) {
if (firstMatch) {
CommandGroups.getInstance().getMark().saveJumpLocation(editor,context);
firstMatch=false;
}
String match=sp.vim_regsub_multi(regmatch,lnum,sub,1,false);
int line=lnum + regmatch.startpos[0].lnum;
LogicalPosition startpos=new LogicalPosition(lnum + regmatch.startpos[0].lnum,regmatch.startpos[0].col);
LogicalPosition endpos=new LogicalPosition(lnum + regmatch.endpos[0].lnum,regmatch.endpos[0].col);
int startoff=editor.logicalPositionToOffset(startpos);
int endoff=editor.logicalPositionToOffset(endpos);
int newend=startoff + match.length();
if (do_all || line != lastLine) {
boolean doReplace=true;
if (do_ask) {
RangeHighlighter hl=highlightMatch(editor,startoff,endoff);
int choice=JOptionPane.showOptionDialog(null,"String_Node_Str" + match + "String_Node_Str","String_Node_Str",JOptionPane.DEFAULT_OPTION,JOptionPane.QUESTION_MESSAGE,null,getConfirmButtons(),null);
editor.getMarkupModel().removeHighlighter(hl);
switch (choice) {
case 0:
doReplace=true;
break;
case 1:
doReplace=false;
break;
case 2:
do_ask=false;
break;
case JOptionPane.CLOSED_OPTION:
case 3:
found=false;
doReplace=false;
got_quit=true;
break;
case 4:
do_all=false;
line2=lnum;
found=false;
doReplace=true;
break;
}
}
if (doReplace) {
editor.getDocument().replaceString(startoff,endoff,match);
lastMatch=startoff;
newpos=editor.offsetToLogicalPosition(newend);
int diff=newpos.line - endpos.line;
line2+=diff;
}
}
lastLine=line;
lnum+=nmatch - 1;
if (do_all) {
if (newpos != null) searchcol=newpos.column;
 else searchcol=endpos.column;
}
 else {
searchcol=0;
lnum++;
}
}
 else {
lnum++;
searchcol=0;
}
}
if (lastMatch != -1) {
MotionGroup.moveCaret(editor,context,CommandGroups.getInstance().getMotion().moveCaretToLineStartSkipLeading(editor,editor.offsetToLogicalPosition(lastMatch).line));
}
 else {
MessageHelper.EMSG(Msg.e_patnotf2,pattern);
}
return res;
}

Fixed version (ground truth):
public boolean searchAndReplace(Editor editor,DataContext context,LineRange range,String excmd,String exarg){
  boolean res=true;
  CharPointer cmd=new CharPointer(new StringBuffer(exarg));
  int which_pat;
  if (excmd.equals("String_Node_Str"))   which_pat=RE_LAST;
 else   which_pat=RE_SUBST;
  CharPointer pat=null;
  CharPointer sub=null;
  char delimiter;
  if (excmd.charAt(0) == 's' && !cmd.isNul() && !Character.isWhitespace(cmd.charAt()) && "String_Node_Str".indexOf(cmd.charAt()) == -1) {
    if (CharacterClasses.isAlpha(cmd.charAt())) {
      MessageHelper.EMSG(Msg.E146);
      return false;
    }
    if (cmd.charAt() == '\\') {
      cmd.inc();
      if ("String_Node_Str".indexOf(cmd.charAt()) == -1) {
        MessageHelper.EMSG(Msg.e_backslash);
        return false;
      }
      if (cmd.charAt() != '&') {
        which_pat=RE_SEARCH;
      }
      pat=new CharPointer("String_Node_Str");
      delimiter=cmd.charAt();
      cmd.inc();
    }
 else {
      which_pat=RE_LAST;
      delimiter=cmd.charAt();
      cmd.inc();
      pat=cmd.ref(0);
      cmd=RegExp.skip_regexp(cmd,delimiter,true);
      if (cmd.charAt() == delimiter) {
        cmd.set('\u0000').inc();
      }
    }
    sub=cmd.ref(0);
    while (!cmd.isNul()) {
      if (cmd.charAt() == delimiter) {
        cmd.set('\u0000').inc();
        break;
      }
      if (cmd.charAt(0) == '\\' && cmd.charAt(1) != 0) {
        cmd.inc();
      }
      cmd.inc();
    }
  }
 else {
    if (lastReplace == null) {
      MessageHelper.EMSG(Msg.e_nopresub);
      return false;
    }
    pat=null;
    sub=new CharPointer(lastReplace);
  }
  if (cmd.charAt() == '&') {
    cmd.inc();
  }
 else {
    do_all=Options.getInstance().isSet("String_Node_Str");
    do_ask=false;
    do_error=true;
    do_print=false;
    do_ic=0;
  }
  while (!cmd.isNul()) {
    if (cmd.charAt() == 'g')     do_all=!do_all;
 else     if (cmd.charAt() == 'c')     do_ask=!do_ask;
 else     if (cmd.charAt() == 'e')     do_error=!do_error;
 else     if (cmd.charAt() == 'r')     which_pat=RE_LAST;
 else     if (cmd.charAt() == 'p')     do_print=true;
 else     if (cmd.charAt() == 'i')     do_ic='i';
 else     if (cmd.charAt() == 'I')     do_ic='I';
 else     break;
    cmd.inc();
  }
  int line1=range.getStartLine();
  int line2=range.getEndLine();
  cmd=CharHelper.skipwhite(cmd);
  if (CharacterClasses.isDigit(cmd.charAt())) {
    int i=CharHelper.getdigits(cmd);
    if (i <= 0 && do_error) {
      MessageHelper.EMSG(Msg.e_zerocount);
      return false;
    }
    line1=line2;
    line2=EditorHelper.normalizeLine(editor,line1 + i - 1);
  }
  cmd=CharHelper.skipwhite(cmd);
  if (!cmd.isNul() && cmd.charAt() != '"') {
    MessageHelper.EMSG(Msg.e_trailing);
    return false;
  }
  String pattern="String_Node_Str";
  if (pat == null || pat.isNul()) {
switch (which_pat) {
case RE_LAST:
      pattern=lastPattern;
    break;
case RE_SEARCH:
  pattern=lastSearch;
break;
case RE_SUBST:
pattern=lastSubstitute;
break;
}
}
 else {
pattern=pat.toString();
}
lastSubstitute=pattern;
setLastPattern(editor,context,pattern);
int start=editor.getDocument().getLineStartOffset(line1);
int end=editor.getDocument().getLineEndOffset(line2);
RegExp sp;
RegExp.regmmatch_T regmatch=new RegExp.regmmatch_T();
sp=new RegExp();
regmatch.regprog=sp.vim_regcomp(pattern,1);
if (regmatch.regprog == null) {
if (do_error) {
MessageHelper.EMSG(Msg.e_invcmd);
}
return false;
}
if (do_ic == 'i') {
regmatch.rmm_ic=true;
}
 else if (do_ic == 'I') {
regmatch.rmm_ic=false;
}
if (!(sub.charAt(0) == '\\' && sub.charAt(1) == '=') && lastReplace != null) {
StringBuffer tmp=new StringBuffer(sub.toString());
int pos=0;
while ((pos=tmp.indexOf("String_Node_Str",pos)) != -1) {
if (pos == 0 || tmp.charAt(pos - 1) != '\\') {
tmp.replace(pos,pos + 1,lastReplace);
pos+=lastReplace.length();
}
pos++;
}
sub=new CharPointer(tmp);
}
lastReplace=sub.toString();
logger.debug("String_Node_Str" + start + "String_Node_Str"+ end+ "String_Node_Str");
logger.debug("String_Node_Str" + pattern + "String_Node_Str"+ sub);
int lastMatch=-1;
boolean found=true;
int lastLine=-1;
int searchcol=0;
boolean firstMatch=true;
boolean got_quit=false;
for (int lnum=line1; lnum <= line2 && !got_quit; ) {
CharacterPosition newpos=null;
int nmatch=sp.vim_regexec_multi(regmatch,editor,lnum,searchcol);
found=nmatch > 0;
if (found) {
if (firstMatch) {
CommandGroups.getInstance().getMark().saveJumpLocation(editor,context);
firstMatch=false;
}
String match=sp.vim_regsub_multi(regmatch,lnum,sub,1,false);
int line=lnum + regmatch.startpos[0].lnum;
CharacterPosition startpos=new CharacterPosition(lnum + regmatch.startpos[0].lnum,regmatch.startpos[0].col);
CharacterPosition endpos=new CharacterPosition(lnum + regmatch.endpos[0].lnum,regmatch.endpos[0].col);
int startoff=EditorHelper.characterPositionToOffset(editor,startpos);
int endoff=EditorHelper.characterPositionToOffset(editor,endpos);
int newend=startoff + match.length();
if (do_all || line != lastLine) {
boolean doReplace=true;
if (do_ask) {
RangeHighlighter hl=highlightMatch(editor,startoff,endoff);
int choice=JOptionPane.showOptionDialog(null,"String_Node_Str" + match + "String_Node_Str","String_Node_Str",JOptionPane.DEFAULT_OPTION,JOptionPane.QUESTION_MESSAGE,null,getConfirmButtons(),null);
editor.getMarkupModel().removeHighlighter(hl);
switch (choice) {
case 0:
doReplace=true;
break;
case 1:
doReplace=false;
break;
case 2:
do_ask=false;
break;
case JOptionPane.CLOSED_OPTION:
case 3:
found=false;
doReplace=false;
got_quit=true;
break;
case 4:
do_all=false;
line2=lnum;
found=false;
doReplace=true;
break;
}
}
if (doReplace) {
editor.getDocument().replaceString(startoff,endoff,match);
lastMatch=startoff;
newpos=EditorHelper.offsetToCharacterPosition(editor,newend);
int diff=newpos.line - endpos.line;
line2+=diff;
}
}
lastLine=line;
lnum+=nmatch - 1;
if (do_all) {
if (newpos != null) searchcol=newpos.column;
 else searchcol=endpos.column;
}
 else {
searchcol=0;
lnum++;
}
}
 else {
lnum++;
searchcol=0;
}
}
if (lastMatch != -1) {
MotionGroup.moveCaret(editor,context,CommandGroups.getInstance().getMotion().moveCaretToLineStartSkipLeading(editor,editor.offsetToLogicalPosition(lastMatch).line));
}
 else {
MessageHelper.EMSG(Msg.e_patnotf2,pattern);
}
return res;
}