10-02-2005, 01:09 PM
Note that MISRA C1 has been superseded by MISRA C2 the answer refers to MISRA C2 (MISRA-C:2004).
This question is in two parts:
Part 1 asks about the use of plain char in system libraries such as strcpy.
Rule 14 in MISRA C1 does indeed ban the use of plain char requiring it to be signed or unsigned. The clarification in MISRA C2 Rules 6.1, 6.2 and 6.3 is that there are three char types:
Rule 6.1 says plain char shall only be used for characters.
Rule 6.2 says signed and unsigned char shall only be used for numeric types.
Rule 6.3 says the ISO POSIX typedefs should be used.
Therefore you can use:
This is because char’s are not necessarily the same as 8 bit integers and many standard C libraries use \"char\" which may default to signed or unsigned or even be a multi-byte character.
Part 2 - Use of strcpy
You do not need to write you own version of strcpy. However, re-writing the system header/include files to be MISRA C compliant would be a good idea. We assume your own include files are MISRA C compliant anyway! Note that MISRA C is a coding standard and covers source code (such as the header files) but does not cover the compiled libraries.
As of January 2005 some compiler writers were already making their header files MISRA C compliant. Also any part of the standard library supplied in source for should be MISRA C compliant. Ask your compiler supplier if they are doing so or have plans to do so. Nothing works faster than customer pressure!
With regard to the use of strcpy note the rules on overlapping memory (see Rule 18.2).
This question is in two parts:
Part 1 asks about the use of plain char in system libraries such as strcpy.
Rule 14 in MISRA C1 does indeed ban the use of plain char requiring it to be signed or unsigned. The clarification in MISRA C2 Rules 6.1, 6.2 and 6.3 is that there are three char types:
Rule 6.1 says plain char shall only be used for characters.
Rule 6.2 says signed and unsigned char shall only be used for numeric types.
Rule 6.3 says the ISO POSIX typedefs should be used.
Therefore you can use:
Code:
unsigned char uint8_t /* for 8 bit integers but */
signed char int8_t /* NOT for characters */
char char_t /* ONLY for characters */
This is because char’s are not necessarily the same as 8 bit integers and many standard C libraries use \"char\" which may default to signed or unsigned or even be a multi-byte character.
Part 2 - Use of strcpy
You do not need to write you own version of strcpy. However, re-writing the system header/include files to be MISRA C compliant would be a good idea. We assume your own include files are MISRA C compliant anyway! Note that MISRA C is a coding standard and covers source code (such as the header files) but does not cover the compiled libraries.
As of January 2005 some compiler writers were already making their header files MISRA C compliant. Also any part of the standard library supplied in source for should be MISRA C compliant. Ask your compiler supplier if they are doing so or have plans to do so. Nothing works faster than customer pressure!
With regard to the use of strcpy note the rules on overlapping memory (see Rule 18.2).