Skip to content

Commit e31df4a

Browse files
committed
Custom BIO_java_bytebuffer write callback over-reports bytes written, violating the BIO_write contract
Motivation: Our custom BIO_java_bytebuffer write callback internally flushes queued bytes but made the mistake to also account for these in the return value of the callback. This is incorrect as a caller to BIO_write(...) will use the return value to detect how much of the input buffer data was consumed. Modifications: Don't account for the flushed data in the returned value Result: Correctly implement the write callback
1 parent 865531f commit e31df4a

1 file changed

Lines changed: 9 additions & 2 deletions

File tree

  • openssl-dynamic/src/main/c

‎openssl-dynamic/src/main/c/ssl.c‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,14 @@ static jint tcn_write_to_bytebuffer(BIO* bio, const char* in, int inl) {
225225

226226
// First check if we need to drain data queued in the internal SSL buffer.
227227
if (bioUserData->nonApplicationBufferLength != 0) {
228-
writeAmount = tcn_flush_sslbuffer_to_bytebuffer(bioUserData);
228+
// Internally queued data that is flushed will not be reported back to the caller as the return
229+
// value is usually used to keep track how much data was written of the given input buffer.
230+
tcn_flush_sslbuffer_to_bytebuffer(bioUserData);
231+
232+
if (bioUserData->bufferLength == 0) {
233+
BIO_set_retry_write(bio); // no space left.
234+
return -1;
235+
}
229236
}
230237

231238
// Next write "in" into what ever space the ByteBuffer has available.
@@ -239,7 +246,7 @@ static jint tcn_write_to_bytebuffer(BIO* bio, const char* in, int inl) {
239246
bioUserData->bufferLength -= writeChunk;
240247
bioUserData->buffer += writeChunk; // Pointer arithmetic based on char* type
241248

242-
return writeAmount + writeChunk;
249+
return writeChunk;
243250
}
244251

245252
static jint tcn_read_from_bytebuffer(BIO* bio, char *out, int outl) {

0 commit comments

Comments
 (0)