Casting a NULL pointer function argument resulted in violation of Misra Rule 11.3
I define a NULL_PTR
as 0U
Then call a function with this NULL_PTR
as argument.
read_some_data(2U, (uint8_t *const) NULL_PTR, (uint8_t *const) NULL_PTR);
Called function prototype:
int16_t read_some_data(const uint8_t id, uint8_t *const data_1, uint8_t *const data_2);
On compilation, Misra raised a rule 11.3 violation error.(A ca开发者_开发知识库st should not be performed between a pointer type and an integral type.)
But if I just pass the NULL_PTR
as follows, no violation.
read_some_data(2U, NULL_PTR, NULL_PTR);
Which is the better way to do? Suppress Misra 11.3 rule or just pass the NULL_PTR
without casting?
What is wrong with the Standard 'NULL'?
Why cast if you can avoid it? Cast always makes code a little bit more dirty and hints that there is something hacky performed.
So I would just pass NULL_PTR without casting. After checking function specification that it can accept NULL_PTR as its second parameter!!!
I used NULL_PTR in my header file to avoid using IAR internal configuration file yvals.h which defines NULL. But that's not an issue since I may have to use yvals.h later due to other reasons.
Whether using NULL or NULL_PTR, I assume that the general consensus is to pass NULL without casting. My function doesn't have any problem in accepting it. This way, I can avoid suppressing Misra 11.3 rule.
Hope I am proceeding the right way.
On compilation, Misra raised a rule 11.3 violation error.
Just to be pedantic, MISRA did not raise a violation error, your compiler raised a MISRA C:2004 rule violation error.
From what you've posted, I'm not convinced the reported violation is correct...
On the other hand, personally I'd define NULL_PTR
as (void *)0u
because (strictly speaking) 0 is not a pointer.
--
For MISRA C:2004, Rule 11.3 is Advisory, providing a recommendation... the Rule also says that this situation may be unavoidable. As such, the violation may be ignored (with justification). If applying MISRA Compliance, the Rule may be disapplied.
Alternatively, the equivalent guideline in the newer version, MISRA C:2012 Rule 11.4 has an explicit exception for a null pointer constant.
精彩评论