Skip to content

Commit 7f58243

Browse files
committed
Fix marker parser misinterpreting number-ish strings as numeric literals
Signed-off-by: sivchari <shibuuuu5@gmail.com>
1 parent d6c2788 commit 7f58243

2 files changed

Lines changed: 17 additions & 7 deletions

File tree

pkg/markers/parse.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ func guessType(scanner *sc.Scanner, raw string, allowSlice bool) *Argument {
265265
maybeItem := guessType(scanner, raw, false)
266266

267267
subRaw := raw[scanner.Pos().Offset:]
268-
subScanner := parserScanner(subRaw, scanner.Error)
268+
subScanner := parserScanner(subRaw, func(*sc.Scanner, string) {})
269269

270270
var tok rune
271271
for {
@@ -291,7 +291,8 @@ func guessType(scanner *sc.Scanner, raw string, allowSlice bool) *Argument {
291291
// (so we don't consume our scanner tokens until we actually
292292
// go to use this -- Go doesn't like scanners that can be rewound).
293293
subRaw := raw[scanner.Pos().Offset:]
294-
subScanner := parserScanner(subRaw, scanner.Error)
294+
var hadScanError bool
295+
subScanner := parserScanner(subRaw, func(*sc.Scanner, string) { hadScanError = true })
295296

296297
// skip whitespace
297298
hint := peekNoSpace(subScanner)
@@ -361,11 +362,17 @@ func guessType(scanner *sc.Scanner, raw string, allowSlice bool) *Argument {
361362
nextTok = subScanner.Scan()
362363
}
363364

364-
if nextTok == sc.Int {
365-
return &Argument{Type: IntType}
366-
}
367-
if nextTok == sc.Float {
368-
return &Argument{Type: NumberType}
365+
// Only treat as a numeric type if the scanner did not emit errors while
366+
// tokenising (e.g. invalid octal "087bdd" produces sc.Int but also
367+
// triggers an error). In that case fall through to the bare-string
368+
// path so the value is treated as a string.
369+
if !hadScanError {
370+
if nextTok == sc.Int {
371+
return &Argument{Type: IntType}
372+
}
373+
if nextTok == sc.Float {
374+
return &Argument{Type: NumberType}
375+
}
369376
}
370377
}
371378

pkg/markers/parse_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,9 @@ var _ = Describe("Parsing", func() {
219219
},
220220
}
221221
It("should support non-uniform, nested maps (and always guess as such)", argParseTestCase{arg: anyArg, raw: `{text: "abc", len: 3, "as bytes": 97;98;99, props: {encoding: ascii, nullsafe: true, tags: {"triple", "in a row"}}}`, output: complexMap}.Run)
222+
223+
It("should parse number-ish strings with invalid octal prefix as string", argParseTestCase{arg: anyArg, raw: `087bdd`, output: "087bdd"}.Run)
224+
It("should parse path-like strings as string", argParseTestCase{arg: anyArg, raw: `/tmp/tmp.4paHkdEcpK`, output: "/tmp/tmp.4paHkdEcpK"}.Run)
222225
})
223226
})
224227
})

0 commit comments

Comments
 (0)