Skip to content

Commit 692162c

Browse files
Explicitly validate the len of params to ensure we don't get "index out of bounds" by CodeQL (#126)
1 parent d5c0135 commit 692162c

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

mockgen/mockgen.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -342,16 +342,17 @@ func (g *generator) generateOngoingVerificationGetCapturedArguments(ongoingVerif
342342
}
343343

344344
func (g *generator) generateOngoingVerificationGetAllCapturedArguments(ongoingVerificationStructName string, typeParamNames string, argTypes []string, isVariadic bool) *generator {
345-
argsAsArray := make([]string, len(argTypes))
345+
numArgs := len(argTypes)
346+
argsAsArray := make([]string, numArgs)
346347
for i, argType := range argTypes {
347348
argsAsArray[i] = fmt.Sprintf("_param%v []%v", i, argType)
348349
}
349350
g.p("func (c *%v%v) GetAllCapturedArguments() (%v) {", ongoingVerificationStructName, typeParamNames, strings.Join(argsAsArray, ", "))
350-
if len(argTypes) > 0 {
351+
if numArgs > 0 {
351352
g.p("_params := pegomock.GetGenericMockFrom(c.mock).GetInvocationParams(c.methodInvocations)")
352353
g.p("if len(_params) > 0 {")
353354
for i, argType := range argTypes {
354-
if isVariadic && i == len(argTypes)-1 {
355+
if isVariadic && i == numArgs-1 {
355356
variadicBasicType := strings.Replace(argType, "[]", "", 1)
356357
g.
357358
p("_param%v = make([]%v, len(c.methodInvocations))", i, argType).
@@ -365,10 +366,13 @@ func (g *generator) generateOngoingVerificationGetAllCapturedArguments(ongoingVe
365366
p("}")
366367
break
367368
} else {
369+
// explicitly validate the length of the params slice to avoid out of bounds code smells
370+
g.p("if len(_params) > %v {", i)
368371
g.p("_param%v = make([]%v, len(c.methodInvocations))", i, argType)
369372
g.p("for u, param := range _params[%v] {", i)
370373
g.p("_param%v[u]=param.(%v)", i, argType)
371374
g.p("}")
375+
g.p("}")
372376
}
373377
}
374378
g.p("}")

0 commit comments

Comments
 (0)