@@ -215,12 +215,7 @@ func readTxnField(r *bytes.Reader, n uint64, truncatedMessage string) ([]byte, e
215215 return nil , nil
216216 }
217217
218- // Ensure the requested length does not exceed the remaining reader length.
219- if n > uint64 (r .Len ()) {
220- return nil , errors .New (truncatedMessage )
221- }
222-
223- // Guard against uint64 -> int overflow / excessively large allocations.
218+ // Guard against excessively large fields before attempting to read them.
224219 if n > uint64 (math .MaxInt ) {
225220 return nil , errors .New ("transaction field size overflows int" )
226221 }
@@ -240,12 +235,23 @@ func readTxnSizedBytes(r *bytes.Reader, n uint64) ([]byte, error) {
240235 return nil , nil
241236 }
242237
243- // At this point, callers are expected to have validated that n is safe to
244- // convert to int and does not exceed the remaining reader length.
245- size := int (n )
246- out := make ([]byte , size )
247- if _ , err := io .ReadFull (r , out ); err != nil {
248- return nil , errors .WithStack (err )
238+ var out []byte
239+ var chunkBuf [txnReadChunkSize ]byte
240+ remaining := n
241+ for remaining >= txnReadChunkSize {
242+ if _ , err := io .ReadFull (r , chunkBuf [:]); err != nil {
243+ return nil , errors .WithStack (err )
244+ }
245+ out = append (out , chunkBuf [:]... )
246+ remaining -= txnReadChunkSize
247+ }
248+ for remaining > 0 {
249+ b , err := r .ReadByte ()
250+ if err != nil {
251+ return nil , errors .WithStack (err )
252+ }
253+ out = append (out , b )
254+ remaining --
249255 }
250256 return out , nil
251257}
0 commit comments