Skip to content

Commit eee1ad9

Browse files
committed
[fix] ensure buffer capacity on an overflow
1 parent 52ecafb commit eee1ad9

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

src/main/java/org/jruby/ext/openssl/SSLSocket.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,7 @@ private IRubyObject doHandshake(final boolean blocking, final boolean exception)
589589
assert status != SSLEngineResult.Status.BUFFER_UNDERFLOW;
590590
if (status == SSLEngineResult.Status.BUFFER_OVERFLOW) {
591591
netWriteData.compact();
592+
netWriteData = Utils.ensureCapacity(netWriteData, engine.getSession().getPacketBufferSize());
592593
netWriteData.flip();
593594
if (handshakeStatus != SSLEngineResult.HandshakeStatus.NEED_UNWRAP || flushData(blocking)) {
594595
sel = waitSelect(SelectionKey.OP_WRITE, blocking, exception);
@@ -602,7 +603,7 @@ private IRubyObject doHandshake(final boolean blocking, final boolean exception)
602603
}
603604
}
604605

605-
private void doWrap(boolean blocking) throws IOException {
606+
private void doWrap(final boolean blocking) throws IOException {
606607
netWriteData.clear();
607608
SSLEngineResult result = engine.wrap(dummy, netWriteData);
608609
netWriteData.flip();
@@ -623,6 +624,11 @@ private void doTasks() {
623624
verifyResult = sslContext.getLastVerifyResult();
624625
}
625626

627+
/**
628+
* @param blocking
629+
* @return whether buffer has remaining data
630+
* @throws IOException
631+
*/
626632
private boolean flushData(boolean blocking) throws IOException {
627633
try {
628634
writeToChannel(netWriteData, blocking);

src/main/java/org/jruby/ext/openssl/Utils.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
package org.jruby.ext.openssl;
2929

3030
import java.io.IOException;
31+
import java.nio.ByteBuffer;
3132
import java.util.HashSet;
3233

3334
import org.jruby.*;
@@ -176,8 +177,12 @@ public void visit(IRubyObject key, IRubyObject value) {
176177
return ret;
177178
}
178179

179-
static IRubyObject extractKeywordArg(ThreadContext context, String keyword, RubyHash opts) {
180-
return opts.op_aref(context, context.runtime.newSymbol(keyword));
180+
static ByteBuffer ensureCapacity(final ByteBuffer buffer, final int size) {
181+
if (size <= buffer.capacity()) return buffer;
182+
buffer.flip();
183+
ByteBuffer newBuffer = ByteBuffer.allocate(size);
184+
newBuffer.put(buffer);
185+
return newBuffer;
181186
}
182187

183188
}// Utils

0 commit comments

Comments
 (0)