Skip to content

Commit

Permalink
fix: Prevent NPE when bucket doesn't exit #857
Browse files Browse the repository at this point in the history
Fixes a NullPointerException when creating a Path object with a bucket
that doesn't exist. This only occurred when autoDetectRequesterPays = true.

Refs: #857
  • Loading branch information
lbergelson committed Mar 15, 2022
1 parent 03f4ec7 commit c9d6353
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -963,8 +963,13 @@ public String toString() {
public boolean requesterPays(String bucketName) {
initStorage();
try {
// instead of true/false, this method returns true/null.
Boolean isRP = storage.get(bucketName).requesterPays();
final Bucket bucket = storage.get(bucketName);
// If the bucket doesn't exist it can't be requester pays.
if (bucket == null) {
return false;
}
// instead of true/false, this method returns true/null
Boolean isRP = bucket.requesterPays();
return isRP != null && isRP.booleanValue();
} catch (StorageException ex) {
if ("userProjectMissing".equals(ex.getReason())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.URISyntaxException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.ReadableByteChannel;
Expand Down Expand Up @@ -327,6 +328,24 @@ public void testAutodetectWhenNotRequesterPays() throws IOException {
"");
}

@Test
public void assertNoCrashWhenBucketDoesntExist() throws URISyntaxException {
CloudStorageConfiguration config =
CloudStorageConfiguration.builder()
.autoDetectRequesterPays(true)
.userProject(project)
.usePseudoDirectories(true)
.build();

final String bucketThatDoesntExist = "abuckethatdoesntexist";
final String subPath = "hello";
CloudStorageFileSystem testBucket =
CloudStorageFileSystem.forBucket(bucketThatDoesntExist, config, storageOptions);
final CloudStoragePath aPathThatDoesntExist = testBucket.getPath(subPath);
Assert.assertEquals(
aPathThatDoesntExist.toUri().toString(), "gs://" + bucketThatDoesntExist + "/" + subPath);
}

@Test
public void testAutoDetectNoUserProject() throws IOException {
CloudStorageFileSystem testBucket = getRequesterPaysBucket(false, "");
Expand Down

0 comments on commit c9d6353

Please sign in to comment.