From 58ed92ed500a20b7ba2209ce405f5dd2553342d2 Mon Sep 17 00:00:00 2001 From: Joseph Hickey Date: Tue, 1 Sep 2020 23:10:15 -0400 Subject: [PATCH] Fix #823, avoid infinite loop in CDS registry find The CFE_ES_FindCDSInRegistry function had an unusual loop control structure with mixed types of signed and unsigned. This has the possibility of being infinite if the MaxNumRegEntries is zero due to the way the end condition is structured. Simplify to be like other loops and use unsigned int control variable. --- fsw/cfe-core/src/es/cfe_es_cds.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/fsw/cfe-core/src/es/cfe_es_cds.c b/fsw/cfe-core/src/es/cfe_es_cds.c index 4cd699447..f4d3b5e3d 100644 --- a/fsw/cfe-core/src/es/cfe_es_cds.c +++ b/fsw/cfe-core/src/es/cfe_es_cds.c @@ -637,13 +637,10 @@ int32 CFE_ES_UnlockCDSRegistry(void) int32 CFE_ES_FindCDSInRegistry(const char *CDSName) { int32 RegIndx = CFE_ES_CDS_NOT_FOUND; - int32 i = -1; + uint32 i = 0; - do + while ( (RegIndx == CFE_ES_CDS_NOT_FOUND) && (i < CFE_ES_Global.CDSVars.MaxNumRegEntries) ) { - /* Point to next record in the CDS Registry */ - i++; - /* Check to see if the record is currently being used */ if (CFE_ES_Global.CDSVars.Registry[i].Taken == true) { @@ -654,7 +651,11 @@ int32 CFE_ES_FindCDSInRegistry(const char *CDSName) RegIndx = i; } } - } while ( (RegIndx == CFE_ES_CDS_NOT_FOUND) && (i < (CFE_ES_Global.CDSVars.MaxNumRegEntries-1)) ); + + /* Point to next record in the CDS Registry */ + i++; + + }; return RegIndx; } /* End of CFE_ES_FindCDSInRegistry() */ @@ -670,7 +671,7 @@ int32 CFE_ES_FindCDSInRegistry(const char *CDSName) int32 CFE_ES_FindFreeCDSRegistryEntry(void) { int32 RegIndx = CFE_ES_CDS_NOT_FOUND; - int32 i = 0; + uint32 i = 0; while ( (RegIndx == CFE_ES_CDS_NOT_FOUND) && (i < CFE_ES_Global.CDSVars.MaxNumRegEntries) ) {