Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rule 11.5 and char/string constants
#4
Answering the points in turn:
  1. Section 6.5.7 of the C90 Standard states that an array of character type may be initialised by a character string literal optionally enclosed in braces. The wording of Rule 9.2 does not cover this particular case but there is no reason to require the string literal be enclosed in braces and therefore no reason for a tool to produce a diagnostic. The rule will be updated in the next revision of the MISRA C Guidelines to make this clear.
  2. Section 6.1.4 of the C90 Standard states that a character string literal behaves as an array of type char. It also states that the behaviour is undefined if a program attempts to modify a string literal. However, it does not make any statement about the const qualifier. Further, the C99 Rationale (admittedly not applicable to MISRA C:2004 but a useful source of guidance anyway) states that this was done intentionally. Therefore it is incorrect to assume that "abcd" has type const char *, even though it may behave as if it did. It follows that it is incorrect to issue a diagnostic against Rule 11.5 for
    Code:
    char * x="abcd";

As has been pointed out by Lundin, the code:
Code:
void main() {
char *x;
char content[4]="abcd";
x=&content[0];
}

is not equivalent. The array has 4 elements which contain the 4 characters from the string literal but there is no room for the null character to terminate the string. This is likely to give rise to problems if the array is treated as a string. The array either needs to be the same size as the string literal including the terminating character or the array size should be left unspecified in which case it will precisely the right number of characters.
Posted by and on behalf of the MISRA C Working Group
Reply


Messages In This Thread

Forum Jump:


Users browsing this thread: 1 Guest(s)