go:noinline
(n int, errOut *Error)
| 50 | |
| 51 | //go:noinline |
| 52 | func (b *ByteBuffer) fill(n int, errOut *Error) bool { |
| 53 | if n < 0 { |
| 54 | if errOut != nil { |
| 55 | *errOut = DeserializationErrorf("negative readable byte count: %d", n) |
| 56 | } |
| 57 | return false |
| 58 | } |
| 59 | if b.reader == nil { |
| 60 | if errOut != nil { |
| 61 | *errOut = BufferOutOfBoundError(b.readerIndex, n, len(b.data)) |
| 62 | } |
| 63 | return false |
| 64 | } |
| 65 | |
| 66 | available := len(b.data) - b.readerIndex |
| 67 | if available >= n { |
| 68 | return true |
| 69 | } |
| 70 | |
| 71 | if b.readerIndex > 0 { |
| 72 | copy(b.data, b.data[b.readerIndex:]) |
| 73 | b.writerIndex -= b.readerIndex |
| 74 | b.readerIndex = 0 |
| 75 | b.data = b.data[:b.writerIndex] |
| 76 | } |
| 77 | |
| 78 | for len(b.data) < n { |
| 79 | if len(b.data) == cap(b.data) { |
| 80 | // n can come from attacker-controlled wire lengths. Do not query |
| 81 | // reader availability here: interface/type-specific probes add |
| 82 | // hot-path cost for a rare fast path and are not the correctness |
| 83 | // source. Grow only from bytes already buffered so truncated streams |
| 84 | // fail before reserving the declared body size. |
| 85 | currentCap := cap(b.data) |
| 86 | newCap := currentCap * 2 |
| 87 | if currentCap > MaxInt/2 { |
| 88 | newCap = MaxInt |
| 89 | } |
| 90 | if newCap < b.bufferSize { |
| 91 | newCap = b.bufferSize |
| 92 | } |
| 93 | if newCap <= currentCap { |
| 94 | newCap = currentCap + 1 |
| 95 | } |
| 96 | if newCap > n { |
| 97 | newCap = n |
| 98 | } |
| 99 | if newCap <= currentCap { |
| 100 | if errOut != nil { |
| 101 | *errOut = DeserializationErrorf("stream buffer size exceeds supported range") |
| 102 | } |
| 103 | return false |
| 104 | } |
| 105 | newData := make([]byte, len(b.data), newCap) |
| 106 | copy(newData, b.data) |
| 107 | b.data = newData |
| 108 | } |
| 109 | spare := b.data[len(b.data):cap(b.data)] |