JamesGuthrie commented on code in PR #3212:
URL: https://github.com/apache/thrift/pull/3212#discussion_r2380928068
##########
lib/go/thrift/binary_protocol.go:
##########
@@ -588,18 +588,48 @@ func (p *TBinaryProtocol) getMinSerializedSize(ttype
TType) int32 {
}
}
-
-
// This function is shared between TBinaryProtocol and TCompactProtocol.
//
// It tries to read size bytes from trans, in a way that prevents large
-// allocations when size is insanely large (mostly caused by malformed
message).
+// allocations when size is insanely large (mostly caused by malformed
message),
+// or smaller than bytes.MinRead.
func safeReadBytes(size int32, trans io.Reader) ([]byte, error) {
if size < 0 {
return nil, nil
}
+ if size > bytes.MinRead {
+ // Use bytes.Buffer to prevent allocating size bytes when size
is very large
+ buf := new(bytes.Buffer)
+ _, err := io.CopyN(buf, trans, int64(size))
+ return buf.Bytes(), err
+ }
+ return readExactBytes(size, trans)
+}
+
+// readExactBytes reads a maximum of size bytes from r until an error or EOF
+// and returns the data it read.
+// The implementation is similar to io.ReadAll, but it uses a buffer of
+// exactly size bytes to prevent bloat when reading short sequences.
+// A successful call returns err == nil, not err == EOF. Because readExactBytes
+// is defined to read from src until EOF, it does not treat an EOF from Read
+// as an error to be reported.
+func readExactBytes(size int32, r io.Reader) ([]byte, error) {
+ // Allocate size bytes
+ b := make([]byte, 0, size)
+ for {
Review Comment:
Yes, that is a better option. Thank you.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]