MCPcopy Create free account
hub / github.com/cel-expr/cel-go / hasEmptyCollectionWithComma

Function hasEmptyCollectionWithComma

fuzzing/fuzz_pratt_parser_test.go:114–167  ·  view source on GitHub ↗

ANTLR grammar allows a standalone comma in empty lists, maps, and structs (e.g., '[,]', '{,}', 'Msg{,}'), whereas Pratt parser requires at least one element/entry before a trailing comma.

(antlrAST *ast.AST, text string)

Source from the content-addressed store, hash-verified

112// (e.g., '[,]', '{,}', 'Msg{,}'), whereas Pratt parser requires at least one element/entry
113// before a trailing comma.
114func hasEmptyCollectionWithComma(antlrAST *ast.AST, text string) bool {
115 if antlrAST == nil {
116 return false
117 }
118 sourceInfo := antlrAST.SourceInfo()
119 rbuf := runes.NewBuffer(text)
120 for _, node := range getAllNodes(antlrAST) {
121 expr := node
122 isEmptyCollection := false
123 closeChar := '}'
124 switch expr.Kind() {
125 case ast.ListKind:
126 if expr.AsList().Size() == 0 {
127 isEmptyCollection = true
128 closeChar = ']'
129 }
130 case ast.MapKind:
131 if expr.AsMap().Size() == 0 {
132 isEmptyCollection = true
133 }
134 case ast.StructKind:
135 if len(expr.AsStruct().Fields()) == 0 {
136 isEmptyCollection = true
137 }
138 }
139 if !isEmptyCollection {
140 continue
141 }
142 offsetRange, found := sourceInfo.GetOffsetRange(expr.ID())
143 if !found || offsetRange.Start < 0 || int(offsetRange.Start) >= rbuf.Len() {
144 continue
145 }
146 openPos := int(offsetRange.Start)
147 hasComma := false
148 for i := openPos + 1; i < rbuf.Len(); i++ {
149 r := rbuf.Get(i)
150 if r == '/' && i+1 < rbuf.Len() && rbuf.Get(i+1) == '/' {
151 for i += 2; i < rbuf.Len() && rbuf.Get(i) != '\n'; i++ {
152 }
153 continue
154 }
155 if r == closeChar {
156 if hasComma {
157 return true
158 }
159 break
160 }
161 if r == ',' {
162 hasComma = true
163 }
164 }
165 }
166 return false
167}
168
169// hasOnlyRecursionLimitErrors returns true if all parse errors are recursion limit exceeded errors.
170func hasOnlyRecursionLimitErrors(errs *common.Errors) bool {

Callers 1

FuzzPrattParserFunction · 0.85

Calls 13

LenMethod · 0.95
GetMethod · 0.95
NewBufferFunction · 0.92
getAllNodesFunction · 0.85
GetOffsetRangeMethod · 0.80
KindMethod · 0.65
SizeMethod · 0.65
AsListMethod · 0.65
AsMapMethod · 0.65
FieldsMethod · 0.65
AsStructMethod · 0.65
IDMethod · 0.65

Tested by

no test coverage detected