Merge pull request #358 from hashicorp/alisdair/fix-malformed-json-panic
json: Fix panic when parsing malformed JSON
This commit is contained in:
commit
08f5f12a13
@ -1,6 +1,7 @@
|
|||||||
package json
|
package json
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/hashicorp/hcl/v2"
|
"github.com/hashicorp/hcl/v2"
|
||||||
@ -95,3 +96,19 @@ func TestParseTemplateUnwrap(t *testing.T) {
|
|||||||
t.Errorf("wrong result %#v; want %#v", val, cty.True)
|
t.Errorf("wrong result %#v; want %#v", val, cty.True)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestParse_malformed(t *testing.T) {
|
||||||
|
src := `{
|
||||||
|
"http_proxy_url: "http://xxxxxx",
|
||||||
|
}`
|
||||||
|
file, diags := Parse([]byte(src), "")
|
||||||
|
if got, want := len(diags), 2; got != want {
|
||||||
|
t.Errorf("got %d diagnostics; want %d", got, want)
|
||||||
|
}
|
||||||
|
if err, want := diags.Error(), `Missing property value colon`; !strings.Contains(err, want) {
|
||||||
|
t.Errorf("diags are %q, but should contain %q", err, want)
|
||||||
|
}
|
||||||
|
if file == nil {
|
||||||
|
t.Errorf("got nil File; want actual file")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -107,6 +107,15 @@ func scan(buf []byte, start pos) []token {
|
|||||||
})
|
})
|
||||||
// If we've encountered an invalid then we might as well stop
|
// If we've encountered an invalid then we might as well stop
|
||||||
// scanning since the parser won't proceed beyond this point.
|
// scanning since the parser won't proceed beyond this point.
|
||||||
|
// We insert a synthetic EOF marker here to match the expectations
|
||||||
|
// of consumers of this data structure.
|
||||||
|
p.Pos.Column++
|
||||||
|
p.Pos.Byte++
|
||||||
|
tokens = append(tokens, token{
|
||||||
|
Type: tokenEOF,
|
||||||
|
Bytes: nil,
|
||||||
|
Range: posRange(p, p),
|
||||||
|
})
|
||||||
return tokens
|
return tokens
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -829,6 +829,21 @@ func TestScan(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Type: tokenEOF,
|
||||||
|
Range: hcl.Range{
|
||||||
|
Start: hcl.Pos{
|
||||||
|
Byte: 1,
|
||||||
|
Line: 1,
|
||||||
|
Column: 2,
|
||||||
|
},
|
||||||
|
End: hcl.Pos{
|
||||||
|
Byte: 1,
|
||||||
|
Line: 1,
|
||||||
|
Column: 2,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user