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

AdRoll/batchiepatchie: syncer/batchsync.go; 214 LoC

Open
#13,356 0 comments 0 reactions 0 assignees View on GitHub
fresh large
Dominant language
No language data
Stars
0
Forks
0
PR merge metrics
PR metrics pending

Description

Found a possible issue in [AdRoll/batchiepatchie](https://www.github.com/AdRoll/batchiepatchie) at [syncer/batchsync.go](https://github.com/AdRoll/batchiepatchie/blob/e86e28f43218f578d6b838b50a888b8f97ddd552/syncer/batchsync.go#L22-L235)

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.

> reference to queue was used in a composite literal at line 33

[Click here to see the code in its original context.](https://github.com/AdRoll/batchiepatchie/blob/e86e28f43218f578d6b838b50a888b8f97ddd552/syncer/batchsync.go#L22-L235)

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

```go
for _, queue := range queues {

/* We got to be careful to make sure we look up Job ID between job
* listings and their description. Since this is at minimum two calls
* to AWS Batch, the state of jobs might change between their
* invocations. Basically we can't be sure there are equal number of
* results in job_results and job_description_results */
job_results := make(map[string]*batch.JobSummary)
job_description_results := make(map[string]*batch.JobDetail)

list_jobs := batch.ListJobsInput{
JobQueue: &queue,
JobStatus: &status,
}

var jobList *batch.ListJobsOutput
var err error

joblistspan := opentracing.StartSpan("listJobs", opentracing.ChildOf(topspan.Context()))

for {
jobList, err = awsclients.Batch.ListJobs(&list_jobs)
if err != nil {
joblistspan.Finish()
return nil, err
}

for _, job := range jobList.JobSummaryList {
job_results[*job.JobId] = job
}

if jobList.NextToken == nil {
break
}

cp := string(*jobList.NextToken)
list_jobs.NextToken = &cp
}
joblistspan.Finish()

describe_jobs := batch.DescribeJobsInput{}
doDescriptionSync := func() error {
job_descriptions, err := awsclients.Batch.DescribeJobs(&describe_jobs)
if err != nil {
return err
}
for _, desc := range job_descriptions.Jobs {
job_description_results[*desc.JobId] = desc
}
return nil
}

/* Also synchronize job descriptions, if we found any jobs. */
if len(jobList.JobSummaryList) > 0 {
describejobsspan := opentracing.StartSpan("describeJobs", opentracing.ChildOf(topspan.Context()))
for _, job := range jobList.JobSummaryList {
job_id_copy := string(*job.JobId)
describe_jobs.Jobs = append(describe_jobs.Jobs, &job_id_copy)
// Maximum number of jobs you can submit to AWS Batch description call is 100
if len(describe_jobs.Jobs) >= 100 {
err = doDescriptionSync()
if err != nil {
describejobsspan.Finish()
return nil, err
}
describe_jobs = batch.DescribeJobsInput{}
}
}
if len(describe_jobs.Jobs) > 0 {
err = doDescriptionSync()
if err != nil {
describejobsspan.Finish()
return nil, err
}
}
describejobsspan.Finish()

log.Info("Fetched ", len(job_description_results), " job descriptions.")
}

jobs_to_insert := make([]*jobs.Job, 0)

for job_id, job := range job_results {
if desc, ok := job_description_results[job_id]; ok {

if desc.Status != nil {
if _, ok := job_summaries[queue]; ok {
switch *desc.Status {
case "SUBMITTED":
job_summaries[queue].Submitted++
case "PENDING":
job_summaries[queue].Pending++
case "RUNNABLE":
job_summaries[queue].Runnable++
case "STARTING":
job_summaries[queue].Starting++
case "RUNNING":
job_summaries[queue].Running++
}
}
}

timeout := -1
for _, value := range desc.Container.Environment {
if *value.Name == "PYBATCH_TIMEOUT" {
timeout, err = strconv.Atoi(*value.Value)
if err != nil {
timeout = -1
log.Warning("PYBATCH_TIMEOUT contains unparseable ", value.Value, " : ", err)
}
break
}
}

var stopped_at *time.Time

if desc.StoppedAt != nil {
tmp := time.Unix(*desc.StoppedAt/1000, (*desc.StoppedAt%1000)*1000000).UTC()
stopped_at = &tmp
}

command_line_json, err := json.Marshal(desc.Container.Command)
if err != nil {
log.Warning("Cannot marshal command line to JSON: ", err)
continue
}

status_reason := ""
var exit_code *int64

if desc.StatusReason != nil {
status_reason = *desc.StatusReason
}

var run_started_time *time.Time
var log_stream_name *string
var task_arn *string

if len(desc.Attempts) > 0 {
last_attempt := desc.Attempts[len(desc.Attempts)-1]
if last_attempt.Container != nil &&
last_attempt.Container.Reason != nil &&
len(*last_attempt.Container.Reason) > 0 {
status_reason = *last_attempt.Container.Reason
}
if last_attempt.StartedAt != nil {
tt := time.Unix(*last_attempt.StartedAt/1000, (*last_attempt.StartedAt%1000)*1000000).UTC()
run_started_time = &tt
}
if last_attempt.Container != nil && last_attempt.Container.ExitCode != nil {
var ec int64
ec = *last_attempt.Container.ExitCode
exit_code = &ec
}
if last_attempt.Container != nil && last_attempt.Container.LogStreamName != nil {
var lsn = *last_attempt.Container.LogStreamName
log_stream_name = &lsn
}
if last_attempt.Container != nil && last_attempt.Container.TaskArn != nil {
task_arn_c := *last_attempt.Container.TaskArn
task_arn = &task_arn_c
}
}

if log_stream_name == nil && desc.Container.LogStreamName != nil {
var lsn = *desc.Container.LogStreamName
log_stream_name = &lsn
}
if (task_arn == nil || *task_arn == "") && desc.Container.TaskArn != nil {
task_arn_c := *desc.Container.TaskArn
task_arn = &task_arn_c
}
known_job_ids[job_id] = true

image := ""
vcpus := int64(0)
memory := int64(0)
if desc.Container != nil {
if desc.Container.Image != nil {
image = *desc.Container.Image
}
if desc.Container.Vcpus != nil {
vcpus = *desc.Container.Vcpus
}
if desc.Container.Memory != nil {
memory = *desc.Container.Memory
}
}

jobs_to_insert = append(jobs_to_insert, &jobs.Job{
Id: *job.JobId,
Name: *job.JobName,
Status: *desc.Status,
Description: *desc.JobDefinition,
LastUpdated: time.Now().UTC(),
JobQueue: queue,
Image: image,
CreatedAt: time.Unix(*desc.CreatedAt/1000, (*desc.CreatedAt%1000)*1000000).UTC(),
StoppedAt: stopped_at,
VCpus: vcpus,
Memory: memory,
CommandLine: string(command_line_json),
Timeout: timeout,
StatusReason: &status_reason,
RunStartTime: run_started_time,
ExitCode: exit_code,
LogStreamName: log_stream_name,
TaskARN: task_arn,
})
}
}

storer.Store(jobs_to_insert)
}

```

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: e86e28f43218f578d6b838b50a888b8f97ddd552

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.