@@ -84,12 +84,20 @@ public boolean isOpen() {
84
84
}
85
85
86
86
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 ));
88
89
int returnValue = -1 ;
89
90
if (!readValue .isNil ()) {
90
91
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 ;
93
101
}
94
102
return returnValue ;
95
103
}
@@ -184,4 +192,10 @@ public int write(ByteBuffer src) throws IOException {
184
192
return write (write , src );
185
193
}
186
194
}
195
+
196
+ private class ReadPartialBufferOverflowException extends IOException {
197
+ public ReadPartialBufferOverflowException (String message ) {
198
+ super (message );
199
+ }
200
+ }
187
201
}
0 commit comments