[previous] MISRA C:2012  Rule-7.4:  (Required) [next] A string literal shall not be assigned to an object unless the object's type is "pointer to const-qualified char"

Amplification:

No attempt shall be made to modify a string literal or wide string literal directly.

The result of the address-of operator, &, applied to a string literal shall not be assigned to an object unless that object's type is "pointer to array of const-qualifed char".

The same considerations apply to wide string literals. A wide string literal shall not be assigned to an object unless the object's type is "pointer to const-qualifed wchar_t". The result of the address-of operator, &, applied to a wide string literal shall not be assigned to an object unless that object's type is "pointer to array of const-qualifed wchar_t".

Example Code:


#pragma PRQA_MESSAGES_OFF 3206

#include "misra.h"
#include "m3cmex.h"

#define MSG "literal string one"

static void rule_0704a(PC * pa);
static void rule_0704b(const PC * cpa);
static void rule_0704c(const PC * const cpca);
static void rule_0704d(PC * const cpca);

extern int16_t rule_0704( void )
{
   PC    *ls_a = "literal string two";                /* 0753 */
   PC    *ls_b = "literal string three";              /* 0753 */
   const PC *ls_c = "literal string four";
   const PC *const ls_d = "literal string five";

   rule_0704a(MSG);                                   /* 0752 */
   rule_0704a(ls_a);
   rule_0704a(ls_b);

   rule_0704b(MSG);
   rule_0704b(ls_a);
   rule_0704b(ls_b);

   rule_0704c(MSG);
   rule_0704c(ls_a);
   rule_0704c(ls_b);

   rule_0704d(MSG);                                   /* 0752 */
   rule_0704d(ls_a);
   rule_0704d(ls_b);

   return 0;
}


static void rule_0704a(PC * pa)
{
   return;
}

static void rule_0704b(const PC * cpa)
{
   return;
}

static void rule_0704c(const PC * const cpca)
{
   return;
}

static void rule_0704d(PC * const cpca)
{
   return;
}


QAC messages that encompass this guideline:

0752 String literal passed as argument to function whose parameter is not a 'pointer to const'.
0753 String literal assigned to pointer which is not a 'pointer to const'.



(c) The Motor Industry Research Association, 2012
QA C Source Code Analyser 8.1.2
MISRA C:2012 Compliance Module 1.0
© 2013 Programming Research
www.programmingresearch.com
Personality Groups | Glossary | Message Index | MISRA C:2012 Rule Index Contents