#2365 NullErr Bug in web::ChunkInStream

SlimerDude Thu 16 Oct 2014

Hi, I recently ran into this:

sys::NullErr: java.lang.NullPointerException
  web::ChunkInStream.readBuf (WebUtil.fan:466)
  fan.sys.InStream.readAllBuf (InStream.java:170)

The (hidden) underlying cause was that the wrapped InStream was closed. But referring to the docs of InStream.readBuf() this shouldn't be a problem:

** Return null and leave buf's state untouched if end of stream.
virtual Int? readBuf(Buf buf, Int n)

The ChunkInStream method in question looks like:

override Int? readBuf(Buf buf, Int n)
{
  if (pushback != null && !pushback.isEmpty && n > 0)
  {
    buf.write(pushback.pop)
    return 1
  }
  if (!checkChunk) return null
  numRead := in.readBuf(buf, chunkRem.min(n))
  chunkRem -= numRead
  return numRead
}

And the cause of the NullErr is in the last 3 lines:

numRead := in.readBuf(buf, chunkRem.min(n))
chunkRem -= numRead
return numRead

When the wrapped instream is closed, it correctly returns null which makes:

chunkRem -= numRead

throw the NullErr. A simple null check should solve the problem:

chunkRem -= numRead ?: 0

Cheers, Steve.

brian Wed 22 Oct 2014

Good catch. That makes sense to me, I pushed a fix

Login or Signup to reply.