github-vet / github-vet/rangeloop-pointer-findings

hexya-erp/hexya: src/tools/generate/generate.go; 77 LoC

Open
#15,606 0 comments 0 reactions 0 assignees View on GitHub
fresh medium
Dominant language
No language data
Stars
0
Forks
0
PR merge metrics
PR metrics pending

Description

Found a possible issue in [hexya-erp/hexya](https://www.github.com/hexya-erp/hexya) at [src/tools/generate/generate.go](https://github.com/hexya-erp/hexya/blob/6dcd5b7b64067afd0d515dcbb63b4477d3c596fa/src/tools/generate/generate.go#L173-L249)

Below is the message reported by the analyzer for this snippet of code. Beware that the analyzer only reports the first issue it finds, so please do not limit your consideration to the contents of the below message.

>

[Click here to see the code in its original context.](https://github.com/hexya-erp/hexya/blob/6dcd5b7b64067afd0d515dcbb63b4477d3c596fa/src/tools/generate/generate.go#L173-L249)

Click here to show the 77 line(s) of Go which triggered the analyzer.

```go
for methodName, methodASTData := range modelASTData.Methods {
if handler, exists := specificMethodsHandlers[methodName]; exists {
handler(&methodASTData, modelData, depsMap)
continue
}
var params, paramsWithType, iParamsWithType, paramsType, call, returns, returnAsserts, returnString, iReturnString string
for _, astParam := range methodASTData.Params {
paramType := astParam.Type.Type
iParamType := trimInterfacePackagePrefix(paramType)
p := fmt.Sprintf("%s,", astParam.Name)
if isRS, _ := isRecordSetType(astParam.Type.Type, modelsASTData); isRS {
iParamType = fmt.Sprintf("%sSet", modelData.Name)
paramType = fmt.Sprintf("%s.%sSet", PoolInterfacesPackage, modelData.Name)
}
if astParam.Variadic {
iParamType = fmt.Sprintf("...%s", iParamType)
paramType = fmt.Sprintf("...%s", paramType)
}
params += p
paramsWithType += fmt.Sprintf("%s %s,", astParam.Name, paramType)
iParamsWithType += fmt.Sprintf("%s %s,", astParam.Name, iParamType)
paramsType += fmt.Sprintf("%s,", paramType)
(*depsMap)[astParam.Type.ImportPath] = true
}
if len(methodASTData.Returns) == 1 {
call = "Call"
(*depsMap)[methodASTData.Returns[0].ImportPath] = true
typ := methodASTData.Returns[0].Type
iTyp := trimInterfacePackagePrefix(typ)
returnAsserts = fmt.Sprintf("resTyped, _ := res.(%s)", typ)
returns = "resTyped"
if isRS, _ := isRecordSetType(typ, modelsASTData); isRS {
typ = fmt.Sprintf("%s.%sSet", PoolInterfacesPackage, modelData.Name)
iTyp = fmt.Sprintf("%sSet", modelData.Name)
returnAsserts = fmt.Sprintf("resTyped := res.(models.RecordSet).Collection().Wrap(\"%s\").(%s)", modelData.Name, typ)
}
returnString = typ
iReturnString = iTyp
} else if len(methodASTData.Returns) > 1 {
for i, ret := range methodASTData.Returns {
typ := ret.Type
iTyp := trimInterfacePackagePrefix(typ)
call = "CallMulti"
(*depsMap)[ret.ImportPath] = true
if isRS, _ := isRecordSetType(ret.Type, modelsASTData); isRS {
typ = fmt.Sprintf("%s.%sSet", PoolInterfacesPackage, modelData.Name)
iTyp = fmt.Sprintf("%sSet", modelData.Name)
returnAsserts += fmt.Sprintf("resTyped%d := res[%d].(models.RecordSet).Collection().Wrap(\"%s\").(%s)\n", i, i, modelData.Name, typ)
} else {
returnAsserts += fmt.Sprintf("resTyped%d, _ := res[%d].(%s)\n", i, i, typ)
}
returnString += fmt.Sprintf("%s,", typ)
iReturnString += fmt.Sprintf("%s,", iTyp)
returns += fmt.Sprintf("resTyped%d,", i)
}
}
modelData.AllMethods = append(modelData.AllMethods, methodData{
Name: methodName,
Doc: methodASTData.Doc,
ToDeclare: methodASTData.ToDeclare,
ParamsTypes: strings.TrimRight(paramsType, ","),
IParamsWithTypes: strings.TrimRight(iParamsWithType, ","),
ReturnString: strings.TrimSuffix(returnString, ","),
IReturnString: strings.TrimSuffix(iReturnString, ","),
})
modelData.Methods = append(modelData.Methods, methodData{
Name: methodName,
Doc: methodASTData.Doc,
ToDeclare: methodASTData.ToDeclare,
Params: strings.TrimRight(params, ","),
ParamsWithType: strings.TrimRight(paramsWithType, ","),
ReturnAsserts: strings.TrimSuffix(returnAsserts, "\n"),
Returns: strings.TrimSuffix(returns, ","),
ReturnString: strings.TrimSuffix(returnString, ","),
Call: call,
})
}

```

Click here to show extra information the analyzer produced.

```
No path was found through the callgraph that could lead to a function which writes a pointer argument.

No path was found through the callgraph that could lead to a function which passes a pointer to third-party code.

root signature {handler 3} was not found in the callgraph; reference was passed directly to third-party code
```

Leave a reaction on this issue to contribute to the project by classifying this instance as a **Bug** :-1:, **Mitigated** :+1:, or **Desirable Behavior** :rocket:
See the descriptions of the classifications [here](https://github.com/github-vet/rangeclosure-findings#how-can-i-help) for more information.

commit ID: 6dcd5b7b64067afd0d515dcbb63b4477d3c596fa

Contributor guide

No contributing guide indexed for this repository

Research direction

Read src/tools/generate/generate.go around lines 173-249 and inspect how methodASTData is referenced while iterating modelASTData.Methods. Reproduce the analyzer finding against the generator and determine whether generated method data can retain the wrong range-loop value; done means the behavior is confirmed or ruled out with a focused regression check.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.