64 Bit Pointer Isssue

Hello guys, i’m having a problem with existing code where they’ve typecasted a structure to a pointer of struct type.It worked in 32 bit mode but now in the process of porting to 64 bit,there seems to be trouble. I get the error ‘type cast’ : conversion from ‘ModuleSize’ to ‘ModuleSize *’ of greater size.I thought of every possibilty of doing the cast but couldn’t succeed.

ModuleSize* cbModSize;
ModuleSize m_PreModuleSize;
cbModSize = ( ModuleSize * )( m_PreModuleSize ? m_PreModuleSize + m_pModulePgInfo[0].m_recsize : m_pModulePgInfo[0].m_recsize );

What alternative can i implement to safely typecast the structure?

Hi,

You have 2 issues with this code (I’m assuming that ModuleSize is a struct
and not operator+ overloading class):

  1. Why do you typecast the ModuleSize into a ModuleSize *? Perhaps you
    wanted cbModSize to hold an offset into m_PreModuleSize, then you
    should have used &m_PreModuleSize.
  2. The conditional assignment is superflous. If m_PreModuleSize is NULL,
    then m_PreModuleSize + m_pModulePgInfo[0].m_recsize is equal to
    m_pModulePgInfo[0].m_recsize. So you shouldn’t use the ?: statement at
    all.

Leonid.

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of
xxxxx@gmail.com
Sent: Friday, October 17, 2008 9:58 AM
To: Windows System Software Devs Interest List
Subject: [ntdev] 64 Bit Pointer Isssue

Hello guys, i’m having a problem with existing code where they’ve typecasted
a structure to a pointer of struct type.It worked in 32 bit mode but now in
the process of porting to 64 bit,there seems to be trouble. I get the error
‘type cast’ : conversion from ‘ModuleSize’ to ‘ModuleSize *’ of greater
size.I thought of every possibilty of doing the cast but couldn’t succeed.

ModuleSize* cbModSize;
ModuleSize m_PreModuleSize;
cbModSize = ( ModuleSize * )( m_PreModuleSize ? m_PreModuleSize +
m_pModulePgInfo[0].m_recsize : m_pModulePgInfo[0].m_recsize );

What alternative can i implement to safely typecast the structure?


NTDEV is sponsored by OSR

For our schedule of WDF, WDM, debugging and other seminars visit:
http://www.osr.com/seminars

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer

__________ Information from ESET NOD32 Antivirus, version of virus signature
database 3530 (20081017) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com

__________ Information from ESET NOD32 Antivirus, version of virus signature
database 3530 (20081017) __________

The message was checked by ESET NOD32 Antivirus.

http://www.eset.com

Thx Leonid,the code is actually not written by me.It was written by someone else for 32 bit driver which i have to convert to 64 bit driver.

xxxxx@gmail.com wrote:

Hello guys, i’m having a problem with existing code where they’ve typecasted a structure to a pointer of struct type.It worked in 32 bit mode but now in the process of porting to 64 bit,there seems to be trouble. I get the error ‘type cast’ : conversion from ‘ModuleSize’ to ‘ModuleSize *’ of greater size.I thought of every possibilty of doing the cast but couldn’t succeed.

ModuleSize* cbModSize;
ModuleSize m_PreModuleSize;
cbModSize = ( ModuleSize * )( m_PreModuleSize ? m_PreModuleSize + m_pModulePgInfo[0].m_recsize : m_pModulePgInfo[0].m_recsize );

What alternative can i implement to safely typecast the structure?

This code is not correct. You must have made a cut-and-paste error. If
ModuleSize is a structure, then the expression “m_PreModuleSize +
m_pModulePgInfo[0].m_recsize” would not compile. You can’t add to a
structure.


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.

What’s the definition of ModuleSize? Also, you’re not showing us the
whole picture here, because the way that code is, m_PreModuleSize is an
uninitialized local variable. How is it actually set?

It is named as though it’s an (unsigned?) integer, but there’s no way to
determine what’s going on here without knowing. It might also be a
(really badly named) pointer, I suppose… but then the following code
doesn’t make a lot of sense.

Also, what is cbModSize used for *after* these lines of code? It kind of
doesn’t make sense that its a pointer, given the names of what it’s
created from.

Anyway, if I *had* to guess what was going on here, I’d guess that
ModuleSize is a pointer, m_recsize is a 32 bit integer, and the *normal*
operation of this line of code is to find some offset to the pointer.
However, if m_PreModuleSize is NULL, then I’m guessing someone earlier
on stuffed some other precomputed pointer into m_recsize (which is a
*really* bad idea), and it used to fit because m_recsize is 32 bits.

It sounds to me like this module needs to be redesigned for 64 bits,
because it appears to be a pile of spaghetti code.

xxxxx@gmail.com wrote:

Hello guys, i’m having a problem with existing code where they’ve typecasted a structure to a pointer of struct type.It worked in 32 bit mode but now in the process of porting to 64 bit,there seems to be trouble. I get the error ‘type cast’ : conversion from ‘ModuleSize’ to ‘ModuleSize *’ of greater size.I thought of every possibilty of doing the cast but couldn’t succeed.

ModuleSize* cbModSize;
ModuleSize m_PreModuleSize;
cbModSize = ( ModuleSize * )( m_PreModuleSize ? m_PreModuleSize + m_pModulePgInfo[0].m_recsize : m_pModulePgInfo[0].m_recsize );

What alternative can i implement to safely typecast the structure?


Ray
(If you want to reply to me off list, please remove “spamblock.” from my
email address)

xxxxx@gmail.com wrote:

Hello guys, i’m having a problem with existing code where they’ve
typecasted a structure to a pointer of struct type.It worked
in 32 bit mode but now in the process of porting to 64 bit,
there seems to be trouble. I get the error ‘type cast’ : conversion
from ‘ModuleSize’ to ‘ModuleSize *’ of greater size.I thought of
every possibilty of doing the cast but couldn’t succeed.

ModuleSize* cbModSize;
ModuleSize m_PreModuleSize;
cbModSize = ( ModuleSize * )( m_PreModuleSize ? m_PreModuleSize + m_pModulePgInfo[0].m_recsize : m_pModulePgInfo[0].m_recsize );

What alternative can i implement to safely typecast the structure?

Dreadful code. If the author wanted a union he should have used a union.

It would help if you gave the definitions of ModuleSize and whatever
m_pModulePgInfo is an array of. You say that ModuleSize is a structure,
but that’s nearly impossible to believe; the code should never have
compiled in that case. It seems far more likely that it is an integer
type of some sort.

Assuming that ModuleSize is an integer type, then casting the expression
to an intptr_t or uintptr_t before casting to a ModuleSize * may do the
trick (which to use depends on whether ModuleSize is signed or
unsigned). For example, if ModuleSize is signed then

cbModSize = (ModuleSize *)(intptr_t)( m_PreModuleSize ? …

may silence the compiler. Going a step further to

cbModSize = (ModuleSize *)(void *)(intptr_t)( m_PreModuleSize ? …

would be about as close as you’re going to get to “right”. Since the
results of this lot are undefined in C (but presumably happen to do the
right thing with the right 32-bit compiler) then it may not be possible
to get rid of all warnings (short of explicitly suppressing the
individual warnings).