readBodyForDecode reads the whole request body so it can be scanned before it is decoded, with the caller's size cap applied. Buffering is not a cost paid for the scan. json.Decoder.Decode already holds the entire top-level value in memory before it finishes — refill accumulates into dec.buf and gr
(r *http.Request, maxBytes int64)
| 706 | // 512.5 MiB. Peak heap is indistinguishable between the first two and is |
| 707 | // order-dependent, so it does not discriminate. BUG-2803. |
| 708 | func readBodyForDecode(r *http.Request, maxBytes int64) ([]byte, error) { |
| 709 | if r.Body == nil { |
| 710 | return nil, io.EOF |
| 711 | } |
| 712 | // The nil ResponseWriter is deliberate and its consequence is worth |
| 713 | // stating, because the comment this replaces got it wrong twice over: |
| 714 | // MaxBytesReader.Close forwards to the underlying body rather than being |
| 715 | // a no-op, and with a nil writer there is no automatic 413 — hitting the |
| 716 | // cap surfaces as a read error, which decodeJSONWithLimit wraps and every |
| 717 | // caller turns into a 400. That is the existing behaviour, unchanged |
| 718 | // here; only the claim about it is corrected (codex round 8). |
| 719 | r.Body = http.MaxBytesReader(nil, r.Body, maxBytes) |
| 720 | return io.ReadAll(r.Body) |
| 721 | } |
| 722 | |
| 723 | // truncateBindableText cuts s to at most maxBytes bytes WITHOUT splitting a |
| 724 | // rune, so the result is still valid UTF-8. |
no outgoing calls
no test coverage detected