Skip to content

Commit 4664468

Browse files
xoltiaarp242
authored andcommitted
Fix ErrorWithPosition panic when less than two lines
Would panic if the TOML had just one line without a newline at the end of the file.
1 parent 211c915 commit 4664468

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

error_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,19 @@ func TestParseError(t *testing.T) {
245245
| 15:04:05.856018510
246246
`,
247247
},
248+
249+
{
250+
&struct{ String string }{},
251+
`string = "test`,
252+
`
253+
| toml: error: unexpected EOF; expected '"'
254+
|
255+
| At line 1, column 14:
256+
|
257+
| 1 | string = "test
258+
| ^
259+
`,
260+
},
248261
}
249262

250263
prep := func(s string) string {

lex.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,9 @@ func (lx *lexer) errorPos(start, length int, err error) stateFn {
275275
func (lx *lexer) errorf(format string, values ...any) stateFn {
276276
if lx.atEOF {
277277
pos := lx.getPos()
278-
pos.Line--
278+
if lx.pos >= 1 && lx.input[lx.pos-1] == '\n' {
279+
pos.Line--
280+
}
279281
pos.Len = 1
280282
pos.Start = lx.pos - 1
281283
lx.items <- item{typ: itemError, pos: pos, err: fmt.Errorf(format, values...)}

0 commit comments

Comments
 (0)