Skip to content

Commit 68ad8e7

Browse files
authored
fix: possible NPE when HttpStorageOptions deserialized (#2153)
Java can dedupe objects in a cyclic traversal, retryingDepsAdapter doesn't need to be transient. b/294399427
1 parent eba8b6a commit 68ad8e7

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

google-cloud-storage/src/main/java/com/google/cloud/storage/HttpStorageOptions.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public class HttpStorageOptions extends StorageOptions {
5252
private static final String DEFAULT_HOST = "https://storage.googleapis.com";
5353

5454
private final HttpRetryAlgorithmManager retryAlgorithmManager;
55-
private final transient RetryDependenciesAdapter retryDepsAdapter;
55+
private final RetryDependenciesAdapter retryDepsAdapter;
5656

5757
private HttpStorageOptions(Builder builder, StorageDefaults serviceDefaults) {
5858
super(builder, serviceDefaults);
@@ -334,7 +334,7 @@ public ServiceRpc create(StorageOptions options) {
334334
* We don't yet want to make HttpStorageOptions itself implement {@link RetryingDependencies} but
335335
* we do need use it in a couple places, for those we create this adapter.
336336
*/
337-
private final class RetryDependenciesAdapter implements RetryingDependencies {
337+
private final class RetryDependenciesAdapter implements RetryingDependencies, Serializable {
338338

339339
private RetryDependenciesAdapter() {}
340340

google-cloud-storage/src/test/java/com/google/cloud/storage/SerializationTest.java

+25
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,11 @@
4040
import com.google.common.collect.ImmutableMap;
4141
import com.google.common.collect.ImmutableSet;
4242
import java.io.ByteArrayInputStream;
43+
import java.io.ByteArrayOutputStream;
4344
import java.io.IOException;
4445
import java.io.InputStream;
4546
import java.io.ObjectInputStream;
47+
import java.io.ObjectOutputStream;
4648
import java.io.Serializable;
4749
import java.util.Base64;
4850
import java.util.Collections;
@@ -213,6 +215,29 @@ protected Serializable[] serializableObjects() {
213215
}
214216
}
215217

218+
@Test
219+
public void avoidNpeHttpStorageOptions_retryDeps() throws IOException, ClassNotFoundException {
220+
HttpStorageOptions optionsHttp1 =
221+
StorageOptions.http()
222+
.setProjectId("http1")
223+
.setCredentials(NoCredentials.getInstance())
224+
.build();
225+
226+
assertThat(optionsHttp1.asRetryDependencies()).isNotNull();
227+
228+
ByteArrayOutputStream baos = new ByteArrayOutputStream();
229+
try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
230+
oos.writeObject(optionsHttp1);
231+
}
232+
233+
byte[] byteArray = baos.toByteArray();
234+
try (ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(byteArray))) {
235+
Object o = ois.readObject();
236+
HttpStorageOptions hso = (HttpStorageOptions) o;
237+
assertThat(hso.asRetryDependencies()).isNotNull();
238+
}
239+
}
240+
216241
@Override
217242
@SuppressWarnings("resource")
218243
protected Restorable<?>[] restorableObjects() {

0 commit comments

Comments
 (0)