Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ func (c *Consumer) source(
}
}

if match.sourcesInd >= 0 {
if match.sourcesInd >= 0 && match.sourcesInd < int32(len(m.Sources)) {
source = m.Sources[match.sourcesInd]
}
if match.namesInd >= 0 {
Expand Down
24 changes: 24 additions & 0 deletions consumer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,30 @@ func testSourceMap(t *testing.T, json string) {
}
}

func TestSourceWithOutOfRangeSourcesInd(t *testing.T) {
// mappings "ACAA" decode to genCol=0, sourcesInd=1, sourceLine=0,
// sourceCol=0. With a sources array of length 1 this used to panic with
// "index out of range" when indexing m.Sources[match.sourcesInd].
var jsmap = `{
"version": 3,
"sources": ["a.js"],
"mappings": "ACAA"
}`

smap, err := sourcemap.Parse("noname", []byte(jsmap))
if err != nil {
t.Fatal(err)
}

source, _, _, _, ok := smap.Source(1, 1)
if !ok {
t.Fatal("expected a match for line 1 col 1")
}
if source != "" {
t.Fatalf("unexpected source %q for out-of-range sourcesInd", source)
}
}

func TestSourceRootURL(t *testing.T) {
jsonStr := sourceMapJSON
jsonStr = strings.Replace(jsonStr, "/the/root", "http://the/root", 1)
Expand Down