Unicorn with delicious cookie
Nous utilisons des cookies pour améliorer votre expérience de navigation. En savoir plus
Accepter
to the top
>
>
>
64-bit errors: LONG, LONG_PTR and blast…

64-bit errors: LONG, LONG_PTR and blast from the past

09 Mar 2023
Author:

64-bit errors are a thing of the bygone days. Very few developers are porting code from a 32-bit to a 64-bit system these days. Those who needed it have already ported their programs. Those who don't need it, they likely won't have to. But sometimes such errors make us recall them.

The short article "Adventures in application compatibility: The case of the display control panel crash on exit" brought the 64-bit errors issue to my mind. Perhaps our readers do not know or forgot that the PVS-Studio static analyzer was initially developed as a tool to detect 64-bit errors. At that time, the tool used to be called Viva64. So, the 64-bit errors issue is very close to us and makes us feel nostalgic :).

By 64-bit errors, we mean such code defects that may occur when porting an app from the 32-bit system to the 64-bit one. However, the code is incorrect on both systems. But it's just fortune that the code is running on the 32-bit system. Let's look at the following synthetic code fragment:

void *ptr = foo();
unsigned num = (unsigned)(ptr);
ptr = (void *)(num);

Someone decided to store the pointer value to the variable of the unsigned type. The code is incorrect. However, the sizes of the pointer and the variable of the unsigned type are the same on the 32-bit system, so everything works well. The problem occurs in the 64-bit system, where the unsigned type has most likely a 32-bit size and pointers become 64-bit. It will cause the loss of high bits in the pointer, when running the code. To prevent this, replace unsigned with uintptr_t.

This is exactly the case described in the mentioned article. The old Windows driver contains the code where the value is casting to LONG instead of LONGP_PTR:

SetWindowLong(GetDlgItem(m_dlg, IDC_SOME_BUTTON),
  GWL_WNDPROC, (LONG)g_originalWndProc);

Windows x64 has adopted the LLP64 data model in which variables of the long type (and LONG) are still 32-bit, as is the int type. It results in the loss of the high 32 bits, when casting to the LONG type. Here is the correct example:

SetWindowLong(GetDlgItem(m_dlg, IDC_SOME_BUTTON),
  GWL_WNDPROC, (LONG_PTR)g_originalWndProc);

In the article, such an old bug in the old driver requires editing directly in the executable binary code. "The later you find the problem, the harder and more expensive it is to fix it" principle is in action.

If you face the problem of porting code to the 64-bit system one day or are searching for errors in the already ported code, you are welcome to visit our website. There you will find the following:

Popular related articles

S'abonner

Comments (0)

close comment form
close form

Remplissez le formulaire ci‑dessous en 2 étapes simples :

Vos coordonnées :

Étape 1
Félicitations ! Voici votre code promo !

Type de licence souhaité :

Étape 2
Team license
Enterprise licence
** En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité
close form
Demandez des tarifs
Nouvelle licence
Renouvellement de licence
--Sélectionnez la devise--
USD
EUR
* En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité

close form
La licence PVS‑Studio gratuit pour les spécialistes Microsoft MVP
close form
Pour obtenir la licence de votre projet open source, s’il vous plait rempliez ce formulaire
* En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité

close form
I want to join the test
* En cliquant sur ce bouton, vous déclarez accepter notre politique de confidentialité

close form
check circle
Votre message a été envoyé.

Nous vous répondrons à


Si l'e-mail n'apparaît pas dans votre boîte de réception, recherchez-le dans l'un des dossiers suivants:

  • Promotion
  • Notifications
  • Spam