okio icon indicating copy to clipboard operation
okio copied to clipboard

NullPointerException in Buffer#write

Open benjamin-bader opened this issue 11 years ago • 14 comments

Using Okio 1.2.0 along with OkHttp 2.1.0, I'm seeing an NPE when reading HTTP responses:

java.lang.NullPointerException
    at okio.Buffer.write(Buffer.java:854)
    at okio.RealBufferedSink.write(RealBufferedSink.java:44)
    at com.squareup.okhttp.internal.http.HttpConnection$FixedLengthSink.write(HttpConnection.java:300)
    at okio.RealBufferedSink.emitCompleteSegments(RealBufferedSink.java:140)
    at okio.RealBufferedSink.write(RealBufferedSink.java:69)
    at com.squareup.okhttp.RequestBody$1.writeTo(RequestBody.java:72)
    at com.squareup.okhttp.Call.getResponse(Call.java:237)  
    at com.squareup.okhttp.Call.execute(Call.java:84)

Evidently, source.head is null. Could this be due to the older OkHttp version?

benjamin-bader avatar Mar 27 '15 18:03 benjamin-bader

Yikes, that's very bad. Can you reproduce this?

swankjesse avatar Mar 27 '15 20:03 swankjesse

I see this in our crash logs; unfortunately I haven't been able to reproduce it myself. It's relatively rare - I've seen it reported exactly 55 times over the past week, out of maybe 14 million requests in the wild.

I'll try to gather more useful information.

Maybe static-analysis tools can help here? I liked that issue before it went into the icebox!

benjamin-bader avatar Mar 27 '15 21:03 benjamin-bader

Any particular patterns on the devices hosting the 55 crashes? I fear there are devices with broken synchronized support!

swankjesse avatar Mar 27 '15 21:03 swankjesse

Eek, that'd be... irresponsible? Awful?

No obvious patterns - an even mixture of Samsung, Huawei, LG, and other usual suspects, with OS versions from 2.3.7 through 4.4.4. Almost no repetition - 55 crashes, and 49 unique device models.

benjamin-bader avatar Mar 27 '15 22:03 benjamin-bader

Well, the device mix rules out lousy devices as the cause. Must be in Okio.

This is really strange. At some point there's a disconnect between the source buffer’s size and its segment count.

I can't reproduce the crash with the most heavy-handed test case I can create:

  @Test public void bufferCombos() throws Exception {
    int[] interestingValues = {
        0, 1, 1023, 1024, 1025, 2047, 2048, 2049, 3071, 3072, 3073, 4095, 4096, 4097, 6143, 6144, 6145
    };
    for (int sourceStart : interestingValues) {
      for (int sourceCount : interestingValues) {
        for (int targetStart : interestingValues) {
          for (int targetCount : interestingValues) {
            for (int copyCount : interestingValues) {
              Buffer source = makeBuffer(sourceStart, sourceCount);
              Buffer target = makeBuffer(targetStart, targetCount);
              try {
                target.write(source, copyCount);
                assertEquals(TestUtil.repeat('a', targetCount + copyCount), target.readUtf8());
                assertEquals(TestUtil.repeat('a', sourceCount - copyCount), source.readUtf8());
              } catch (ArrayIndexOutOfBoundsException ignored) {
                assertEquals(TestUtil.repeat('a', targetCount), target.readUtf8());
                assertEquals(TestUtil.repeat('a', sourceCount), source.readUtf8());
              }
              System.out.printf("%04d %04d %04d %04d %04d%n", sourceStart, sourceCount, targetStart,
                  targetCount, copyCount);
            }
          }
        }
      }
    }
  }

  private Buffer makeBuffer(int sourceStart, int sourceCount) throws EOFException {
    Buffer result = new Buffer();
    result.writeUtf8(TestUtil.repeat('a', sourceStart + sourceCount));
    result.readUtf8(sourceStart);
    return result;
  }

I'll continue to study this!

swankjesse avatar Mar 27 '15 22:03 swankjesse

The good news is that this only happens for a particular endpoint, so at least the range of input is bounded. I'll see about shipping an instrumented OkHttp in our next release - at the very least, we should be able to report some more information than just a stack trace.

benjamin-bader avatar Mar 27 '15 23:03 benjamin-bader

That's so awesome. Thank you!

swankjesse avatar Mar 27 '15 23:03 swankjesse

@benjamin-bader we're getting ready to release 1.4. Any updates?

swankjesse avatar May 16 '15 04:05 swankjesse

Hi, thanks for following up. We continue to see one almost every day, but haven't learned anything useful yet. I'll re-open this if and when I do.

benjamin-bader avatar May 17 '15 02:05 benjamin-bader

Hi, has this issue be fixed?

Trenson avatar Jul 06 '19 10:07 Trenson

I'm getting 100s of these errors from sending push notifications. My suspicion is that it has something to do with the image push notifications that Firebase now supports. These crashes don't seem to be user facing which is good. Unfortunately, Google counts crashes in app store rankings so I'm scared about the amount of crashes I'm getting.

Here's the crash logs:

java.lang.NullPointerException: 
  at com.android.okhttp.okio.Buffer.write (Buffer.java:1182)
  at com.android.okhttp.okio.Buffer.read (Buffer.java:1221)
  at com.android.okhttp.okio.RealBufferedSource.read (RealBufferedSource.java:65)
  at com.android.okhttp.internal.http.Http1xStream$FixedLengthSource.read (Http1xStream.java:602)
  at com.android.okhttp.internal.Util.skipAll (Util.java:172)
  at com.android.okhttp.internal.Util.discard (Util.java:154)
  at com.android.okhttp.internal.http.Http1xStream$FixedLengthSource.close (Http1xStream.java:619)
  at com.android.okhttp.okio.RealBufferedSource.close (RealBufferedSource.java:421)
  at com.android.okhttp.okio.RealBufferedSource$1.close (RealBufferedSource.java:409)
  at com.google.android.gms.internal.firebase_messaging.zzk.zza
  at com.google.firebase.messaging.zzi.close
  at com.google.firebase.messaging.zzb.zza
  at com.google.firebase.messaging.FirebaseMessagingService.zzc
  at com.google.firebase.messaging.zzg.run
  at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1167)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:641)
  at com.google.android.gms.common.util.concurrent.zza.run (Unknown Source:6)
  at java.lang.Thread.run (Thread.java:764)

rayliverified avatar Aug 14 '19 23:08 rayliverified

Opened issue at Firebase repo https://github.com/firebase/firebase-android-sdk/issues/709

rayliverified avatar Aug 14 '19 23:08 rayliverified

Those come from the Android OS and its copy of Okio from a few years ago.

On Wed, Aug 14, 2019, 7:20 PM Ray Li [email protected] wrote:

Opened issue at Firebase repo firebase/firebase-android-sdk#709 https://github.com/firebase/firebase-android-sdk/issues/709

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/square/okio/issues/134?email_source=notifications&email_token=AAAQIEMK4F5TXK3AG6MGLYTQESHLDA5CNFSM4A6YDVMKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD4KMLFY#issuecomment-521454999, or mute the thread https://github.com/notifications/unsubscribe-auth/AAAQIEKTNM75HAZO2X2AAT3QESHLDANCNFSM4A6YDVMA .

JakeWharton avatar Aug 15 '19 00:08 JakeWharton

I have that same problem. Has anyone found the solution?

turgayulutas avatar Aug 27 '19 16:08 turgayulutas