Skip to content

Commit

Permalink
Fix NPE
Browse files Browse the repository at this point in the history
Seems jar.getDirectories() can contain null values, if there is a jar with META-INF/services/subfolder, but META-INF/services itself does not contain any files. In this case the directories() map contains a key for "META-INF/services" but a null value (which is the map which is null here)

Signed-off-by: Christoph Rueger <[email protected]>
  • Loading branch information
chrisrueger committed Dec 4, 2024
1 parent 1fb6873 commit 62655e4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
24 changes: 24 additions & 0 deletions biz.aQute.bndlib.tests/test/test/AnalyzerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
Expand All @@ -30,10 +31,13 @@
import aQute.bnd.osgi.Clazz;
import aQute.bnd.osgi.Constants;
import aQute.bnd.osgi.Domain;
import aQute.bnd.osgi.EmbeddedResource;
import aQute.bnd.osgi.FileResource;
import aQute.bnd.osgi.Jar;
import aQute.bnd.osgi.Packages;
import aQute.bnd.osgi.Processor;
import aQute.bnd.osgi.Resource;
import aQute.bnd.osgi.metainf.MetaInfService;
import aQute.lib.io.IO;

class T0 {}
Expand Down Expand Up @@ -1416,6 +1420,26 @@ public void testSuperfluous() throws Exception {
}
}

@Test
public void testEmptyMetaInfServicesFolder() throws Exception {

Resource r = new EmbeddedResource(new byte[1], 0L);
try (Jar jar = new Jar("test")) {
jar.putResource("META-INF/services/subfolder/", r);

Map<String, Resource> map = jar.getDirectories()
.getOrDefault("META-INF/services", Collections.emptyMap());

assertTrue(jar.getDirectories()
.containsKey("META-INF/services"));
assertNull(jar.getResource("META-INF/services"));

Map<String, MetaInfService> serviceFiles = MetaInfService.getServiceFiles(jar);
assertNotNull(serviceFiles);
assertTrue(serviceFiles.isEmpty());
}
}

static void assertNotPresent(Collection<?> map, String string) {
Collection<String> ss = new HashSet<>();
for (Object o : map)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ public static Map<String, MetaInfService> getServiceFiles(Jar jar) throws Except
Map<String, Resource> map = jar.getDirectories()
.getOrDefault(META_INF_SERVICES_STEM, Collections.emptyMap());

if (map == null) {
// can happen when META-INF/services is empty, but has subfolders
return result;
}

for (Map.Entry<String, Resource> e : map.entrySet()) {
String path = e.getKey();
Resource resource = e.getValue();
Expand Down

0 comments on commit 62655e4

Please sign in to comment.