CInt (Long) en VB.NET se comporter différemment dans des environnements 32 et 64 bits

StackOverflow https://stackoverflow.com/questions/2783826

  •  03-10-2019
  •  | 
  •  

Question

Aujourd'hui, j'ai eu un problème de conversion d'un long (Int64) à un nombre entier (Int32). Le problème est que mon code fonctionnait toujours dans des environnements 32 bits, mais lorsque je tente MÊME exécutable dans un ordinateur 64 bits, il se bloque avec une exception System.OverflowException.

J'ai préparé ce code de test dans Visual Studio 2008 dans un nouveau projet avec les paramètres par défaut:

Module Module1

    Sub Main()
      Dim alpha As Long = -1
      Dim delta As Integer

      Try
         delta = CInt(alpha And UInteger.MaxValue)
         Console.WriteLine("CINT OK")
         delta = Convert.ToInt32(alpha And UInteger.MaxValue)
         Console.WriteLine("Convert.ToInt32 OK")
      Catch ex As Exception
         Console.WriteLine(ex.GetType().ToString())
      Finally
         Console.ReadLine()
      End Try
   End Sub

End Module

Sur mes configurations 32 bits (Windows XP SP3 32 bits et Windows 7 32 bits), il imprime jusqu'à « CINT OK », mais dans l'ordinateur 64 bits (Windows 7 64 bits) que j'ai testé MÊME exécutable il affiche le nom de l'exception.

Est-ce un comportement documenté? J'ai essayé de trouver une référence, mais j'a lamentablement échoué.

Pour référence, je laisse le CIL Code trop:

.method public static void  Main() cil managed
{
  .entrypoint
  .custom instance void [mscorlib]System.STAThreadAttribute::.ctor() = ( 01 00 00 00 )
  // Code size       88 (0x58)
  .maxstack  2
  .locals init ([0] int64 alpha,
           [1] int32 delta,
           [2] class [mscorlib]System.Exception ex)
  IL_0000:  nop
  IL_0001:  ldc.i4.m1
  IL_0002:  conv.i8
  IL_0003:  stloc.0
  IL_0004:  nop
  .try
  {
    .try
    {
      IL_0005:  ldloc.0
      IL_0006:  ldc.i4.m1
      IL_0007:  conv.u8
      IL_0008:  and
      IL_0009:  conv.ovf.i4
      IL_000a:  stloc.1
      IL_000b:  ldstr      "CINT OK"
      IL_0010:  call       void [mscorlib]System.Console::WriteLine(string)
      IL_0015:  nop
      IL_0016:  ldloc.0
      IL_0017:  ldc.i4.m1
      IL_0018:  conv.u8
      IL_0019:  and
      IL_001a:  call       int32 [mscorlib]System.Convert::ToInt32(int64)
      IL_001f:  stloc.1
      IL_0020:  ldstr      "Convert.ToInt32 OK"
      IL_0025:  call       void [mscorlib]System.Console::WriteLine(string)
      IL_002a:  nop
      IL_002b:  leave.s    IL_0055
    }  // End .try
    catch [mscorlib]System.Exception
    {
      IL_002d:  dup
      IL_002e:  call       void [Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.ProjectData::SetProjectError(class [mscorlib]System.Exception)
      IL_0033:  stloc.2
      IL_0034:  nop
      IL_0035:  ldloc.2
      IL_0036:  callvirt   instance class [mscorlib]System.Type [mscorlib]System.Exception::GetType()
      IL_003b:  callvirt   instance string [mscorlib]System.Type::ToString()
      IL_0040:  call       void [mscorlib]System.Console::WriteLine(string)
      IL_0045:  nop
      IL_0046:  call       void [Microsoft.VisualBasic]Microsoft.VisualBasic.CompilerServices.ProjectData::ClearProjectError()
      IL_004b:  leave.s    IL_0055
    }  // End handler
  }  // End .try
  finally
  {
    IL_004d:  nop
    IL_004e:  call       string [mscorlib]System.Console::ReadLine()
    IL_0053:  pop
    IL_0054:  endfinally
  }  // End handler
  IL_0055:  nop
  IL_0056:  nop
  IL_0057:  ret
} // End of method Module1::Main

Je soupçonne que l'instruction qui se comporte différemment est soit conv.ovf.i4 ou la paire ldc.i4.m1 / conv.u8.

Qu'est-ce qui se passe?

Convert.ToInt32(long) échoue dans les deux environnements. Il est seulement CInt (Long) qui se comporte différemment.

Était-ce utile?

La solution

Malheureusement, la version 64 bits est exacte. Il est vraiment un débordement, le résultat de l'expression est un long avec la valeur et HFFFFFFFF. Le bit de signe est ET-ed de la valeur, il est plus négatif. La valeur résultante ne peut pas être convertie en un nombre entier, la valeur entière maximale est et h7fffffff. Vous pouvez voir cela en ajoutant ce code à votre extrait:

 Dim value As Long = alpha And UInteger.MaxValue
 Console.WriteLine(value)

Sortie: 4294967295

La gigue x64 utilise une toute autre façon de vérifier les trop-pleins, il ne repose pas sur l'exception de débordement de CPU, mais compare explicitement les valeurs à Integer.MAXVALUE et Integer.MinValue. La gigue x86 se trompe, il optimise le code trop et finit par faire une opération non signée qui ne déclenche pas l'exception du processeur.

Dépôt d'un rapport de bogue à connect.microsoft.com est probablement pas la peine, la fixation de ce pour la gigue x86 serait un changement de rupture drastique. Vous devrez retravailler cette logique. Je ne sais pas comment, je ne vois pas ce que vous essayez de faire.

Autres conseils

Je ne sais pas d'une véritable référence en tant que telle, mais si vous allez à cette page:

http://msdn.microsoft.com/en-us /library/system.int32.aspx

Vous pouvez voir dans l'échantillon où ils utilisent CInt ils ne l'envelopper dans un gestionnaire de OverflowException (Essayez CInt sur cette page pour le trouver). Donc, au moins ils disent implicitement que CInt peut jeter que dans certaines circonstances.

Si vous ne voulez pas que les exceptions sont lancées, vous pouvez modifier le réglage de Remove integer overflow checks sur la page Compile Options avancées.

Essayez de changer la construction cible plate-forme de « Any CPU » à « x86 ».

Juste pour compléter la documentation de cette question, je fait ceci:

Imports System.Runtime.InteropServices

Module Module1
   <DllImport("KERNEL32.DLL", EntryPoint:="DebugBreak", _
        SetLastError:=False, CharSet:=CharSet.Unicode, _
        ExactSpelling:=True, _
        CallingConvention:=CallingConvention.StdCall)> _
        Public Sub DebugBreak()
   End Sub

   Sub Main()
      Dim alpha As Long = -1
      Dim delta As Integer

      DebugBreak() ' To call OllyDbg
      ' Needed to prevent the jitter from raising the overflow exception in the second CInt without really doing the convertion first
      alpha = alpha Xor Environment.TickCount
      Console.WriteLine(alpha)

      delta = CInt(alpha And UInteger.MaxValue)
      Console.WriteLine(delta)

      alpha = alpha And UInteger.MaxValue
      delta = CInt(alpha)
      Console.WriteLine(delta)

      Console.ReadLine()
   End Sub
End Module

Utilisation OllyDbg je suis arrivé ceci:

CPU Disasm
Address   Hex dump          Command                                  Comments
00D10070    55              PUSH EBP
00D10071    8BEC            MOV EBP,ESP
00D10073    57              PUSH EDI
00D10074    56              PUSH ESI
00D10075    53              PUSH EBX
00D10076    E8 A1BFC7FF     CALL 0098C01C
00D1007B    E8 A18C1879     CALL <JMP.&KERNEL32.GetTickCount>        ; Jump to KERNEL32.GetTickCount
00D10080    99              CDQ
00D10081    F7D0            NOT EAX
00D10083    F7D2            NOT EDX
00D10085    8BF0            MOV ESI,EAX
00D10087    8BFA            MOV EDI,EDX
00D10089    E8 62D25D78     CALL 792ED2F0                            ; Called everytime Console is referenced here
00D1008E    57              PUSH EDI
00D1008F    56              PUSH ESI
00D10090    8BC8            MOV ECX,EAX
00D10092    8B01            MOV EAX,DWORD PTR DS:[ECX]
00D10094    FF90 C4000000   CALL DWORD PTR DS:[EAX+0C4]              ; Console.WriteLine(Int64)
00D1009A    8BDE            MOV EBX,ESI                              ; Note: EDI:ESI holds alpha variable
00D1009C    83E3 FF         AND EBX,FFFFFFFF                         ; delta = CInt(alpha And UInteger.MaxValue)
00D1009F    E8 4CD25D78     CALL 792ED2F0
00D100A4    8BC8            MOV ECX,EAX
00D100A6    8BD3            MOV EDX,EBX
00D100A8    8B01            MOV EAX,DWORD PTR DS:[ECX]
00D100AA    FF90 BC000000   CALL DWORD PTR DS:[EAX+0BC]              ; Console.WriteLine(Int32)
00D100B0    33FF            XOR EDI,EDI                              ; alpha = alpha And UInteger.MaxValue
00D100B2    85F6            TEST ESI,ESI                             ; delta = CInt(alpha) [Begins here]
00D100B4    7C 06           JL SHORT 00D100BC
00D100B6    85FF            TEST EDI,EDI
00D100B8    75 2B           JNE SHORT 00D100E5
00D100BA    EB 05           JMP SHORT 00D100C1
00D100BC    83FF FF         CMP EDI,-1
00D100BF    75 24           JNE SHORT 00D100E5
00D100C1    8BDE            MOV EBX,ESI                              ; delta = CInt(alpha) [Ends here]
00D100C3    E8 28D25D78     CALL 792ED2F0
00D100C8    8BC8            MOV ECX,EAX
00D100CA    8BD3            MOV EDX,EBX
00D100CC    8B01            MOV EAX,DWORD PTR DS:[ECX]
00D100CE    FF90 BC000000   CALL DWORD PTR DS:[EAX+0BC]              ; Console.WriteLine(Int32)
00D100D4    E8 1B1AA878     CALL 79791AF4
00D100D9    8BC8            MOV ECX,EAX
00D100DB    8B01            MOV EAX,DWORD PTR DS:[ECX]
00D100DD    FF50 64         CALL DWORD PTR DS:[EAX+64]
00D100E0    5B              POP EBX
00D100E1    5E              POP ESI
00D100E2    5F              POP EDI
00D100E3    5D              POP EBP
00D100E4    C3              RETN

Comme vous pouvez le voir la deuxième phrase de CInt est beaucoup plus complexe que juste Anding (ce qui pourrait effectivement être supprimée comme EBX ne changera pas et les EFLAGS ne sont pas consommés partout). L'origine probable de ce problème peut être vu dans la réponse de Hans

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top