Versions Compared

Key

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

...

Code Block
enum { max = 15 };
int i = /* initializeInitialize to user supplied value */;

if ( (i >= 0) && ( (i++) <= max) ) {
  /* codeCode */
}

Although the behavior is well defined, it is not immediately obvious whether or not i gets incremented.

...

Code Block
bgColor#ffcccc
langc
char *p = /* initializeInitialize, may or may not be NULL */

if (p || (p = (char *) malloc(BUF_SIZE)) ) {
  /* doDo stuff with p */
  free(p);
  p = NULL;
}
else {
  /* handleHandle malloc() error */
  return;
}

Because malloc() is called only called if p is NULL when entering the if clause, free() might be called with a pointer to local data not allocated by malloc(). (See MEM34-C. Only free memory allocated dynamically.) This behavior is partially due to the uncertainty of whether or not malloc() is actually called.

...

Code Block
bgColor#ccccff
langc
char *p;
char *q = NULL;
if (p == NULL) {
  q = (char *) malloc(BUF_SIZE);
  p = q;
}
if (p == NULL) {
  /* handleHandle malloc() error */
  return;
}

/* doDo stuff with p */
free(q);
q = NULL;

...

Tool

Version

Checker

Description

Compass/ROSE

 

 

Could detect possible violations of this recommendation by reporting expressions with side effects, including function calls, that appear on the right-hand - side of an && or || operator

LDRA tool suite

Include Page
LDRA_V
LDRA_V

35 D
1 Q
133 S
406 S
408 S

Fully implemented

PRQA QA-C
Include Page
PRQA_V
PRQA_V
3415Fully implemented

...