misra

Best practice for compute the function return value

我怕爱的太早我们不能终老 提交于 2019-11-27 08:46:10
Often I built functions, in C, that checks some parameters and return an error code. Which is the best approach to stop the values checking when I found an error? First example: ErrorCode_e myCheckFunction( some params ) { ErrorCode_e error = CHECK_FAILED; if( foo == bar ) { if( foo_1 == bar_1 ) { if( foo_2 == bar_2 ) { error = CHECK_SUCCESS; } } } return error; } Second Example: ErrorCode_e myCheckFunction( some params ) { if( foo != bar ) { return CHECK_FAILED; } if( foo_1 != bar_1 ) { return CHECK_FAILED; } if( foo_2 != bar_2 ) { return CHECK_SUCCESS; } } I prefer the first approach because

Why does MISRA C state that a copy of pointers can cause a memory exception?

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-27 02:02:50
问题 MISRA C 2012 directive 4.12 is "Dynamic memory allocation should not be used". As an example, the document provides this sample of code: char *p = (char *) malloc(10); char *q; free(p); q = p; /* Undefined behaviour - value of p is indeterminate */ And the document states that: Although the value stored in the pointer is unchanged following the call to free, it is possible, on some targets, that the memory to which it points no longer exists and the act of copying that pointer could cause a

Best practice for compute the function return value

折月煮酒 提交于 2019-11-26 17:46:31
问题 Often I built functions, in C, that checks some parameters and return an error code. Which is the best approach to stop the values checking when I found an error? First example: ErrorCode_e myCheckFunction( some params ) { ErrorCode_e error = CHECK_FAILED; if( foo == bar ) { if( foo_1 == bar_1 ) { if( foo_2 == bar_2 ) { error = CHECK_SUCCESS; } } } return error; } Second Example: ErrorCode_e myCheckFunction( some params ) { if( foo != bar ) { return CHECK_FAILED; } if( foo_1 != bar_1 ) {

What is the benefit of terminating if … else if constructs with an else clause?

耗尽温柔 提交于 2019-11-26 09:20:40
问题 Our organization has a required coding rule (without any explanation) that: if … else if constructs should be terminated with an else clause Example 1: if ( x < 0 ) { x = 0; } /* else not needed */ Example 2: if ( x < 0 ) { x = 0; } else if ( y < 0 ) { x = 3; } else /* this else clause is required, even if the */ { /* programmer expects this will never be reached */ /* no change in value of x */ } What edge case is this designed to handle? What also concerns me about the reason is that