Program: 61919
Project: 61919
Buggy source:
private static boolean addToContextMenu(boolean add,String exeName){
  final String extensions[]=new String[]{"String_Node_Str","String_Node_Str"};
  final WinReg.HKEY REG_CLASSES_HKEY=WinReg.HKEY_LOCAL_MACHINE;
  final String REG_CLASSES_PATH="String_Node_Str";
  String appDir=getAppDir();
  String verb="String_Node_Str";
  String verbName="String_Node_Str";
  boolean exists;
  try {
    exists=Advapi32Util.registryKeyExists(REG_CLASSES_HKEY,REG_CLASSES_PATH + "String_Node_Str" + exeName);
    if ((!exists) && add) {
      Advapi32Util.registryCreateKey(REG_CLASSES_HKEY,REG_CLASSES_PATH + "String_Node_Str" + exeName);
      Advapi32Util.registryCreateKey(REG_CLASSES_HKEY,REG_CLASSES_PATH + "String_Node_Str" + exeName+ "String_Node_Str");
      Advapi32Util.registryCreateKey(REG_CLASSES_HKEY,REG_CLASSES_PATH + "String_Node_Str" + exeName+ "String_Node_Str");
      Advapi32Util.registrySetStringValue(REG_CLASSES_HKEY,REG_CLASSES_PATH + "String_Node_Str" + exeName+ "String_Node_Str","String_Node_Str",verbName);
      Advapi32Util.registryCreateKey(REG_CLASSES_HKEY,REG_CLASSES_PATH + "String_Node_Str" + exeName+ "String_Node_Str");
      Advapi32Util.registrySetStringValue(REG_CLASSES_HKEY,REG_CLASSES_PATH + "String_Node_Str" + exeName+ "String_Node_Str","String_Node_Str","String_Node_Str" + appDir + exeName+ "String_Node_Str");
    }
    for (    String ext : extensions) {
      if (!Advapi32Util.registryKeyExists(REG_CLASSES_HKEY,REG_CLASSES_PATH + "String_Node_Str" + ext)) {
        Advapi32Util.registryCreateKey(REG_CLASSES_HKEY,REG_CLASSES_PATH + "String_Node_Str" + ext);
        Advapi32Util.registrySetStringValue(REG_CLASSES_HKEY,REG_CLASSES_PATH + "String_Node_Str" + ext,"String_Node_Str","String_Node_Str");
      }
      String clsName=Advapi32Util.registryGetStringValue(REG_CLASSES_HKEY,REG_CLASSES_PATH + "String_Node_Str" + ext,"String_Node_Str");
      if (!Advapi32Util.registryKeyExists(REG_CLASSES_HKEY,REG_CLASSES_PATH + clsName)) {
        Advapi32Util.registryCreateKey(REG_CLASSES_HKEY,REG_CLASSES_PATH + clsName);
        Advapi32Util.registrySetStringValue(REG_CLASSES_HKEY,REG_CLASSES_PATH + clsName,"String_Node_Str","String_Node_Str");
      }
      if (!Advapi32Util.registryKeyExists(REG_CLASSES_HKEY,REG_CLASSES_PATH + clsName + "String_Node_Str")) {
        Advapi32Util.registryCreateKey(REG_CLASSES_HKEY,REG_CLASSES_PATH + clsName + "String_Node_Str");
      }
      exists=Advapi32Util.registryKeyExists(REG_CLASSES_HKEY,REG_CLASSES_PATH + clsName + "String_Node_Str"+ verb);
      if ((!exists) && add) {
        Advapi32Util.registryCreateKey(REG_CLASSES_HKEY,REG_CLASSES_PATH + clsName + "String_Node_Str"+ verb);
        Advapi32Util.registrySetStringValue(REG_CLASSES_HKEY,REG_CLASSES_PATH + clsName + "String_Node_Str"+ verb,"String_Node_Str",verbName);
        Advapi32Util.registryCreateKey(REG_CLASSES_HKEY,REG_CLASSES_PATH + clsName + "String_Node_Str"+ verb+ "String_Node_Str");
        Advapi32Util.registrySetStringValue(REG_CLASSES_HKEY,REG_CLASSES_PATH + clsName + "String_Node_Str"+ verb+ "String_Node_Str","String_Node_Str","String_Node_Str" + appDir + exeName+ "String_Node_Str");
      }
      if (exists && (!add)) {
        registryDeleteKey(REG_CLASSES_HKEY,REG_CLASSES_PATH + clsName + "String_Node_Str"+ verb+ "String_Node_Str");
        registryDeleteKey(REG_CLASSES_HKEY,REG_CLASSES_PATH + clsName + "String_Node_Str"+ verb);
      }
      exists=Advapi32Util.registryKeyExists(REG_CLASSES_HKEY,REG_CLASSES_PATH + "String_Node_Str" + exeName);
      if (exists && (!add)) {
        registryDeleteKey(REG_CLASSES_HKEY,REG_CLASSES_PATH + "String_Node_Str" + exeName+ "String_Node_Str");
        registryDeleteKey(REG_CLASSES_HKEY,REG_CLASSES_PATH + "String_Node_Str" + exeName+ "String_Node_Str");
        registryDeleteKey(REG_CLASSES_HKEY,REG_CLASSES_PATH + "String_Node_Str" + exeName+ "String_Node_Str");
        registryDeleteKey(REG_CLASSES_HKEY,REG_CLASSES_PATH + "String_Node_Str" + exeName);
      }
      if (Advapi32Util.registryValueExists(WinReg.HKEY_CURRENT_USER,"String_Node_Str" + ext + "String_Node_Str","String_Node_Str")) {
        String mruList=Advapi32Util.registryGetStringValue(WinReg.HKEY_CURRENT_USER,"String_Node_Str" + ext + "String_Node_Str","String_Node_Str");
        if (mruList != null) {
          exists=false;
          char appChar=0;
          for (int i=0; i < mruList.length(); i++) {
            String app=Advapi32Util.registryGetStringValue(WinReg.HKEY_CURRENT_USER,"String_Node_Str" + ext + "String_Node_Str","String_Node_Str" + mruList.charAt(i));
            if (app.equals(exeName)) {
              appChar=mruList.charAt(i);
              exists=true;
              break;
            }
          }
          if ((!exists) && add) {
            for (int c='a'; c <= 'z'; c++) {
              if (mruList.indexOf(c) == -1) {
                mruList+=(char)c;
                Advapi32Util.registrySetStringValue(WinReg.HKEY_CURRENT_USER,"String_Node_Str" + ext + "String_Node_Str","String_Node_Str" + (char)c,exeName);
                Advapi32Util.registrySetStringValue(WinReg.HKEY_CURRENT_USER,"String_Node_Str" + ext + "String_Node_Str","String_Node_Str",mruList);
                break;
              }
            }
          }
          if (exists && (!add)) {
            mruList=mruList.replace("String_Node_Str" + appChar,"String_Node_Str");
            Advapi32Util.registrySetStringValue(WinReg.HKEY_CURRENT_USER,"String_Node_Str" + ext + "String_Node_Str","String_Node_Str",mruList);
            registryDeleteValue(WinReg.HKEY_CURRENT_USER,"String_Node_Str" + ext + "String_Node_Str","String_Node_Str" + appChar);
          }
        }
      }
      if (Advapi32Util.registryKeyExists(REG_CLASSES_HKEY,REG_CLASSES_PATH + "String_Node_Str")) {
        exists=Advapi32Util.registryKeyExists(REG_CLASSES_HKEY,REG_CLASSES_PATH + "String_Node_Str" + ext+ "String_Node_Str"+ verb);
        if ((!exists) && add) {
          if (!Advapi32Util.registryKeyExists(REG_CLASSES_HKEY,REG_CLASSES_PATH + "String_Node_Str" + ext+ "String_Node_Str")) {
            Advapi32Util.registryCreateKey(REG_CLASSES_HKEY,REG_CLASSES_PATH + "String_Node_Str" + ext+ "String_Node_Str");
          }
          if (!Advapi32Util.registryKeyExists(REG_CLASSES_HKEY,REG_CLASSES_PATH + "String_Node_Str" + ext+ "String_Node_Str")) {
            Advapi32Util.registryCreateKey(REG_CLASSES_HKEY,REG_CLASSES_PATH + "String_Node_Str" + ext+ "String_Node_Str");
          }
          Advapi32Util.registryCreateKey(REG_CLASSES_HKEY,REG_CLASSES_PATH + "String_Node_Str" + ext+ "String_Node_Str"+ verb);
          Advapi32Util.registrySetStringValue(REG_CLASSES_HKEY,REG_CLASSES_PATH + "String_Node_Str" + ext+ "String_Node_Str"+ verb,"String_Node_Str",verbName);
          Advapi32Util.registryCreateKey(REG_CLASSES_HKEY,REG_CLASSES_PATH + "String_Node_Str" + ext+ "String_Node_Str"+ verb+ "String_Node_Str");
          Advapi32Util.registrySetStringValue(REG_CLASSES_HKEY,REG_CLASSES_PATH + "String_Node_Str" + ext+ "String_Node_Str"+ verb+ "String_Node_Str","String_Node_Str","String_Node_Str" + appDir + exeName+ "String_Node_Str");
        }
        if (exists && (!add)) {
          registryDeleteKey(REG_CLASSES_HKEY,REG_CLASSES_PATH + "String_Node_Str" + ext+ "String_Node_Str"+ verb+ "String_Node_Str");
          registryDeleteKey(REG_CLASSES_HKEY,REG_CLASSES_PATH + "String_Node_Str" + ext+ "String_Node_Str"+ verb);
        }
      }
    }
    return true;
  }
 catch (  Exception ex) {
    SHELLEXECUTEINFO sei=new SHELLEXECUTEINFO();
    sei.fMask=0x00000040;
    sei.lpVerb=new WString("String_Node_Str");
    sei.lpFile=new WString(appDir + exeName);
    sei.lpParameters=new WString(add ? "String_Node_Str" : "String_Node_Str");
    sei.nShow=WinUser.SW_NORMAL;
    Shell32.INSTANCE.ShellExecuteEx(sei);
    Kernel32.INSTANCE.WaitForSingleObject(sei.hProcess,1000 * 60 * 60* 24);
  }
  return false;
}

Fixed version (ground truth):
private static boolean addToContextMenu(boolean add,boolean fromCommandLine,String exeName){
  final String extensions[]=new String[]{"String_Node_Str","String_Node_Str"};
  final WinReg.HKEY REG_CLASSES_HKEY=WinReg.HKEY_LOCAL_MACHINE;
  final String REG_CLASSES_PATH="String_Node_Str";
  String appDir=getAppDir();
  String verb="String_Node_Str";
  String verbName="String_Node_Str";
  boolean exists;
  try {
    exists=Advapi32Util.registryKeyExists(REG_CLASSES_HKEY,REG_CLASSES_PATH + "String_Node_Str" + exeName);
    if ((!exists) && add) {
      Advapi32Util.registryCreateKey(REG_CLASSES_HKEY,REG_CLASSES_PATH + "String_Node_Str" + exeName);
      Advapi32Util.registryCreateKey(REG_CLASSES_HKEY,REG_CLASSES_PATH + "String_Node_Str" + exeName+ "String_Node_Str");
      Advapi32Util.registryCreateKey(REG_CLASSES_HKEY,REG_CLASSES_PATH + "String_Node_Str" + exeName+ "String_Node_Str");
      Advapi32Util.registrySetStringValue(REG_CLASSES_HKEY,REG_CLASSES_PATH + "String_Node_Str" + exeName+ "String_Node_Str","String_Node_Str",verbName);
      Advapi32Util.registryCreateKey(REG_CLASSES_HKEY,REG_CLASSES_PATH + "String_Node_Str" + exeName+ "String_Node_Str");
      Advapi32Util.registrySetStringValue(REG_CLASSES_HKEY,REG_CLASSES_PATH + "String_Node_Str" + exeName+ "String_Node_Str","String_Node_Str","String_Node_Str" + appDir + exeName+ "String_Node_Str");
    }
    for (    String ext : extensions) {
      if (!Advapi32Util.registryKeyExists(REG_CLASSES_HKEY,REG_CLASSES_PATH + "String_Node_Str" + ext)) {
        Advapi32Util.registryCreateKey(REG_CLASSES_HKEY,REG_CLASSES_PATH + "String_Node_Str" + ext);
        Advapi32Util.registrySetStringValue(REG_CLASSES_HKEY,REG_CLASSES_PATH + "String_Node_Str" + ext,"String_Node_Str","String_Node_Str");
      }
      String clsName=Advapi32Util.registryGetStringValue(REG_CLASSES_HKEY,REG_CLASSES_PATH + "String_Node_Str" + ext,"String_Node_Str");
      if (!Advapi32Util.registryKeyExists(REG_CLASSES_HKEY,REG_CLASSES_PATH + clsName)) {
        Advapi32Util.registryCreateKey(REG_CLASSES_HKEY,REG_CLASSES_PATH + clsName);
        Advapi32Util.registrySetStringValue(REG_CLASSES_HKEY,REG_CLASSES_PATH + clsName,"String_Node_Str","String_Node_Str");
      }
      if (!Advapi32Util.registryKeyExists(REG_CLASSES_HKEY,REG_CLASSES_PATH + clsName + "String_Node_Str")) {
        Advapi32Util.registryCreateKey(REG_CLASSES_HKEY,REG_CLASSES_PATH + clsName + "String_Node_Str");
      }
      exists=Advapi32Util.registryKeyExists(REG_CLASSES_HKEY,REG_CLASSES_PATH + clsName + "String_Node_Str"+ verb);
      if ((!exists) && add) {
        Advapi32Util.registryCreateKey(REG_CLASSES_HKEY,REG_CLASSES_PATH + clsName + "String_Node_Str"+ verb);
        Advapi32Util.registrySetStringValue(REG_CLASSES_HKEY,REG_CLASSES_PATH + clsName + "String_Node_Str"+ verb,"String_Node_Str",verbName);
        Advapi32Util.registryCreateKey(REG_CLASSES_HKEY,REG_CLASSES_PATH + clsName + "String_Node_Str"+ verb+ "String_Node_Str");
        Advapi32Util.registrySetStringValue(REG_CLASSES_HKEY,REG_CLASSES_PATH + clsName + "String_Node_Str"+ verb+ "String_Node_Str","String_Node_Str","String_Node_Str" + appDir + exeName+ "String_Node_Str");
      }
      if (exists && (!add)) {
        registryDeleteKey(REG_CLASSES_HKEY,REG_CLASSES_PATH + clsName + "String_Node_Str"+ verb+ "String_Node_Str");
        registryDeleteKey(REG_CLASSES_HKEY,REG_CLASSES_PATH + clsName + "String_Node_Str"+ verb);
      }
      exists=Advapi32Util.registryKeyExists(REG_CLASSES_HKEY,REG_CLASSES_PATH + "String_Node_Str" + exeName);
      if (exists && (!add)) {
        registryDeleteKey(REG_CLASSES_HKEY,REG_CLASSES_PATH + "String_Node_Str" + exeName+ "String_Node_Str");
        registryDeleteKey(REG_CLASSES_HKEY,REG_CLASSES_PATH + "String_Node_Str" + exeName+ "String_Node_Str");
        registryDeleteKey(REG_CLASSES_HKEY,REG_CLASSES_PATH + "String_Node_Str" + exeName+ "String_Node_Str");
        registryDeleteKey(REG_CLASSES_HKEY,REG_CLASSES_PATH + "String_Node_Str" + exeName);
      }
      if (Advapi32Util.registryValueExists(WinReg.HKEY_CURRENT_USER,"String_Node_Str" + ext + "String_Node_Str","String_Node_Str")) {
        String mruList=Advapi32Util.registryGetStringValue(WinReg.HKEY_CURRENT_USER,"String_Node_Str" + ext + "String_Node_Str","String_Node_Str");
        if (mruList != null) {
          exists=false;
          char appChar=0;
          for (int i=0; i < mruList.length(); i++) {
            String app=Advapi32Util.registryGetStringValue(WinReg.HKEY_CURRENT_USER,"String_Node_Str" + ext + "String_Node_Str","String_Node_Str" + mruList.charAt(i));
            if (app.equals(exeName)) {
              appChar=mruList.charAt(i);
              exists=true;
              break;
            }
          }
          if ((!exists) && add) {
            for (int c='a'; c <= 'z'; c++) {
              if (mruList.indexOf(c) == -1) {
                mruList+=(char)c;
                Advapi32Util.registrySetStringValue(WinReg.HKEY_CURRENT_USER,"String_Node_Str" + ext + "String_Node_Str","String_Node_Str" + (char)c,exeName);
                Advapi32Util.registrySetStringValue(WinReg.HKEY_CURRENT_USER,"String_Node_Str" + ext + "String_Node_Str","String_Node_Str",mruList);
                break;
              }
            }
          }
          if (exists && (!add)) {
            mruList=mruList.replace("String_Node_Str" + appChar,"String_Node_Str");
            Advapi32Util.registrySetStringValue(WinReg.HKEY_CURRENT_USER,"String_Node_Str" + ext + "String_Node_Str","String_Node_Str",mruList);
            registryDeleteValue(WinReg.HKEY_CURRENT_USER,"String_Node_Str" + ext + "String_Node_Str","String_Node_Str" + appChar);
          }
        }
      }
      if (Advapi32Util.registryKeyExists(REG_CLASSES_HKEY,REG_CLASSES_PATH + "String_Node_Str")) {
        exists=Advapi32Util.registryKeyExists(REG_CLASSES_HKEY,REG_CLASSES_PATH + "String_Node_Str" + ext+ "String_Node_Str"+ verb);
        if ((!exists) && add) {
          if (!Advapi32Util.registryKeyExists(REG_CLASSES_HKEY,REG_CLASSES_PATH + "String_Node_Str" + ext+ "String_Node_Str")) {
            Advapi32Util.registryCreateKey(REG_CLASSES_HKEY,REG_CLASSES_PATH + "String_Node_Str" + ext+ "String_Node_Str");
          }
          if (!Advapi32Util.registryKeyExists(REG_CLASSES_HKEY,REG_CLASSES_PATH + "String_Node_Str" + ext+ "String_Node_Str")) {
            Advapi32Util.registryCreateKey(REG_CLASSES_HKEY,REG_CLASSES_PATH + "String_Node_Str" + ext+ "String_Node_Str");
          }
          Advapi32Util.registryCreateKey(REG_CLASSES_HKEY,REG_CLASSES_PATH + "String_Node_Str" + ext+ "String_Node_Str"+ verb);
          Advapi32Util.registrySetStringValue(REG_CLASSES_HKEY,REG_CLASSES_PATH + "String_Node_Str" + ext+ "String_Node_Str"+ verb,"String_Node_Str",verbName);
          Advapi32Util.registryCreateKey(REG_CLASSES_HKEY,REG_CLASSES_PATH + "String_Node_Str" + ext+ "String_Node_Str"+ verb+ "String_Node_Str");
          Advapi32Util.registrySetStringValue(REG_CLASSES_HKEY,REG_CLASSES_PATH + "String_Node_Str" + ext+ "String_Node_Str"+ verb+ "String_Node_Str","String_Node_Str","String_Node_Str" + appDir + exeName+ "String_Node_Str");
        }
        if (exists && (!add)) {
          registryDeleteKey(REG_CLASSES_HKEY,REG_CLASSES_PATH + "String_Node_Str" + ext+ "String_Node_Str"+ verb+ "String_Node_Str");
          registryDeleteKey(REG_CLASSES_HKEY,REG_CLASSES_PATH + "String_Node_Str" + ext+ "String_Node_Str"+ verb);
        }
      }
    }
    return true;
  }
 catch (  Exception ex) {
    if (!fromCommandLine) {
      SHELLEXECUTEINFO sei=new SHELLEXECUTEINFO();
      sei.fMask=0x00000040;
      sei.lpVerb=new WString("String_Node_Str");
      sei.lpFile=new WString(appDir + exeName);
      sei.lpParameters=new WString(add ? "String_Node_Str" : "String_Node_Str");
      sei.nShow=WinUser.SW_NORMAL;
      Shell32.INSTANCE.ShellExecuteEx(sei);
      Kernel32.INSTANCE.WaitForSingleObject(sei.hProcess,1000 * 60 * 60* 24);
    }
 else {
      Logger.getLogger(ContextMenuTools.class.getName()).log(Level.SEVERE,null,ex);
    }
  }
  return false;
}