Unicorn with delicious cookie
Nous utilisons des cookies pour améliorer votre expérience de navigation. En savoir plus
Accepter
to the top
>
>
>
V2632. MISRA. Object with temporary lif…
menu mobile close menu
Analyzer diagnostics
General Analysis (C++)
General Analysis (C#)
General Analysis (Java)
Micro-Optimizations (C++)
Diagnosis of 64-bit errors (Viva64, C++)
Customer specific requests (C++)
MISRA errors
AUTOSAR errors
OWASP errors (C++)
OWASP errors (C#)
OWASP errors (Java)
Problems related to code analyzer
Additional information
toggle menu Contents

V2632. MISRA. Object with temporary lifetime should not undergo array-to-pointer conversion.

07 Fév 2025

This diagnostic rule is based on the MISRA (Motor Industry Software Reliability Association) software development guidelines.

This diagnostic rule is relevant only for C.

Arrays as temporary objects should not be converted to a pointer. Temporary objects exist only for the duration of their complete value expression. They are destroyed immediately after the value expression completion.

An array can be a member of a structure or union and therefore form part of the result value of any value expression. Because an array used in an expression is always decayed to a pointer, in C it is possible to form a pointer to an array that is a sub-object of a temporary object. Modification of temporary array elements, as well as accessing them after their lifetime, leads to undefined behavior.

Look at the code example:

struct S
{
  int arr[10];
};

struct S getS(void);

void foo(int const *p);

void bar()
{
  p = getS().arr;         // <=
  foo(getS().arr);        // <=

  int j = getS().arr[3];
  getS().arr[3] = j;      // <=
}

The S structure contains the arr array of 10 elements as a data member. The analyzer issues a warning when trying to access this array via the temporary object.

To fix it, declare the object with a normal lifetime:

struct S
{
  int arr[10];
};

struct S s;
struct S getS(void);

void foo(int const* p);

void global_object()
{
  int* p = s.arr;
  s.arr[0] = 1;
  p[1] = 1;

  foo(s.arr);
}

void local_object()
{
  struct S obj = getS();
  int* p = obj.arr;
  obj.arr[0] = 1;
  p[1] = 1;

  foo(obj.arr);
}

This diagnostic is classified as:

  • MISRA-C-18.9
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