Go to main content
Oracle® Developer Studio 12.6: C User's Guide

Exit Print View

Updated: July 2017
 
 

6.3 Checking With lint

The lint program recognizes the same levels of type-based alias-disambiguation as the compiler’s -xalias_level command. The lint program also recognizes the pragmas related to type-based alias-disambiguation documented in this chapter. For a detailed explanation of the lint -Xalias_level command, see -Xalias_level[=l].

Four situations that lint detects and generates warnings are:

  • Casting a scalar pointer to a struct pointer

  • Casting a void pointer to a struct pointer

  • Casting a structure field to a scalar pointer

  • Casting a struct pointer to a struct pointer at the level of -Xalias_level=strict without explicit aliasing

6.3.1 Struct Pointer Cast of Scalar Pointer

In the following example, the pointer p of type integer is cast as a pointer of type struct foo. With lint -Xalias_level=weak (or higher), this example generates an error.

struct foo {
    int a;
    int b;
  };

struct foo *f;
int *p;

void main()
{
    f = (struct foo *)p; /* struct pointer cast of scalar pointer error */
}

6.3.2 Struct Pointer Cast of Void Pointer

In the following example, the void pointer vp, is cast as a struct pointer. With lint -Xalias_level=weak (or higher), this example generates a warning.

struct foo {
    int a;
    int b;
  };

struct foo *f;
void *vp;

void main()
{
    f = (struct foo *)vp; /* struct pointer cast of void pointer warning */
}

6.3.3 Cast of Struct Field to Structure Pointer

In the following example, the address of structure member foo.b is being cast as a struct pointer and then assigned to f2. With lint -Xalias_level=weak (or higher), this example generates an error.

struct foo{
    int a;
    int b;
  };

struct foo *f1;
struct foo *f2;

void main()
{
    f2 = (struct foo *)&f1->b; /* cast of a scalar pointer to struct pointer error*/
}

6.3.4 Explicit Aliasing Required

In the following example, the pointer f1 of type struct fooa is being cast as a pointer of type struct foob. With lint -Xalias_level=strict (or higher) such a cast requires explicit aliasing, unless the struct types are identical (the same number of fields of the same type). In addition, at alias levels standard and strong, the assumptions is that the tags must match for aliasing to occur. Use #pragma alias (struct fooa, struct foob) before the assignment to f1 and lint stops generating the warning.

struct fooa {
    int a;
};

struct foob {
    int b;
};

struct fooa *f1;
struct foob *f2;

void main()
{
    f1 = (struct fooa *)f2; /* explicit aliasing required warning */
}