Skip to content

Commit cd35dee

Browse files
committed
Raise a better message when readpartial overflows our buffer
See jruby#6246 and ruby/webrick#43 for a discussion of this change.
1 parent 5cedc0f commit cd35dee

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

core/src/main/java/org/jruby/util/IOChannel.java

+17-3
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,20 @@ public boolean isOpen() {
8484
}
8585

8686
protected int read(CallSite read, ByteBuffer dst) throws IOException {
87-
IRubyObject readValue = read.call(runtime.getCurrentContext(), io, io, runtime.newFixnum(dst.remaining()));
87+
int remaining = dst.remaining();
88+
IRubyObject readValue = read.call(runtime.getCurrentContext(), io, io, runtime.newFixnum(remaining));
8889
int returnValue = -1;
8990
if (!readValue.isNil()) {
9091
ByteList str = ((RubyString)readValue).getByteList();
91-
dst.put(str.getUnsafeBytes(), str.getBegin(), str.getRealSize());
92-
returnValue = str.getRealSize();
92+
int realSize = str.getRealSize();
93+
94+
if (realSize > remaining) {
95+
throw new ReadPartialBufferOverflowException(
96+
"error calling " + io.getType() + "#readpartial: requested " + remaining + " bytes but received " + realSize);
97+
}
98+
99+
dst.put(str.getUnsafeBytes(), str.getBegin(), realSize);
100+
returnValue = realSize;
93101
}
94102
return returnValue;
95103
}
@@ -184,4 +192,10 @@ public int write(ByteBuffer src) throws IOException {
184192
return write(write, src);
185193
}
186194
}
195+
196+
private class ReadPartialBufferOverflowException extends IOException {
197+
public ReadPartialBufferOverflowException(String message) {
198+
super(message);
199+
}
200+
}
187201
}

0 commit comments

Comments
 (0)