Skip to content

Instantly share code, notes, and snippets.

@kaedys
Last active April 28, 2016 21:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kaedys/53b8fd05690d5d8f202afa7878d6e3d5 to your computer and use it in GitHub Desktop.
Save kaedys/53b8fd05690d5d8f202afa7878d6e3d5 to your computer and use it in GitHub Desktop.
Thrift memory explosion fix
22c23
---
> "bytes"
475c477,478
---
> const readLimit = 32768
>
482c486
---
> /* Original version
491c495,523
< }
---
> */
> var (
> buf bytes.Buffer
> e error
> b []byte
> )
>
> switch {
> case int(size) <= len(p.buffer):
> b = p.buffer[:size] // avoids allocation for small reads
> case int(size) < readLimit:
> b = make([]byte, size)
> default:
> b = make([]byte, readLimit)
> }
>
> for size > 0 {
> _, e = io.ReadFull(p.trans, b)
> buf.Write(b)
> if e != nil {
> break
> }
> size -= readLimit
> if size < readLimit && size > 0 {
> b = b[:size]
> }
> }
> return buf.String(), NewTProtocolException(e)
> }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment