[Mono-dev] Patch for IntPtr bug

russell.kay at realtimeworlds.com russell.kay at realtimeworlds.com
Wed Feb 11 10:45:08 EST 2009

We get addresses on Linux that are >2Gb from malloc in the native code,
if we go through the conversion from int (i.e.)IntPtr.ToInt64() is not
giving a sign extended value back when this happens and then a cast to
IntPtr will fail. The cast from a pointer i.e. void* or byte* will also
not sign extend the long.

The conversion is not sign extending the long (perhaps because it does
actually fit)...

My patch is correct.


-----Original Message-----
From: mono-devel-list-bounces at lists.ximian.com
[mailto:mono-devel-list-bounces at lists.ximian.com] On Behalf Of Robert
Sent: 11 February 2009 15:24
To: mono-devel-list at lists.ximian.com
Subject: Re: [Mono-dev] Patch for IntPtr bug

russell.kay at realtimeworlds.com wrote:

> I encountered a problem when casting a long to an IntPtr, which is
> something we have ended up doing a lot (we want to keep binary
> compatibility between x86 and x64, only changing the native code on
> different architectures). We encountered a problem with Mono as there
> has been a check introduced into the IntPtr constructor from a long,
> this is checking the range of the long to ensure it is in range,
> unfortunately the check is incorrect. A long can easily hold from
> Int32.MinValue to UInt32.MaxValue (and not Int32.MaxValue as it
> currently in there).

The patch is wrong, since casting a long > Int32.MaxValue to a
4 byte signed intptr is simply invalid.

The current check in IntPtr.cs also matches MS.NET.


Mono-devel-list mailing list
Mono-devel-list at lists.ximian.com

This email has been scanned by the MessageLabs Email Security System


This message and any attachments contain privileged and confidential information intended for the use of the addressee named above. If you are not the intended recipient of this message, you are hereby notified that any use, dissemination, distribution or reproduction of this message is prohibited. Please note that we cannot guarantee that this message or any attachment is virus free or that it has not been intercepted and amended. The views of the author may not necessarily reflect those of Realtime Worlds Ltd.


Realtime Worlds Ltd is registered in Scotland, number 225628. Registered Office: 152 West Marketgait, Dundee, DD1 1NJ.

More information about the Mono-devel-list mailing list