Rule 11.5 and char/string constants - Printable Version +- MISRA Discussion Forums (https://forum.misra.org.uk) +-- Forum: MISRA C (https://forum.misra.org.uk/forumdisplay.php?fid=4) +--- Forum: MISRA-C: 2004 rules (https://forum.misra.org.uk/forumdisplay.php?fid=17) +---- Forum: 6.11 Pointer Type Conversions (https://forum.misra.org.uk/forumdisplay.php?fid=38) +---- Thread: Rule 11.5 and char/string constants (/showthread.php?tid=799) |
Rule 11.5 and char/string constants - armand - 12-01-2011 Hi, Whilst testing my implementation of rule 4.2 with a simple example, I got a warning about rule 11.5. It is on the following simple example: void main() { char * x="abcd"; return; } that is automatically translated by CIL into the following C code, where one can notice a new cast. void main() { char *x; x=(char *)"abcd"; } the 11.5 warning concerns the assignment, where it complains that the attribute const has been lost. Indeed, the rhs expression has type const char *, so this seems normal. A correct writing would be: void main() { char *x; char content[4]="abcd"; x=&content[0]; } which does not generate the 11.5 warning as we have well typed the constant and assigned the first element's address into x. BUT, now I get a warning about rule 9.2 explaining that I haven't a braced expression on the rhs of the assignement, to initialize the array!! I can correct this by decomposing "abcd" into {'a','b','c','d''} which removes the last warning but gets tedious. So my question is: does MISRA discourage programmers from using strings? Re: Rule 11.5 and char/string constants - Lundin - 13-01-2011 Perhaps you get the warning because the array is too small: C strings are null-terminated. Correct code would be content[5] or content[]. Do you get the same warning if you write char content[4] = {'a','b','c','d','\0'}; Apart from that bug, I don't think you should get any warning, though one can't really blame the tool vendor from generating it, as rule 9.2 doesn't actually mention strings at all. Perhaps some text should be added to 9.2 mentioning string initialization. Re: Rule 11.5 and char/string constants - armand - 13-01-2011 The tool "vendor" is me, as I am developing a MISRA rules checker within a static analysis tool. So I'm trying to make it as compliant as possible with the MISRA C 204 rules. If I write the assignment by decomposing the string into characters then I have no more 9.2 warning as the brackets are there. I guess that rule 9.2 should distinguish the case of strings that are a shortcut for arrays of chars. Re: Rule 11.5 and char/string constants - misra-c - 18-01-2011 Answering the points in turn:
As has been pointed out by Lundin, the code: Code: void main() { 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. |