MCPcopy Index your code
hub / github.com/docker/docker-agent / firstJSONObject

Function firstJSONObject

pkg/modelerrors/modelerrors.go:739–759  ·  view source on GitHub ↗

firstJSONObject returns the first complete JSON object found in s, or nil. encoding/json handles escaped quotes and braces inside string values. Limits parsing to 1MB to prevent memory exhaustion from malicious or accidentally huge error responses. To handle '{' characters in URLs or status text (e

(s string)

Source from the content-addressed store, hash-verified

737// To handle '{' characters in URLs or status text (e.g., "param={value}"),
738// we try parsing from each '{' position until we find valid JSON.
739func firstJSONObject(s string) []byte {
740 const maxJSONSize = 1 << 20 // 1MB
741
742 pos := 0
743 for {
744 idx := strings.IndexByte(s[pos:], '{')
745 if idx < 0 {
746 return nil
747 }
748 start := pos + idx
749
750 reader := io.LimitReader(strings.NewReader(s[start:]), maxJSONSize)
751 var raw json.RawMessage
752 if err := json.NewDecoder(reader).Decode(&raw); err == nil {
753 // Successfully decoded JSON
754 return raw
755 }
756 // Try next '{' position
757 pos = start + 1
758 }
759}
760
761// scalarString renders a JSON scalar as text. JSON numbers decode to float64;
762// whole numbers are rendered without a trailing ".0" so a code of 400 prints

Callers 2

classifyOverflowFunction · 0.85
parseProviderErrorFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected