Fix Parser.Range returning wrong offset for error highlights
The subsliceOffset method incorrectly computed offset as
len(p.data) - len(b), which only works when b is a suffix (tail) of
p.data. However, error highlights (ParserError.Highlight) are arbitrary
subslices from the middle of the input (e.g., b[0:1] from parseSimpleKey),
so their len has no relationship to their position.
This was a regression introduced in commit 3aaf147 (Remove unsafe package
usage) which replaced danger.SubsliceOffset (pointer arithmetic) with the
incorrect len-based approach.
Fix by using reflect.ValueOf().Pointer() to compute the actual byte
offset between slice data pointers, matching the approach already used
in errors.go:subsliceOffset.
Fixes #1047
Co-authored-by: Thomas Pelletier <thomas@pelletier.dev>
This commit is contained in:
+12
-3
@@ -3,6 +3,7 @@ package unstable
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"unicode"
|
||||
|
||||
"github.com/pelletier/go-toml/v2/internal/characters"
|
||||
@@ -83,10 +84,18 @@ func (p *Parser) rangeOfToken(token, rest []byte) Range {
|
||||
}
|
||||
|
||||
// subsliceOffset returns the byte offset of subslice b within p.data.
|
||||
// b must be a suffix (tail) of p.data.
|
||||
// b must share the same backing array as p.data.
|
||||
func (p *Parser) subsliceOffset(b []byte) int {
|
||||
// b is a suffix of p.data, so its offset is len(p.data) - len(b)
|
||||
return len(p.data) - len(b)
|
||||
if len(b) == 0 {
|
||||
return 0
|
||||
}
|
||||
dataPtr := reflect.ValueOf(p.data).Pointer()
|
||||
subPtr := reflect.ValueOf(b).Pointer()
|
||||
offset := int(subPtr - dataPtr)
|
||||
if offset < 0 || offset > len(p.data) {
|
||||
panic("subslice is not within parser data")
|
||||
}
|
||||
return offset
|
||||
}
|
||||
|
||||
// Raw returns the slice corresponding to the bytes in the given range.
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package unstable
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -673,6 +674,98 @@ key3 = "value3"
|
||||
assert.Equal(t, []string{"key1", "key2", "key3"}, keys)
|
||||
}
|
||||
|
||||
func TestRangeOffsetAfterComment(t *testing.T) {
|
||||
input := []byte("# comment\n= \"value\"")
|
||||
|
||||
p := Parser{}
|
||||
p.Reset(input)
|
||||
for p.NextExpression() {
|
||||
}
|
||||
err := p.Error()
|
||||
if err == nil {
|
||||
t.Fatal("expected an error")
|
||||
}
|
||||
var perr *ParserError
|
||||
if !errors.As(err, &perr) {
|
||||
t.Fatalf("expected ParserError, got %T", err)
|
||||
}
|
||||
r := p.Range(perr.Highlight)
|
||||
shape := p.Shape(r)
|
||||
|
||||
if r.Offset != 10 {
|
||||
t.Errorf("Range offset: got %d, want 10", r.Offset)
|
||||
}
|
||||
if shape.Start.Line != 2 || shape.Start.Column != 1 {
|
||||
t.Errorf("position: got %d:%d, want 2:1", shape.Start.Line, shape.Start.Column)
|
||||
}
|
||||
}
|
||||
|
||||
func TestErrorHighlightPositions(t *testing.T) {
|
||||
examples := []struct {
|
||||
desc string
|
||||
input string
|
||||
wantLine int
|
||||
wantColumn int
|
||||
}{
|
||||
{
|
||||
desc: "invalid key start after comment",
|
||||
input: "# comment\n= \"value\"",
|
||||
wantLine: 2,
|
||||
wantColumn: 1,
|
||||
},
|
||||
{
|
||||
desc: "invalid key start on first line",
|
||||
input: "= \"value\"",
|
||||
wantLine: 1,
|
||||
wantColumn: 1,
|
||||
},
|
||||
{
|
||||
desc: "invalid key after multiple comments",
|
||||
input: "# comment 1\n# comment 2\n= \"value\"",
|
||||
wantLine: 3,
|
||||
wantColumn: 1,
|
||||
},
|
||||
{
|
||||
desc: "invalid key after valid key-value",
|
||||
input: "a = 1\n= \"value\"",
|
||||
wantLine: 2,
|
||||
wantColumn: 1,
|
||||
},
|
||||
{
|
||||
desc: "invalid key after whitespace on line",
|
||||
input: "a = 1\n = \"value\"",
|
||||
wantLine: 2,
|
||||
wantColumn: 3,
|
||||
},
|
||||
}
|
||||
|
||||
for _, e := range examples {
|
||||
t.Run(e.desc, func(t *testing.T) {
|
||||
p := Parser{}
|
||||
p.Reset([]byte(e.input))
|
||||
for p.NextExpression() {
|
||||
}
|
||||
err := p.Error()
|
||||
if err == nil {
|
||||
t.Fatal("expected an error")
|
||||
}
|
||||
var perr *ParserError
|
||||
if !errors.As(err, &perr) {
|
||||
t.Fatalf("expected ParserError, got %T", err)
|
||||
}
|
||||
r := p.Range(perr.Highlight)
|
||||
shape := p.Shape(r)
|
||||
|
||||
if shape.Start.Line != e.wantLine {
|
||||
t.Errorf("line: got %d, want %d", shape.Start.Line, e.wantLine)
|
||||
}
|
||||
if shape.Start.Column != e.wantColumn {
|
||||
t.Errorf("column: got %d, want %d", shape.Start.Column, e.wantColumn)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func ExampleParser() {
|
||||
doc := `
|
||||
hello = "world"
|
||||
|
||||
Reference in New Issue
Block a user