Bug 60041 - NPE in WebappClassLoaderBase
Summary: NPE in WebappClassLoaderBase
Status: RESOLVED FIXED
Alias: None
Product: Tomcat 7
Classification: Unclassified
Component: Catalina (show other bugs)
Version: trunk
Hardware: PC Mac OS X 10.1
: P2 normal (vote)
Target Milestone: ---
Assignee: Tomcat Developers Mailing List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-08-25 07:36 UTC by gehui
Modified: 2016-08-25 21:00 UTC (History)
0 users



Attachments
patch (787 bytes, patch)
2016-08-25 07:39 UTC, gehui
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description gehui 2016-08-25 07:36:41 UTC
After deploy war in tomcat, delete the jar in WEB-INF/lib/, then it may throws NullPointerException when load class


The reason as follow:

WebappClassLoaderBase (between line 3093 and 3110 in method openJARs())

    protected boolean openJARs() {
        if (started && (jarFiles.length > 0)) {
            lastJarAccessed = System.currentTimeMillis();
            if (jarFiles[0] == null) {
                for (int i = 0; i < jarFiles.length; i++) {
                    try {
                        jarFiles[i] = new JarFile(jarRealFiles[i]);
                    } catch (IOException e) {
                        if (log.isDebugEnabled()) {
                            log.debug("Failed to open JAR", e);
                        }
                        return false;
                    }
                }
            }
        }
        return true;
    }


If IOException is thrown in line 3099, the jarFiles will be [file1,file2,NULL,NULL,...]. after that, it will return true when invoke openJARs(), beacuse jarFiles[0] != null. 


WebappClassLoaderBase (between line 3271 and 3285 in method openJARs())

            if (openJARs()) {
                for (int i = 0; i < jarFiles.length; i++) {

                    jarEntry = jarFiles[i].getJarEntry(jarEntryPath);

                    if (jarEntry != null) {
                        try {
                            entry.manifest = jarFiles[i].getManifest();
                        } catch (IOException ioe) {
                            // Ignore
                        }
                        break;
                    }
                 }
             }



beacuse jarFiles=[file1,file2,NULL,NULL,...], so jarFiles[i] may be null, This will cause NullPointerException in line 3274. In this case, it is difficult to judge what went wrong.

I attached a patch that fixes it
Comment 1 gehui 2016-08-25 07:39:45 UTC
Created attachment 34171 [details]
patch
Comment 2 Mark Thomas 2016-08-25 21:00:40 UTC
Thanks. I applied a variation that used i18n. Note deleting a JAR like this is expected to break things. This has been fixed in 7.0.x for 7.0.71 onwards.