Archived issue #0026377
Passing Handle objects as arguments to functions as non-const reference to base type is dangerous
Description
As pointed out by Commenter 3, passing Handle object as argument to function accepting non-const reference to handle to base type may be dangerous, as the handle can be modified by that function, and there is no guarantee that the new object pointed by it will be compatible with the original handle type.
Example:
Handle(TDataStd_Real) A;
if (Label.FindAttribute (TDataStd_Integer::GetID(), A)) {
// A is used as TData_Std_Real, while the object pointed by it is TDataStd_Integer
}
It would be good to have protection in this situation, to ensure that either exception is raised or at least handle is nullified if it is assigned non-compatible type. This should be possible when new handles are implemented in the frames of #0024023
Example:
Handle(TDataStd_Real) A;
if (Label.FindAttribute (TDataStd_Integer::GetID(), A)) {
// A is used as TData_Std_Real, while the object pointed by it is TDataStd_Integer
}
It would be good to have protection in this situation, to ensure that either exception is raised or at least handle is nullified if it is assigned non-compatible type. This should be possible when new handles are implemented in the frames of #0024023
Steps to reproduce
Not required
Public activity
3 archived notes
Participants are labeled by their role within this record.
Branch [archived branch] has been created by Author.
[revision removed]
Detailed log of new commits:
Author: Author
Date: Sun Jun 28 13:52:12 2015 +0300
0026377: Passing Handle objects as arguments to functions as non-const reference to base type is dangerous
Function SafeUpCast() added in opencascade::handle class to allow safe passing of handle as argument to functions accepting non-const reference to handle of base type.
Operator of cast to non-const reference is removed.
[revision removed]
Detailed log of new commits:
Author: Author
Date: Sun Jun 28 13:52:12 2015 +0300
0026377: Passing Handle objects as arguments to functions as non-const reference to base type is dangerous
Function SafeUpCast() added in opencascade::handle class to allow safe passing of handle as argument to functions accepting non-const reference to handle of base type.
Operator of cast to non-const reference is removed.
Tested with CR26711_6
Branch [archived branch] has been deleted by Participant.
[revision removed]
[revision removed]
Related records
- #0024023 · related to · closedRevamp the OCCT Handle
- #0027104 · related to · closedDownCast() cannot return null for mismatched handle
- #0027111 · related to · closedAdd generalized copy constructor in handle class for old compilers
- #0027185 · related to · closedData Exchange - IGES - incorrect reading of DE for undefined entity