Enforce a maximum token size.

This commit is contained in:
Nigel Tao committed 2017-05-04 11:16:37 +10:00
1 parent aebbb2b830
commit 1617d5eb34
1 file changed
+12 -2
+12 -2
View File
@@ -8,6 +8,8 @@ import (
"fmt"
)
const maxTokenSize = 1023
type IDMap struct {
byName map[string]ID
byKey []string
@@ -111,7 +113,11 @@ loop:
if alpha(c) {
j := i + 1
for ; j < len(src) && alphaNumeric(src[j]); j++ {
for j < len(src) && alphaNumeric(src[j]) {
j++
if j-i > maxTokenSize {
return nil, fmt.Errorf("token: identifier too long at %s:%d", filename, line)
}
}
id, err := m.insert(string(src[i:j]))
if err != nil {
@@ -129,7 +135,11 @@ loop:
//
// TODO: save numbers in a separate map, not the IDMap??
j := i + 1
for ; j < len(src) && numeric(src[j]); j++ {
for j < len(src) && numeric(src[j]) {
j++
if j-i > maxTokenSize {
return nil, fmt.Errorf("token: constant too long at %s:%d", filename, line)
}
}
id, err := m.insert(string(src[i:j]))
if err != nil {