Skip to content

Loop counters should not be modified in the body of the loop. #822

@ArielSAdamsNASA

Description

@ArielSAdamsNASA

Describe the bug
Counter variable, i, is modified inside the for loop, which may lead to an infinite loop. Should use while loop instead.

Location
root/cfe/fsw/cfe-core/src/tbl/cfe_tbl_task_cmds.c
lines 1373 - 1380

Expected behavior
Use while loop or add comments explaining the reasoning of using for loop for future developers (must ensure loop issues will not occur such as the code looping infinitely). If using while loop, please provide maximum limit for counter.

Code snips

for (i=0; i<CFE_PLATFORM_TBL_MAX_CRITICAL_TABLES; i++)
{
if (strncmp(CFE_TBL_TaskData.CritReg[i].Name, TableName, CFE_TBL_MAX_FULL_NAME_LEN) == 0)
{
CritRegRecPtr = &CFE_TBL_TaskData.CritReg[i];
i=CFE_PLATFORM_TBL_MAX_CRITICAL_TABLES;
}
}

Observed on:
https://lgtm.com/projects/g/nasa/cFS/snapshot/20ff9b0c5aa6653815b9cd2feb4a02180b239737/files/cfe/fsw/cfe-core/src/tbl/cfe_tbl_task_cmds.c?sort=name&dir=ASC&mode=heatmap

Additional context
References: https://help.semmle.com/wiki/display/CCPPOBJ/For+loop+variable+changed+in+body

Reporter Info
Ariel Adams, ASRC Federal

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions