Go to main content

Oracle® Solaris 64-bit Developer's Guide

Exit Print View

Updated: November 2020
 
 

4.3 Finding Errors With lint

The lint program from Oracle Developer Studio allows you to detect issues in the code such as non-portable codes and unreachable statements. The –errchk option detects potential 64-bit porting problems. You can also specify cc -v, which directs the compiler to perform additional and stricter semantic checks. The –v option also enables certain lint-like checks on the named files.

When you enhance code to be 64-bit safe, use the header files present in the Oracle Solaris operating system because these files have the correct definition of the derived types and data structures for the 64-bit compilation environment.

Use lint to check code that is written for both the 32-bit and the 64-bit compilation environment. Specify the –errchk=longptr64 option to generate LP64 warnings. Also use the –errchk=longptr64 flag, which checks portability to an environment for which the size of long integers and pointers is 64-bit and the size of plain integers is 32-bit. The –errchk=longptr64 flag checks assignments of pointer expressions and long integer expressions to plain integers, even when explicit casts are used.

Use the –errchk=longptr64,signext,sizematch option to find code where the normal ISO C value-preserving rules allow the extension of the sign of a signed-integral value in an expression of unsigned-integral type.

Use the –m64 option of lint when you want to check code that you intend to run in the Oracle Solaris 64-bit compilation environment only.

For a description of lint options, see the Oracle Developer Studio 12.6: C User's Guide.

lint warnings show the line number of the offending code, a message that describes the problem, and an indication of whether a pointer is involved. The warning message also indicates the sizes of the involved data types. When you know a pointer is involved and you know the size of the data types, you can find specific 64-bit problems and avoid the preexisting problems between 32-bit and smaller types.

Be aware, however, that even though lint gives warnings about potential 64-bit problems, it cannot detect all problems. Also, in many cases, code that is intentional and correct for the application generates a warning.

You can suppress the warning for a given line of code by placing a comment of the form "NOTE(LINTED("<optional message">))" on the previous line. This comment directive is useful when you want lint to ignore certain lines of code such as casts and assignments. When you use NOTE, include note.h. For more information, see the lint(1) man page.


Caution  -  Exercise extreme care when you use the "NOTE(LINTED("<optional message">))" comment because it can mask real problems.


The sample program and lint output following illustrate most of the lint warnings that can arise in code that is not 64-bit clean.

    1	#include <inttypes.h>
    2	#include <stdio.h>
    3	#include <stdlib.h>
    4	
    5	static char chararray[] = "abcdefghijklmnopqrstuvwxyz";
    6	
    7	static char *myfunc(int i)
    8	{	
    9		return(& chararray[i]);
   10	}
   11	
   12	void main(void)
   13	{
   14		int	intx;
   15		long	longx;
   16		char	*ptrx;
   17	
   18		(void) scanf("%d", &longx);
   19		intx = longx;
   20		ptrx = myfunc(longx);
   21		(void) printf("%d\n", longx);
   22		intx = ptrx;
   23		ptrx = intx;
   24		intx = (int)longx;
   25		ptrx = (char *)intx;
   26		intx = 2147483648L;
   27		intx = (int) 2147483648L;
   28		ptrx = myfunc(2147483648L);
   29	}

(19) warning: assignment of 64-bit integer to 32-bit integer
(20) warning: passing 64-bit integer arg, expecting 32-bit integer: myfunc(arg 1)
(22) warning: improper pointer/integer combination: op "="
(22) warning: conversion of pointer loses bits
(23) warning: improper pointer/integer combination: op "="
(23) warning: cast to pointer from 32-bit integer
(24) warning: cast from 64-bit integer to 32-bit integer
(25) warning: cast to pointer from 32-bit integer
(26) warning: 64-bit constant truncated to 32 bits by assignment
(27) warning: cast from 64-bit integer constant expression to 32-bit integer
(28) warning: passing 64-bit integer constant arg, expecting 32-bit integer: myfunc(arg 1)
function argument ( number ) type inconsistent with format
    scanf (arg 2) 	long * :: (format) int *	t.c(18)
    printf (arg 2) 	long  :: (format) int 	t.c(21)

(The lint warning that arises from line 27 of this code sample is issued when the constant expression does not fit into the type into which it is being cast.)

Warnings for a given source line can be suppressed by placing a /*LINTED*/ comment on the previous line. This is useful where you have really intended the code to be a specific way. However, the Oracle Developer Studio NOTE mechanism allows more fine-grained control. An example might be in the case of casts and assignments. For more information, see lint Directives or the lint(1) Oracle Developer Studio 12.6 man page.