While you might not agree with the MISRA rules, you are not supposed to find creative attempt such as wrapping your code in do {} while(0); to achieve a single point of exit, or as a way to write less code.
MISRA forbids goto as well, so although it's a common idiom to use goto to jump to a common exit point in a function, it will not pass the MISRA rules.
Even if you find it verbose, you are supposed write the code as e.g.
int32 CFE_ES_GetAppInfo(CFE_ES_AppInfo_t *AppInfo, uint32 AppId)
{
int32 ReturnCode = CFE_SUCCESS;
if ( AppInfo != 0 )
{
if ( AppId < CFE_ES_MAX_APPLICATIONS )
{
if ( CFE_ES_Global.AppTable[AppId].RecordUsed == TRUE )
{
CFE_ES_GetAppInfoInternal(AppId, AppInfo);
ReturnCode = CFE_SUCCESS;
}
else
{
CFE_ES_WriteToSysLog("CFE_ES_GetAppInfo: App ID Not Active: %d\n",(int)AppId);
ReturnCode = CFE_ES_ERR_APPID;
}
}
else
{
CFE_ES_WriteToSysLog("CFE_ES_GetAppInfo: App ID Exceeds CFE_ES_APPLICATION_MAX: %d\n",(int)AppId);
ReturnCode = CFE_ES_ERR_APPID;
}
}
else
{
CFE_ES_WriteToSysLog("CFE_ES_GetAppInfo: Invalid Parameter ( Null Pointer )\n");
ReturnCode = CFE_ES_ERR_BUFFER;
}
return(ReturnCode);
} /* End of CFE_ES_GetAppInfo() */
And you are not supposed to find workarounds to avoid nested if
statements to handle a single exit point.
If your code gets to many nested statements, you should rather break your function up into smaller pieces that each can handle its own part to reduce the nesting.
While the above code is verbose, it's still small enough to not need breaking up, but as a demonstration, it could be
static int32 CFE_ES_GetAppInfoImpl(CFE_ES_AppInfo_t *AppInfo, uint32 AppId)
{
int32 ReturnCode;
if ( CFE_ES_Global.AppTable[AppId].RecordUsed == TRUE )
{
CFE_ES_GetAppInfoInternal(AppId, AppInfo);
ReturnCode = CFE_SUCCESS;
}
else
{
CFE_ES_WriteToSysLog("CFE_ES_GetAppInfo: App ID Not Active: %d\n",(int)AppId);
ReturnCode = CFE_ES_ERR_APPID;
}
return ReturnCode;
}
int32 CFE_ES_GetAppInfo(CFE_ES_AppInfo_t *AppInfo, uint32 AppId)
{
int32 ReturnCode = CFE_SUCCESS;
if ( AppInfo != 0 )
{
if ( AppId < CFE_ES_MAX_APPLICATIONS )
{
ReturnCode = CFE_ES_GetAppInfoImpl(AppInfo, AppID);
}
else
{
CFE_ES_WriteToSysLog("CFE_ES_GetAppInfo: App ID Exceeds CFE_ES_APPLICATION_MAX: %d\n",(int)AppId);
ReturnCode = CFE_ES_ERR_APPID;
}
}
else
{
CFE_ES_WriteToSysLog("CFE_ES_GetAppInfo: Invalid Parameter ( Null Pointer )\n");
ReturnCode = CFE_ES_ERR_BUFFER;
}
return(ReturnCode);
} /* End of CFE_ES_GetAppInfo() */