9.1 Variable may not have been initialized

6.9 Initialisation

Moderators: misra-c, david ward

Post Reply
ankitshah413
Posts: 14
Joined: Wed Feb 14, 2018 10:02 am
Company: Inform GmbH

9.1 Variable may not have been initialized

Post by ankitshah413 » Mon Feb 19, 2018 11:51 am

Hello,

I have an issue with MISRA 9.1 rule. In my code i have a "if" loop and inside that a variable is initialized and then is read after that. But when i do static analysis using pclint then i have a warning for 9.1 rule. Below is my code :

struct mgm2_info_return msg_ascu_info;

Code: Select all

 if(klr_ecuConf_t->feat_multislave)
    {
        msg_ascu_info = mgmF_massage_get_ascu_info();
    }
    msg_info = mgmF_massage_get_info();
The line where it is read and where warning occurs:

Code: Select all

 if( (((vF_mcms_fl_on((tm_uint8)V_FLONCMD_ACK)) ||
          (msg_info.pmcu_state ==  LDF_MASSSQC_STATE_ACTIVE))
                    &&((klr_ecuConf_t->feat_multislave)
                    || (msg_ascu_info.ascu_state ==  LDF_MASSSQC_STATE_ACTIVE))
        ) &&
        (msg_info.pmcu_trig_source == RqSource_ActvComf))
Do i need to write a justification or i need to initialize the entire structure first?

dg1980
Posts: 108
Joined: Wed Apr 27, 2016 2:33 pm
Company: Elektrobit Automotive GmbH

Re: 9.1 Variable may not have been initialized

Post by dg1980 » Tue Feb 20, 2018 10:07 am

Be advised that MISRA 2004 is superseded by MISRA 2012 and that rule 9.1 has become mandatory - no deviation permitted.
If msg_ascu_info is a stack variable and the two if statements shown are sequential you have a serious problem if klr_ecuConf_t->feat_multislave is zero.

ankitshah413
Posts: 14
Joined: Wed Feb 14, 2018 10:02 am
Company: Inform GmbH

Re: 9.1 Variable may not have been initialized

Post by ankitshah413 » Tue Feb 20, 2018 10:20 am

Yes, the two IF statements are sequential. But here klr_ecuConf_t->feat_multislave is assigned a value that is greater then zero at runtime as it is a slave variant.

But, it is not assigned any value in this function.

misra-c
Posts: 548
Joined: Thu Jan 05, 2006 1:11 pm

Re: 9.1 Variable may not have been initialized

Post by misra-c » Fri Apr 27, 2018 12:02 pm

This code will be a violation of rule 9.1 if it can be shown that this code can be reached when "klr_ecuConf_t->feat_multislave" is False and "msg_ascu_info" is unset. Without the full code it is not possible to give a definite answer.

If the code is very complex, it may not be possible for a static analysis to determine whether the above conditions are met or not.
---
Posted by and on behalf of
the MISRA C Working Group

Francois
Posts: 9
Joined: Thu Jul 13, 2017 2:22 pm
Company: TE CONNECTIVITY

Re: 9.1 Variable may not have been initialized

Post by Francois » Mon Apr 30, 2018 7:23 am

You may have to write your code differently and less complex.

1. The first IF doesn't perform a real test. Actually you're only test that the variable is not empty, instead of "== true/false/on/off/etc .
2. For the second IF, you know that the compiler will generate/use temporary variables/registers to store temporary test results.
My opinion is: Do it yourself :). Create local variables to store your checks.
Small tasks are easier to read/maintain/debug/optimize ^^

One example:

Code: Select all

..(vF_mcms_fl_on((tm_uint8)V_FLONCMD_ACK)) || (msg_info.pmcu_state ==  LDF_MASSSQC_STATE_ACTIVE)...
You're comparing return of a function with the result of a test.
i assume that the result of the function, even if you define a boolean type, is not the same type of the "==" test result...

Post Reply

Return to “6.9 Initialisation”