Non-initialized variable

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 Commenti

Which variable is it complaining about?
Sam
Sam il 24 Mag 2021
Not really sure. Since using CUI, we are only having the HTML report with us.

Accedi per commentare.

Risposte (2)

Sam
Sam il 24 Mag 2021

0 voti

Not really sure. Since using CUI, we are only having the HTML report with us.
Anirban
Anirban il 24 Mag 2021
Modificato: Anirban il 24 Mag 2021

0 voti

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 Commenti

Sam
Sam il 24 Mag 2021
Thanks for the reply.
Yes, I have noticed that too, since it's a point on Dereferencing a value, probably it's a pointer to refer.
The calling function is using this code snippet:-
/*--------------------------------------From another file-----------------------------------------------------*/
uint8 u8_DataOut[5] = {some initials};
uint8 ErrorCode = 0;
uint8 OpStatus = 0;
Func_Process_ReadData ( OpStatus, u8_DataOut, &ErrorCode );
/*-----------------------------------------------------------------------------------------------------------------*/
Anirban
Anirban il 24 Mag 2021
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 il 24 Mag 2021
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.

Accedi per commentare.

Prodotti

Release

R2019b

Richiesto:

Sam
il 23 Mag 2021

Commentato:

Sam
il 24 Mag 2021

Community Treasure Hunt

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

Start Hunting!

Translated by