C and C++


LANG.CAST.PC.FLOAT : Float Pointer Conversion

Summary

A cast or coercion from pointer type fl* to pointer type T*, or from T* to fl*, where:

Properties

Class Name Float Pointer Conversion
Significance style
Mnemonic LANG.CAST.PC.FLOAT
Categories
Misra2004 Misra2004:12.12 The underlying bit representations of floating-point values shall not be used
AUTOSARC++14 AUTOSARC++14:M3-9-3 The underlying bit representations of floating-point values shall not be used.
MisraC++2008 MisraC++2008:3-9-3 The underlying bit representations of floating-point values shall not be used.
CWE CWE:704 Incorrect Type Conversion or Cast
  CWE:710 Improper Adherence to Coding Standards
JSF++ JSF++:147 The underlying bit representations of floating point numbers shall not be used in any way by the programmer.
  JSF++:182 Type casting from any type to or from pointers shall not be used.
  JSF++:183 Every possible measure should be taken to avoid type casting.
Availability Available for C and C++.
Enabling Checks for this warning class are disabled by default, and require the unnormalized C ASTs for the project. To enable them, add the following WARNING_FILTER rule and RETAIN_UNNORMALIZED_C_AST specification to the project configuration file.
RETAIN_UNNORMALIZED_C_AST = Yes
WARNING_FILTER += allow class="Float Pointer Conversion"
Note that retaining the unnormalized ASTs will increase the disk space used to store the project representation, and may make the analysis take longer.

Example

double * to_floatptr(float *fptr, void* vptr, int *intptr, long  int i ){
    float *localfptr;
    double d = (double) i;                       /* no pointer conversion */
    localfptr = (float *) &d;  /* 'Float Pointer Conversion' warning issued here (cast from double* to float*) */

    switch (i) {
    case 1:
        fptr = intptr;         /* 'Float Pointer Conversion' warning issued here (coercion from int* to float*) */
        break;
    case 2:
    fptr = (float*) intptr;    /* 'Float Pointer Conversion' warning issued here (cast  from int* to float*) */
        break;
    case 3:
        fptr = i;                                /* ok: coercion from non-pointer to float* */
        break;
    case 4:
        *fptr = *localfptr;                      /* no pointer conversion */
        break;
    default:
        fptr = vptr;                             /* ok: coercion from void* to float* */
    break;
    }
    return fptr;               /* 'Float Pointer Conversion' warning issued here (coercion from float* to double*) */
}

int from_floatptr(float *fptr, double d, int i){
    int localint = (int)fptr;                    /* ok: cast from float* to non-pointer */
    int *iptr;
    void *vptr = fptr;                           /* ok: coercion from float* to void* */

    switch (i){
    case 1:
        iptr = fptr;           /* 'Float Pointer Conversion' warning issued here (coercion from float* to int*) */
        break;
    case 2:
        iptr = (int *)&d;      /* 'Float Pointer Conversion' warning issued here (cast from double* to int*) */
        break;
    default:
        iptr = vptr;                             /* no float pointer involved */
        break;
    }
    return *iptr + localint;                     /* no float pointer involved */
}

Relevant Configuration File Parameters

The following configuration file parameters affect checks for this warning class.