>
>
>
64-bit code issues in real programs: po…

Andrey Karpov
Articles: 671

64-bit code issues in real programs: pointer type change

Explicit type conversions often mask errors related to a change of a pointer type. One of such errors is casting of a pointer to 32-bit objects into a pointer to 64-bit ones.

Let us look at one example received from the users of our tool PVS-Studio (Viva64). The error shows after porting the code to the 64-bit Windows:

void ProcessTime(long * pTime)
{
  time((time_t *)pTime);
}

In a 32-bit program, the 32-bit version of the type time_t was used. On a 64-bit system, only the 64-bit version of time_t type is available. This is explicitly defined in the header file crtdefs.h:

#ifdef  _USE_32BIT_TIME_T
#ifdef  _WIN64
#error You cannot use 32-bit time_t (_USE_32BIT_TIME_T) with _WIN64
#endif
#ifdef _USE_32BIT_TIME_T
typedef __time32_t time_t;
#else
typedef __time64_t time_t;
#endif

The explicit type conversion we have demonstrated, can lead to an unpredictable program behavior or crash. It is difficult to diagnose such errors as the construct of the explicit type conversion suppresses the compiler warnings (see the note: "Search of explicit type conversion errors in 64-bit programs").

The diagnostic warning "V114. Dangerous explicit type pointer conversion" generated by PVS-Studio (Viva64) code analyzer when checking 64-bit projects helps detect such errors. Besides the example given above, the diagnostic warning V114 can detect a similar error related to a change of an array type:

int array[4] = { 1, 2, 3, 4 };
size_t *sizetPtr = (size_t *)(array);
cout << sizetPtr[1] << endl;
Result on the 32-bit system: 2
Result on the 64-bit system: 17179869187