Skip to content

Commit 54d948d

Browse files
authored
Adding null checks for InstrHttpInputStream/InstrHttpOutputStream (#3415)
1 parent b85721d commit 54d948d

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

firebase-perf/src/main/java/com/google/firebase/perf/network/InstrURLConnectionBase.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,13 @@ public InputStream getInputStream() throws IOException {
140140

141141
try {
142142
final InputStream inputStream = httpUrlConnection.getInputStream();
143-
return new InstrHttpInputStream(inputStream, networkMetricBuilder, timer);
143+
// Make sure we don't pass in a null into InstrHttpInputStream, since InstrHttpInputStream is
144+
// not null-safe.
145+
if (inputStream != null) {
146+
return new InstrHttpInputStream(inputStream, networkMetricBuilder, timer);
147+
}
148+
// Only reached when inputStream is null
149+
return inputStream;
144150
} catch (final IOException e) {
145151
networkMetricBuilder.setTimeToResponseCompletedMicros(timer.getDurationMicros());
146152
NetworkRequestMetricBuilderUtil.logError(networkMetricBuilder);
@@ -156,8 +162,14 @@ public long getLastModified() {
156162

157163
public OutputStream getOutputStream() throws IOException {
158164
try {
159-
return new InstrHttpOutputStream(
160-
httpUrlConnection.getOutputStream(), networkMetricBuilder, timer);
165+
final OutputStream outputStream = httpUrlConnection.getOutputStream();
166+
// Make sure we don't pass in a null into InstrHttpOutputStream, since InstrHttpOutputStream
167+
// is not null-safe.
168+
if (outputStream != null) {
169+
return new InstrHttpOutputStream(outputStream, networkMetricBuilder, timer);
170+
}
171+
// Only reached when outputStream is null
172+
return outputStream;
161173
} catch (final IOException e) {
162174
networkMetricBuilder.setTimeToResponseCompletedMicros(timer.getDurationMicros());
163175
NetworkRequestMetricBuilderUtil.logError(networkMetricBuilder);

firebase-perf/src/test/java/com/google/firebase/perf/network/InstrURLConnectionBaseTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,15 @@ public void testGetInputStreamThrowsIOException() throws IOException {
107107
assertThat(metric.getTimeToResponseCompletedUs()).isEqualTo(2000);
108108
}
109109

110+
@Test
111+
public void testGetInputStreamWithNullInputStreamReturnsNull() throws IOException {
112+
HttpURLConnection urlConnection = mockHttpUrlConnection();
113+
when(urlConnection.getInputStream()).thenReturn(null);
114+
assertThat(
115+
new InstrURLConnectionBase(urlConnection, timer, networkMetricBuilder).getInputStream())
116+
.isNull();
117+
}
118+
110119
@Test
111120
public void testGetOutputStreamThrowsIOException() throws IOException {
112121
HttpURLConnection urlConnection = mockHttpUrlConnection();
@@ -122,6 +131,16 @@ public void testGetOutputStreamThrowsIOException() throws IOException {
122131
assertThat(metric.getTimeToResponseCompletedUs()).isEqualTo(2000);
123132
}
124133

134+
@Test
135+
public void testGetOutputStreamWithNullOuputStreamReturnsNull() throws IOException {
136+
HttpURLConnection urlConnection = mockHttpUrlConnection();
137+
when(urlConnection.getOutputStream()).thenReturn(null);
138+
assertThat(
139+
new InstrURLConnectionBase(urlConnection, timer, networkMetricBuilder)
140+
.getOutputStream())
141+
.isNull();
142+
}
143+
125144
@Test
126145
public void testGetPermissionThrowsIOException() throws IOException {
127146
HttpURLConnection urlConnection = mockHttpUrlConnection();

0 commit comments

Comments
 (0)