Program: 42261
Project: 42261
Buggy source:
private XValue parseXExpr(Shell shell,String arg) throws CoreException {
  mLogger.entry(shell,arg);
  Processor processor=Shell.getProcessor();
  XQueryCompiler compiler=processor.newXQueryCompiler();
  compiler.setModuleURIResolver(new ShellModuleURIResolver(mShell));
  compiler.declareNamespace("String_Node_Str",EvalDefinition.kXMLSH_EXT_NAMESPACE);
  NameValueMap<String> ns=shell.getEnv().getNamespaces();
  if (ns != null) {
    for (    String prefix : ns.keySet()) {
      String uri=ns.get(prefix);
      compiler.declareNamespace(prefix,uri);
    }
  }
  List<XValue> args=shell.getArgs();
  XQueryExecutable expr=null;
  StringBuffer sb=new StringBuffer();
  Variables variables=shell.getEnv().getVars();
  Collection<String> varnames=variables.getVarNames();
  NameValueMap<XdmValue> usedVars=new NameValueMap<XdmValue>(args.size() + varnames.size());
  for (  String name : varnames) {
    XVariable var=variables.get(name);
    XdmValue xdmValue=convertVar(var);
    if (xdmValue != null) {
      usedVars.put(var.getName(),xdmValue);
      sb.append("String_Node_Str").append(name).append("String_Node_Str");
    }
  }
  int i=0;
  for (  XValue xv : args) {
    i++;
    XdmValue xdmValue=convertValue(xv);
    if (xdmValue != null) {
      String name="String_Node_Str" + i;
      usedVars.put(name,xdmValue);
      sb.append("String_Node_Str" + name + "String_Node_Str");
    }
  }
  sb.append(arg);
  Shell saved_shell=ThreadLocalShell.set(shell);
  try {
    expr=compiler.compile(sb.toString());
    XQueryEvaluator eval=expr.load();
    for (    Entry<String,XdmValue> entry : usedVars.entrySet()) {
      XdmValue v=entry.getValue();
      eval.setExternalVariable(new QName(entry.getKey()),v);
    }
    XdmValue result=eval.evaluate();
    return mLogger.exit(XDMTypeFamily.getInstance().getXValue(result));
  }
 catch (  SaxonApiException e) {
    String msg="String_Node_Str" + arg;
    mLogger.warn(msg,e);
    mShell.printErr(msg,e);
    throw new CoreException(msg,e);
  }
 finally {
    ThreadLocalShell.set(saved_shell);
  }
}

Fixed version (ground truth):
private XValue parseXExpr(Shell shell,String arg) throws CoreException {
  mLogger.entry(shell,arg);
  Processor processor=Shell.getProcessor();
  XQueryCompiler compiler=processor.newXQueryCompiler();
  compiler.setModuleURIResolver(new ShellModuleURIResolver(mShell));
  compiler.declareNamespace("String_Node_Str",EvalDefinition.kXMLSH_EXT_NAMESPACE);
  NameValueMap<String> ns=shell.getEnv().getNamespaces();
  if (ns != null) {
    for (    String prefix : ns.keySet()) {
      String uri=ns.get(prefix);
      compiler.declareNamespace(prefix,uri);
    }
  }
  List<XValue> args=shell.getArgs();
  XQueryExecutable expr=null;
  StringBuffer sb=new StringBuffer();
  Variables variables=shell.getEnv().getVars();
  Collection<String> varnames=variables.getVarNames();
  NameValueMap<XdmValue> usedVars=new NameValueMap<XdmValue>();
  for (  String name : varnames) {
    XVariable var=variables.get(name);
    XdmValue xdmValue=convertVar(var);
    if (xdmValue != null) {
      usedVars.put(var.getName(),xdmValue);
      sb.append("String_Node_Str").append(name).append("String_Node_Str");
    }
  }
  int i=0;
  for (  XValue xv : args) {
    i++;
    XdmValue xdmValue=convertValue(xv);
    if (xdmValue != null) {
      String name="String_Node_Str" + i;
      usedVars.put(name,xdmValue);
      sb.append("String_Node_Str" + name + "String_Node_Str");
    }
  }
  sb.append(arg);
  Shell saved_shell=ThreadLocalShell.set(shell);
  try {
    expr=compiler.compile(sb.toString());
    XQueryEvaluator eval=expr.load();
    for (    Entry<String,XdmValue> entry : usedVars.entrySet()) {
      XdmValue v=entry.getValue();
      eval.setExternalVariable(new QName(entry.getKey()),v);
    }
    XdmValue result=eval.evaluate();
    return mLogger.exit(XDMTypeFamily.getInstance().getXValue(result));
  }
 catch (  SaxonApiException e) {
    String msg="String_Node_Str" + arg;
    mLogger.warn(msg,e);
    mShell.printErr(msg,e);
    throw new CoreException(msg,e);
  }
 finally {
    ThreadLocalShell.set(saved_shell);
  }
}