Merge pull request #38525 from juanvallejo/jvallejo/fix-panic-on-invalid-json-syntax

Automatic merge from submit-queue (batch tested with PRs 38525, 38977)

Prevent json decoder panic on invalid input

Related downstream issue: https://github.com/openshift/origin/issues/12132
```
# Can be replicated on kubectl with:
$ cat panic.json
{
  "kind": "Pod",
  "apiVersion": "v1",
  "metadata": {
    "name": "",
    "labels": {
      "name": ""
    },
    "generateName": "",
    "namespace": "",
    "annotations": []
  },
  "spec": {}
},

$ kubectl create -f panic.json --validate=false
```

**Release note**:
```release-note
release-note-none
```

This patch handles cases where `ioutil.ReadAll` will return a single
character output on an invalid json input, causing the `Decode` method
to panic when it tries to calculate the line number for the syntax
error. The example below would cause a panic due to the trailing comma
at the end:

```
{
  "kind": "Pod",
  "apiVersion": "v1",
  "metadata": {
    "name": "",
    "labels": {
      "name": ""
    },
    "generateName": "",
    "namespace": "",
    "annotations": []
  },
  "spec": {}
},
```

@kubernetes/cli-review @fabianofranz
This commit is contained in:
Kubernetes Submit Queue
2016-12-19 13:23:03 -08:00
committed by GitHub
3 changed files with 14 additions and 5 deletions

View File

@@ -194,7 +194,7 @@ func (s *Serializer) RecognizesData(peek io.Reader) (ok, unknown bool, err error
// we could potentially look for '---'
return false, true, nil
}
_, ok = utilyaml.GuessJSONStream(peek, 2048)
_, _, ok = utilyaml.GuessJSONStream(peek, 2048)
return ok, false, nil
}