goparsec icon indicating copy to clipboard operation
goparsec copied to clipboard

Project status; Panic in parsec.String() with unterminated string

Open rm-hull opened this issue 3 years ago • 1 comments

Hi @prataprc, first off: is this project still maintained? Im guessing that the Travis CI is no longer working and that the version of go (1.13) means that it wont build with modern toolchain?

If I run an unterminated string through the String() parser, it panics with runtime error: index out of range [6] with length 6:

data := []byte(`"hello`)
scanner := parsec.NewScanner(data)
node, _ := parsec.String()(scanner)
print(node)

I think the issue is in tokenizer.go, in method scanString (suggested changes in comments):

func scanString(txt []byte) (tok []byte, readn int) {
	if len(txt) < 2 {
		return nil, 0
	}

	e := 1
	for txt[e] != '"' {
		c := txt[e]
		if c == '\\' || c == '"' || c < ' ' {
			break
		}
		if c < utf8.RuneSelf {
			e++
			// SUGGESTED CHANGE 1
			// if e >= len(txt) {
			// 	return nil, 0
			// }
			continue
		}
		r, size := utf8.DecodeRune(txt[e:])
		if r == utf8.RuneError && size == 1 {
			return nil, 0
		}
		e += size

		// SUGGESTED CHANGE 2
		// if e >= len(txt) {
		// 	return nil, 0
		// }
	}

	// ...

Is there an appetite for bringing this project up-to-date and fixing this issue?

rm-hull avatar Aug 21 '22 12:08 rm-hull

Hmm, diving in a bit further, I see there is a test case:

	// malformed string
	func() {
		defer func() {
			if r := recover(); r == nil {
				t.Errorf("expected panic")
			}
		}()
		s = NewScanner([]byte(`"hello`))
		String()(s)
	}()

Could you give some background as to why you would expect that to panic

rm-hull avatar Aug 21 '22 12:08 rm-hull