Skip to content

Commit

Permalink
Fix nullptr exception (#1355)
Browse files Browse the repository at this point in the history
* Add test cases to url naming

* Fix possible nullptr exception when parent does not exist
  • Loading branch information
tdrwenski committed Jun 13, 2024
1 parent 378806e commit 98545e9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
4 changes: 3 additions & 1 deletion cdm/core/src/main/java/thredds/filesystem/MFileOS.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,11 @@ public String getName() {
return file.getName();
}

@Nullable
@Override
public MFile getParent() {
return new MFileOS(file.getParentFile());
File parent = file.getParentFile();
return parent == null ? null : new MFileOS(parent);
}

@Override
Expand Down
10 changes: 10 additions & 0 deletions cdm/core/src/test/java/ucar/nc2/util/TestURLnaming.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

package ucar.nc2.util;

import static com.google.common.truth.Truth.assertThat;

import java.lang.invoke.MethodHandles;
import org.junit.Test;
import org.slf4j.Logger;
Expand All @@ -30,6 +32,14 @@ public void testResolve() {
testResolve("file://test/me/", "file:/wanna", "file:/wanna");
testResolve("file://test/me/", "C:/wanna", "C:/wanna");
testResolve("http://test/me/", "file:wanna", "file:wanna");

testResolve("urlWithoutSlash", "file:///path/with/slash", "file:///path/with/slash");
}

@Test
public void testResolveFile() {
assertThat(URLnaming.resolveFile("urlWithoutSlash", "file:///path/with/slash"))
.isEqualTo("file:///path/with/slash");
}

private void testResolve(String base, String rel, String result) {
Expand Down

0 comments on commit 98545e9

Please sign in to comment.