Skip to main content

IAR Embedded Workbench for RX 5.20

FPT-misuse

In this section:
Synopsis

A function pointer is used in an invalid context.

Enabled by default

Yes

Severity/Certainty

Low/High

lowhigh.png
Full description

A function pointer is used in an invalid context. It is an error to use a function pointer to do anything other than calling the function being pointed to, comparing the function pointer to another pointer using != or ==, passing the function pointer to a function, returning the function pointer from a function, or storing the function pointer in a data structure. Misusing a function pointer might result in erroneous behavior, and in junk data being interpreted as instructions and being executed as such.

Coding standards
CERT EXP16-C

Do not compare function pointers to constant values

CWE 480

Use of Incorrect Operator

Code examples

The following code example fails the check and will give a warning:

/* declare a function */
int foo(int x, int y){
  return x+y;
}

#pragma diag_suppress=Pa153

int foo2(int x, int y) {

  if (foo)
    return (foo)(x,y);

  if (foo < foo2)
    return (foo)(x,y);
return 0;
}

The following code example passes the check and will not give a warning about this issue:

typedef int (*fptr)(int,int);

int f_add(int x, int y){
  return x+y;

}

int f_sub(int x, int y){
  return x-y;
}


int foo(int opcode, int x, int y){
  
  fptr farray[2];
  farray[0] = f_add;
  farray[1] = f_sub;

  return (farray[opcode])(x,y);

}

int foo2(fptr f1, fptr f2){

  if (f1 == f2)
    return 1;
  else
    return 0;

}