Skip to content

Commit 98545e9

Browse files
authored
Fix nullptr exception (#1355)
* Add test cases to url naming * Fix possible nullptr exception when parent does not exist
1 parent 378806e commit 98545e9

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

cdm/core/src/main/java/thredds/filesystem/MFileOS.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,11 @@ public String getName() {
8888
return file.getName();
8989
}
9090

91+
@Nullable
9192
@Override
9293
public MFile getParent() {
93-
return new MFileOS(file.getParentFile());
94+
File parent = file.getParentFile();
95+
return parent == null ? null : new MFileOS(parent);
9496
}
9597

9698
@Override

cdm/core/src/test/java/ucar/nc2/util/TestURLnaming.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
package ucar.nc2.util;
77

8+
import static com.google.common.truth.Truth.assertThat;
9+
810
import java.lang.invoke.MethodHandles;
911
import org.junit.Test;
1012
import org.slf4j.Logger;
@@ -30,6 +32,14 @@ public void testResolve() {
3032
testResolve("file://test/me/", "file:/wanna", "file:/wanna");
3133
testResolve("file://test/me/", "C:/wanna", "C:/wanna");
3234
testResolve("http://test/me/", "file:wanna", "file:wanna");
35+
36+
testResolve("urlWithoutSlash", "file:///path/with/slash", "file:///path/with/slash");
37+
}
38+
39+
@Test
40+
public void testResolveFile() {
41+
assertThat(URLnaming.resolveFile("urlWithoutSlash", "file:///path/with/slash"))
42+
.isEqualTo("file:///path/with/slash");
3343
}
3444

3545
private void testResolve(String base, String rel, String result) {

0 commit comments

Comments
 (0)