从安全的方式长期投射物体? [英] Cast object from long in safe manner?

查看:59
本文介绍了从安全的方式长期投射物体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我正在编写客户端/服务器应用程序。有1个服务器,以及许多

客户端。服务器处理来自每个客户端的请求,通常

代表他们创建和操作C ++对象。


现在,当客户端请求对象时创建后,我将一个

指针传递给对象(从服务器内存地址范围)作为long

整数。对于客户来说,这只是他们希望获得
访问权限的对象的ID。从那时起,当客户希望在

对象上执行操作时,它们会传回适当的长整数对象。以及他们希望执行的操作

。然后,服务器转换长整数。进入它期望的

对象,然后执行操作。


这个工作得很好,只是显然非常危险。如果一个

客户端要传回一个不同的长整数,并且这被转换为

一个对象,并且访问了该对象,应用程序将崩溃或者

可能表现出内存损坏的陌生感。


如何获得我的长整数?以安全的方式回到一个物体?我用
尝试了dynamic_cast但它不会工作。


我的意思的简单例子:


------------------


A级{

// ......

虚拟foo(){}

}


A * a =新A();


long objectID =(long)a;


A * a2 = dynamic_cast< A *>((void *)objectID);


------------------


a2始终为NULL。


Jamie烧伤。


[见 http: //www.gotw.ca/resources/clcm.htm 了解有关的信息]

[comp.lang.c ++。moderated。第一次海报:做到这一点! ]

Hello,

I am writing a client / server application. There is 1 server, and many
clients. The server processes requests from each client, and typically
creates and manipulates C++ objects on their behalf.

Now, when a client requests for an object to be created, I pass back a
pointer to the object (from server memory address scope) as a "long
integer". To the client, this is just an ID for the object they wish to
access. From then on, when clients wish an operation to be performed on an
object, they pass back the appropriate "long integer" and the manipulation
they wish to perform. The server then casts the "long integer" into the
object it expects, and performs the manipulation.

This is working quite well, only it is obviously quite dangerous. If a
client was to pass back a different "long integer", and this was casted into
an object, and this object was accessed, the application would crash hard or
potentially exhibit strangeness from memory corruption.

How can I get my "long integer" back into an object in a safe manner? I
tried dynamic_cast but it wouldn''t work.

Quick example of what I mean:

------------------

class A {
// ...
virtual foo() {}
}

A* a = new A();

long objectID = (long) a;

A* a2 = dynamic_cast<A*>((void *) objectID);

------------------

a2 is always NULL.

Jamie Burns.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

推荐答案

如何获得我的长整数?以安全的方式回到一个物体?我尝试了dynamic_cast但它不会工作。
How can I get my "long integer" back into an object in a safe manner? I
tried dynamic_cast but it wouldn''t work.



一个想法是不使用长整数但指向所有对象的基础

类的指针。而不是dynamic_cast会很好地工作

(虽然它很慢)。


One idea would be not to use "long integer" but a pointer to a base
class of all your objects. than a dynamic_cast would work pretty well
(although it''s quite slow).


" Jamie Burns" schrieb im Newsbeitrag新闻:bu ****************** @ news.demon.co.uk ...

:您好,



:我正在编写客户端/服务器应用程序。有1台服务器,还有很多

:客户端。服务器处理来自每个客户端的请求,通常

:代表他们创建和操作C ++对象。



:现在,当客户端请求创建一个对象,我传回一个

:指向该对象的指针(从服务器内存地址范围)作为long

:integer。对于客户来说,这只是他们想要的对象的ID。
:access。从那时起,当客户希望在

:对象上执行操作时,它们会传回适当的长整数对象。和操纵

:他们希望表演。然后,服务器转换长整数。进入

:它预期的对象,然后执行操作。



:这个工作得很好,只是显然非常危险。如果

:客户端要传回一个不同的长整数,并且这被转换为

:一个对象,并且访问了该对象,应用程序将严重崩溃或

:可能表现出内存损坏的陌生感。



:我怎样才能得到我的长整数以安全的方式回到一个物体?我

:尝试过dynamic_cast但是它不会工作。


将指针强制转换为指针永远不会真正安全。一个

指针可能太大而不适合长或某人可能会因为你怀疑而修改了长值。


我希望有一个服务器应用程序将其对象与

一起保存在一些集合(可能是地图)中,并且只将id传递给

客户端。当它从客户端收到一个id时,服务器可以轻松地b
搜索地图并找到相应的对象(如果id有效)。


问候

亨氏


[见 http://www.gotw.ca/resources/clcm.htm 有关的信息]

[comp.lang.c ++。moderated。第一次海报:做到这一点! ]
"Jamie Burns" schrieb im Newsbeitrag news:bu******************@news.demon.co.uk...
: Hello,
:
: I am writing a client / server application. There is 1 server, and many
: clients. The server processes requests from each client, and typically
: creates and manipulates C++ objects on their behalf.
:
: Now, when a client requests for an object to be created, I pass back a
: pointer to the object (from server memory address scope) as a "long
: integer". To the client, this is just an ID for the object they wish to
: access. From then on, when clients wish an operation to be performed on an
: object, they pass back the appropriate "long integer" and the manipulation
: they wish to perform. The server then casts the "long integer" into the
: object it expects, and performs the manipulation.
:
: This is working quite well, only it is obviously quite dangerous. If a
: client was to pass back a different "long integer", and this was casted into
: an object, and this object was accessed, the application would crash hard or
: potentially exhibit strangeness from memory corruption.
:
: How can I get my "long integer" back into an object in a safe manner? I
: tried dynamic_cast but it wouldn''t work.

It''s never really safe to cast a pointer to long and back to pointer. A
pointer may be too large to fit into a long or someone might have
modified the long value as you suspect.

I would prefer to have a server app that keeps its objects together with
some id in some collection (probably a map) and only passes the id to the
client. When it recieves an id from the client, the server can easyly
search the map and find the corresponding object (if the id is valid).

Regards
Heinz

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


Jamie Burns写道:
Jamie Burns wrote:
你好,

我正在写一个客户端/服务器应用程序。有1个服务器,和许多客户端。服务器处理来自每个客户端的请求,并且通常代表他们创建和操作C ++对象。

现在,当客户端请求创建对象时,我会传回一个
指向对象的指针(从服务器内存地址范围)作为long
整数。对于客户端,这只是他们希望访问的对象的ID。从那时起,当客户希望对某个对象执行操作时,它们会传回适当的长整数和他们希望执行的操作。然后服务器转换
长整数。进入它期望的对象,并执行
操作。

这很有效,只是显然非常危险。如果
客户端要传回一个不同的长整数,并且这被转换为一个对象,并且访问了该对象,则该应用程序将崩溃或可能表现出陌生感来自记忆
腐败。

如何获得我的长整数?以安全的方式回到对象中?
我尝试了dynamic_cast但它不起作用。
Hello,

I am writing a client / server application. There is 1 server, and
many clients. The server processes requests from each client, and
typically creates and manipulates C++ objects on their behalf.

Now, when a client requests for an object to be created, I pass back a
pointer to the object (from server memory address scope) as a "long
integer". To the client, this is just an ID for the object they wish
to access. From then on, when clients wish an operation to be
performed on an object, they pass back the appropriate "long integer"
and the manipulation they wish to perform. The server then casts the
"long integer" into the object it expects, and performs the
manipulation.

This is working quite well, only it is obviously quite dangerous. If a
client was to pass back a different "long integer", and this was
casted into an object, and this object was accessed, the application
would crash hard or potentially exhibit strangeness from memory
corruption.

How can I get my "long integer" back into an object in a safe manner?
I tried dynamic_cast but it wouldn''t work.




dynamic_cast仅适用于在类层次结构中进行转换一个

多态类。它需要一些关于存储在对象中的类型的信息。但是没有void对象,所以你不能从一个空的* b $ b $ dynamic $cast *。

我会离开使用指针本身作为ID,而不是

维护一个指向对象的指针的列表(可能是std :: vector),然后

然后提供一个索引作为对象的ID。然后你的支票

减少到一个简单的边界检查。

[见 http://www.gotw.ca/resources/clcm.htm 有关的信息]

[comp.lang.c ++。moderated。第一次海报:做到这一点! ]



dynamic_cast only works for casting within a class hierarchy of a
polymorphic class. It needs some information about the type that is
stored in the object. But there is no void object, so you can''t
dynamic_cast from a void*.
I would step away from using the pointer itself as ID, and rather
maintain a list (maybe std::vector) of pointers to your objects, and
then provide an index into that as ID for the objects. Then your check
reduces to a simple bounds check.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]


这篇关于从安全的方式长期投射物体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆