eclipse-ee4j / grizzly

Grizzly
https://eclipse-ee4j.github.io/grizzly
Other
147 stars 69 forks source link

ByteChunk.append throws NPE under certain conditions #2127

Closed bseiller closed 1 year ago

bseiller commented 3 years ago

Problem Description:

ByteChunk.append() is missing a check if out is non-null, analog to ByteChunk.flushBuffers()

If ByteChunk.append() https://github.com/eclipse-ee4j/grizzly/blob/a2ce7775658e11fbccbb9acd32e2daf2b0799f45/modules/http/src/main/java/org/glassfish/grizzly/http/util/ByteChunk.java#L363 is called and ByteChunk.limit equals to len while both ByteChunk.out and ByteChunk.optimizedWrite have their default values (out == null and optimizedWrite == true) this results in a NPE.

Steps to Reproduce:

see https://github.com/bseiller/grizzly_http_bytechunk/blob/master/src/test/java/ByteChunkAppendTest.java#L45 for a unit-test that provokes the NullPointerException and permutations of values that don't.

Possible solution:

Compare to https://github.com/apache/tomcat/blob/main/java/org/apache/tomcat/util/buf/ByteChunk.java#L292 which contains an explicit check if out is non-null. An alternative and easier way to fix this would probably be to just check if ByteChunk.out == null in append() and throw an IOException like in ByteChunk.flushBuffers() which would be consistent, as ByteChunk.append() declares IOException as thrown exception anyway.

Impact of Issue

See here for more details of the context of where and when this happens and the stacktrace: https://github.com/payara/Payara/issues/5263