Skip to content

Commit 31bbf21

Browse files
committed
Fix comment loop
1 parent 3281872 commit 31bbf21

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

common/json/internal/contextjson/comment.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,9 @@ func (p *commentNodeParser) parseValue(i int, path CommentPath, anchor int) (com
333333
return node, end, true
334334
default:
335335
end := p.parseLiteralEnd(i)
336+
if end == i {
337+
return commentNode{}, i + 1, false
338+
}
336339
node := commentNode{path: cloneCommentPath(path), start: anchor, end: end}
337340
p.nodes = append(p.nodes, node)
338341
return node, end, true

common/json/internal/contextjson/context_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"errors"
77
"strings"
88
"testing"
9+
"time"
910

1011
json "github.com/sagernet/sing/common/json/internal/contextjson"
1112
)
@@ -881,3 +882,31 @@ func TestOmitZero(t *testing.T) {
881882
t.Fatalf("content = %s", content)
882883
}
883884
}
885+
886+
func TestUnmarshalTerminatesOnInvalidArrayElement(t *testing.T) {
887+
t.Parallel()
888+
// Regression: a stray byte where a key was expected inside an array
889+
// element (here U+3002 full-width period) caused the comment-node
890+
// parser to spin forever. parseLiteralEnd returns immediately at
891+
// delimiters like ':', and parseArray's loop did not enforce
892+
// forward progress. A leading comment is required to route the
893+
// input through the comment-node parser.
894+
const input = `{
895+
// comment
896+
"rules": [
897+
{
898+
。 "action": "sniff"
899+
}
900+
]
901+
}`
902+
done := make(chan error, 1)
903+
go func() {
904+
var value any
905+
done <- json.Unmarshal([]byte(input), &value)
906+
}()
907+
select {
908+
case <-done:
909+
case <-time.After(time.Second):
910+
t.Fatal("Unmarshal did not return within 1s (suspected infinite loop)")
911+
}
912+
}

0 commit comments

Comments
 (0)