Non-initialized variable

9 次查看(过去 30 天)
Sam
Sam 2021-5-23
评论: Sam 2021-5-24
Func_Process_ReadData ( uint8 OpStatus, uint8 *u8_DataOut, uint8 *u8_ErrorCode )
{
uint8 ErrorCode = INITIAL;
uint8 *ReadData = NULL;
Std_ReturnType ReturnCode = E_NOT_OK;
if(u8_DataOut != NULL)
{
ReadData = u8_DataOut;
ReturnCode = Call_DataServices_ReadData (ReadData);
}
ErrorCode = *u8_ErrorCode;
Status_Check (OpStatus);
Error_Check (ErrorCode);
return (ReturnCode);
}
Getting "Dereferenced value is read before being initialized." for this code snippet, even after adding the initializations. Can someone tell me, in case we are missing any steps? I'm getting this issue as an High Impact Defect on Polyspace BugFinder report.
  2 个评论
Walter Roberson
Walter Roberson 2021-5-23
Which variable is it complaining about?
Sam
Sam 2021-5-24
Not really sure. Since using CUI, we are only having the HTML report with us.

请先登录,再进行评论。

回答(2 个)

Sam
Sam 2021-5-24
Not really sure. Since using CUI, we are only having the HTML report with us.

Anirban
Anirban 2021-5-24
编辑:Anirban 2021-5-24
Since the message refers to a 'dereferenced value', it is probably referring to the line:
ErrorCode = *u8_ErrorCode;
I don't see the buffer that u8_ErrorCode points to, in the code snippet itself. Maybe, u8_ErrorCode has not been made to point to a buffer before being passed to Func_Process_ReadData ?
PS Your workflow of using the HTML report to locate an issue seems quite tedious. Since you do have access to the code, I am wondering why you are not reviewing the results in the Polyspace desktop UI (or in a web browser with Polyspace Access). Anyway, if you contact Technical Support, they might provide you better solutions with the HTML report.
  3 个评论
Anirban
Anirban 2021-5-24
Your two code snippets together do not seem to lead to a Non-initialized variable defect. You might have to contact Technical Support and provide more context.
However, I am showing you a possibility of how one might get a Non-initialized variable defect with a slight variation of your code snippets. If you remove the bold parts, it is essentially the two code snippets you sent. This does not lead to a Non-initialized variable defect. But with the addition of the bold code, there now exists a path where the buffer pointed to by u8_ErrorCode might be non-initialized (basically it is the path that goes through the else branch). You might have something like this going on in your code.
Without a self-contained reproduction like this, it is difficult to diagnose what is going on.
#include <stdlib.h>
#define uint8 unsigned char
#define Std_ReturnType int
uint8 getRandom();
void func() {
/*--------------------------------------From another file-----------------------------------------------------*/
uint8 u8_DataOut[5] = {0,0,0,0,0};
uint8 ErrorCode;
uint8 pathDecider = getRandom();
if(pathDecider) {
ErrorCode = 0;
}
else
{} //On a path going through this, ErrorCode will be non-initialized.
uint8 OpStatus = 0;
Func_Process_ReadData ( OpStatus, u8_DataOut, &ErrorCode );
/*-----------------------------------------------------------------------------------------------------------------*/
}
Std_ReturnType Func_Process_ReadData ( uint8 OpStatus, uint8 *u8_DataOut, uint8 *u8_ErrorCode )
{
uint8 ErrorCode = 0;
uint8 *ReadData = NULL;
Std_ReturnType ReturnCode = 1;
if(u8_DataOut != NULL)
{
ReadData = u8_DataOut;
ReturnCode = Call_DataServices_ReadData (ReadData);
}
ErrorCode = *u8_ErrorCode;
Status_Check (OpStatus);
Error_Check (ErrorCode);
return (ReturnCode);
}
Sam
Sam 2021-5-24
Thanks for the reply.
The code I have shared, is the flow that is there. We don't have any branching in that code snippet.
That's why wondering, if everything's correct, where are we going wrong. And hence, had to post here for a better understanding.
I'll again check the same, and try to re-consider the flow, if we can get something out of it.

请先登录,再进行评论。

产品


版本

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by