Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
bgColor#FFcccc
langcpp

void f(char const *str, int slen) {
   char *p = const_cast<char*>(str);
   int i;
   for (i = 0; i < slen && str[i]; i++) {
      if (str[i] != ' ') *p++ = str[i];
   }
}

...

Code Block
bgColor#ccccff
langcpp

void f(char *str, int slen) {
   char *p = str;
   int i;
   for (i = 0; i < slen && str[i]; i++) {
      if (str[i] != ' ') *p++ = str[i];
   }
}

...

Code Block
bgColor#FFcccc
langcpp

int const vals[] = {3, 4, 5};
memset((int*) vals, 0, sizeof(vals));

...

Code Block
bgColorccccff
langcpp

int vals[] = {3, 4, 5};
memset(vals, 0, sizeof(vals));

...

EXP05-EX1: An exception to this rule is allowed when it is necessary to cast away const when invoking a legacy API that does not accept a const argument, provided the function does not attempt to modify the referenced variable. For example, the following code casts away the const qualification of INVFNAME in the call to the audit_log() function.

Code Block

/* Legacy function defined elsewhere - cannot be modified */
void audit_log(char *errstr) {
  fprintf(stderr, "Error: %s.\n", errstr);
}

/* ... */
const char INVFNAME[]  = "Invalid file name.";
audit_log(const_cast<char*>(INVFNAME)); /* EXP05-EX1 */
/* ... */

...

ECLAIR

Tool

Version

Checker

Description

Section

LDRA tool suite

7.6.0

 

 

Section

GCC

 

 

Section

Can detect violations of this recommendation when the -Wcast-qual flag is used.

Section

Compass/ROSE

 

 

 

Section
Include Page
ECLAIR_V
ECLAIR_V
Section

castexpr

section

CP1.EXP35


Fully

Implemented

implemented

Related Vulnerabilities

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

...