Non-initialized variable

조회 수: 3 (최근 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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by