Showing posts with label pointer. Show all posts
Showing posts with label pointer. Show all posts

Thursday, March 25, 2010

Do not convert pointer to pointer




A cast should not be performed between a pointer to object type and a different pointer to object type (misra2004_11_4_DoNotConvertPointerToPointer.rule)


Description:

"A cast should not be performed between a pointer to object type and a different pointer to object type. Conversions of this type may be invalid if the new pointer type requires a stricter alignment."

Note: This rule skips casting of void type.

Benefits:

Prevents incorrect pointer alignment.

Example:

void foo( ) {
   int* pi;
   char* i;

   i = (char*) pi; // Violation
   i = (char*) &i; // Violation
}

Repair:

Do not convert pointer to different pointer.

References:
MISRA-C:2004 Guidelines for the use of the C language in critical systems
Chapter 6, Section 11

Author
ParaSoft


Tags: cast, pointer, void, pointer alignment, MISRA, critical systems


Monday, March 22, 2010

Avoid indexing pointer




Array indexing shall be the only allowed form of pointer arithmetic (misra2004_17_4_AvoidIndexingPointerAsArray.rule)


Description:

"Array indexing is the only acceptable form of pointer arithmetic, because it is clearer and hence less error prone than pointer manipulation. This rule bans the explicit calculation of pointer values. Array indexing shall only be applied to objects defined as an array type. Any explicitly calculated pointer value has the potential to access unintended or invalid memory addresses. Pointers may go out of bounds of arrays or structures, or may even point to effectively arbitrary locations."

Drawbacks: For more complex code rule may not be able to check if there is indexed pointer which points to array. For such cases the rule may report false positives.

Labels