Program: 5326
Project: 5326
Buggy source:
/** 
 * Patch hive classes by bytecode rewriting in the given source jar. Currently it rewrite the following classes: <ul> <li>  {@link HiveAuthFactory} - This is for skipping kerberos authentication from the explore service container.{@link SessionState} - This is to workaround a native memory leakage bug due tounclosed URLClassloaders, introduced by HIVE-14037. In normal Java process this leakage won't be a problem as eventually those URLClassLoaders will get GC and have the memory released. However, since explore container runs in YARN and YARN monitor the RSS memory usage, it is highly possible that the URLClassLoader won't get GC due to low heap memory usage, while already taken up all the allowed RSS memory. We don't need aux jars added inside the explore JVM since all CDAP classes are already in the classloader. We only use aux jars config to tell hive to localize CDAP jars to task containers. </li> </ul>
 * @param sourceJar the source jar to look for the {@link HiveAuthFactory} class.
 * @param targetJar the target jar to write to if rewrite happened
 * @return the source jar if there is no rewrite happened; the target jar if rewrite happened.
 * @throws IOException if failed to read/write to the jar files.
 */
public static File patchHiveClasses(File sourceJar,File targetJar) throws IOException {
  try (JarFile input=new JarFile(sourceJar)){
    boolean needPatch=false;
    for (    String classFile : HIVE_CLASS_FILES_TO_PATCH.keySet()) {
      needPatch=needPatch || (input.getEntry(classFile) != null);
    }
    if (!needPatch) {
      return sourceJar;
    }
    try (JarOutputStream output=new JarOutputStream(new FileOutputStream(targetJar))){
      Enumeration<JarEntry> sourceEntries=input.entries();
      while (sourceEntries.hasMoreElements()) {
        JarEntry entry=sourceEntries.nextElement();
        output.putNextEntry(new JarEntry(entry.getName()));
        try (InputStream entryInputStream=input.getInputStream(entry)){
          Set<String> patchMethods=HIVE_CLASS_FILES_TO_PATCH.get(entry.getName());
          if (patchMethods == null) {
            ByteStreams.copy(entryInputStream,output);
            continue;
          }
          output.write(rewriteMethodToNoop(entry.getName(),entryInputStream,patchMethods));
        }
       }
    }
     return targetJar;
  }
 }

Fixed version (ground truth):
/** 
 * Patch hive classes by bytecode rewriting in the given source jar. Currently it rewrite the following classes: <ul> <li>  {@link HiveAuthFactory} - This is for skipping kerberos authentication from the explore service container.{@link SessionState} - This is to workaround a native memory leakage bug due tounclosed URLClassloaders, introduced by HIVE-14037. In normal Java process this leakage won't be a problem as eventually those URLClassLoaders will get GC and have the memory released. However, since explore container runs in YARN and YARN monitor the RSS memory usage, it is highly possible that the URLClassLoader won't get GC due to low heap memory usage, while already taken up all the allowed RSS memory. We don't need aux jars added inside the explore JVM since all CDAP classes are already in the classloader. We only use aux jars config to tell hive to localize CDAP jars to task containers. </li> </ul>
 * @param sourceJar the source jar to look for the {@link HiveAuthFactory} class.
 * @param targetJar the target jar to write to if rewrite happened
 * @return the source jar if there is no rewrite happened; the target jar if rewrite happened.
 * @throws IOException if failed to read/write to the jar files.
 */
public static File patchHiveClasses(File sourceJar,File targetJar) throws IOException {
  try (JarFile input=new JarFile(sourceJar)){
    boolean needPatch=false;
    for (    String classFile : HIVE_CLASS_FILES_TO_PATCH.keySet()) {
      needPatch=needPatch || (input.getEntry(classFile) != null);
    }
    if (!needPatch) {
      return sourceJar;
    }
    try (JarOutputStream output=new JarOutputStream(new FileOutputStream(targetJar))){
      Enumeration<JarEntry> sourceEntries=input.entries();
      while (sourceEntries.hasMoreElements()) {
        JarEntry entry=sourceEntries.nextElement();
        output.putNextEntry(new JarEntry(entry.getName()));
        try (InputStream entryInputStream=input.getInputStream(entry)){
          Set<String> patchMethods=HIVE_CLASS_FILES_TO_PATCH.get(entry.getName());
          if (patchMethods == null) {
            ByteStreams.copy(entryInputStream,output);
            continue;
          }
          output.write(Classes.rewriteMethodToNoop(entry.getName(),entryInputStream,patchMethods));
        }
       }
    }
     return targetJar;
  }
 }