指针赋值将指针从 64 位截断为 32 位
Pointer assignment truncates the pointer from 64bit to 32bit
我一直在尝试修改 likewise-open
中的一段代码,但在这里完全被难住了。
一些背景
正在处理此 file,尝试围绕一些 LDAP 查询进行编码:
typedef void *MYH;
typedef MYH HANDLE;
HANDLE hDirectory = NULL;
hDirectory = LsaDmpGetLdapHandle(pConn);
LsaDmpGetLdapHandle() 已定义 here
typedef void *MYH;
typedef MYH HANDLE;
HANDLE
LsaDmpGetLdapHandle(
IN PLSA_DM_LDAP_CONNECTION pConn
)
{
return pConn->hLdapConnection;
}
其中 PLSA_DM_LDAP_CONNECTION 是 typedef
用于以下 struct
:
struct _LSA_DM_LDAP_CONNECTION
{
...
// NULL if not connected
HANDLE hLdapConnection;
...
};
基本上到处都是HANDLE
类型。
注意:为了避免各种*.h
文件对它的定义不同,我在两个文件
中添加了typedef void *MYH;
麻烦:
代码会在 hDirectory
从 LsaDmpGetLdapHandle
返回的行分配后崩溃,我尝试进一步使用 hDirectory
调试到现在:
附上gdb,pConn
中的hLdapConnection
是:
(gdb) p pConn->hLdapConnection
= (void *) 0x7feb939d6390
然而,hDirectory
是:
(gdb) p hDirectory
= (void *) 0xffffffff939d6390
赋值后我不明白为什么会有区别??
此外,需要注意的是,两个指针地址中的 939d6390
是通用的。
有趣的是,这两种方法都有效
// If I pass hDirectory reference
LsaDmLdapGetHandle(pConn, &hDirectory);
// where this function is defined as, in the other file:
DWORD
LsaDmLdapGetHandle(
IN PLSA_DM_LDAP_CONNECTION pConn,
OUT HANDLE* phDirectory)
{
HANDLE hDirectory = NULL;
hDirectory = LsaDmpGetLdapHandle(pConn);
*phDirectory = hDirectory;
return ERROR_SUCCESS;
}
// Or I call another function, which then call LsaDmpGetLdapHandle(), in the other file
hDirectory = LsaDmLdapGetHandleCopy(pConn);
HANDLE
LsaDmLdapGetHandleCopy(
IN PLSA_DM_LDAP_CONNECTION pConn)
{
HANDLE hDirectory = NULL;
hDirectory = LsaDmpGetLdapHandle(pConn);
return hDirectory;
}
我想,这两个文件中的 HANDLE 定义可能不同,因此我在两个文件中添加了自己的 void *
定义
看起来像 this
的复制品
By default all return values are int. So if a prototype is missing for function then compiler treats the return value as 32-bit and generates code for 32-bit return value. Thats when your upper 4 bytes gets truncated.
我一直在尝试修改 likewise-open
中的一段代码,但在这里完全被难住了。
一些背景
正在处理此 file,尝试围绕一些 LDAP 查询进行编码:
typedef void *MYH;
typedef MYH HANDLE;
HANDLE hDirectory = NULL;
hDirectory = LsaDmpGetLdapHandle(pConn);
LsaDmpGetLdapHandle() 已定义 here
typedef void *MYH;
typedef MYH HANDLE;
HANDLE
LsaDmpGetLdapHandle(
IN PLSA_DM_LDAP_CONNECTION pConn
)
{
return pConn->hLdapConnection;
}
其中 PLSA_DM_LDAP_CONNECTION 是 typedef
用于以下 struct
:
struct _LSA_DM_LDAP_CONNECTION
{
...
// NULL if not connected
HANDLE hLdapConnection;
...
};
基本上到处都是HANDLE
类型。
注意:为了避免各种*.h
文件对它的定义不同,我在两个文件
typedef void *MYH;
麻烦:
代码会在 hDirectory
从 LsaDmpGetLdapHandle
返回的行分配后崩溃,我尝试进一步使用 hDirectory
调试到现在:
附上gdb,pConn
中的hLdapConnection
是:
(gdb) p pConn->hLdapConnection
= (void *) 0x7feb939d6390
然而,hDirectory
是:
(gdb) p hDirectory
= (void *) 0xffffffff939d6390
赋值后我不明白为什么会有区别??
此外,需要注意的是,两个指针地址中的 939d6390
是通用的。
有趣的是,这两种方法都有效
// If I pass hDirectory reference
LsaDmLdapGetHandle(pConn, &hDirectory);
// where this function is defined as, in the other file:
DWORD
LsaDmLdapGetHandle(
IN PLSA_DM_LDAP_CONNECTION pConn,
OUT HANDLE* phDirectory)
{
HANDLE hDirectory = NULL;
hDirectory = LsaDmpGetLdapHandle(pConn);
*phDirectory = hDirectory;
return ERROR_SUCCESS;
}
// Or I call another function, which then call LsaDmpGetLdapHandle(), in the other file
hDirectory = LsaDmLdapGetHandleCopy(pConn);
HANDLE
LsaDmLdapGetHandleCopy(
IN PLSA_DM_LDAP_CONNECTION pConn)
{
HANDLE hDirectory = NULL;
hDirectory = LsaDmpGetLdapHandle(pConn);
return hDirectory;
}
我想,这两个文件中的 HANDLE 定义可能不同,因此我在两个文件中添加了自己的 void *
定义
看起来像 this
的复制品By default all return values are int. So if a prototype is missing for function then compiler treats the return value as 32-bit and generates code for 32-bit return value. Thats when your upper 4 bytes gets truncated.