DiscussionsIssue archiveOCCT:Foundation Classes

Archived issue #0026377

Passing Handle objects as arguments to functions as non-const reference to base type is dangerous

Open CASCADEOCCT:Foundation Classesclosed3 public notes

Search issues

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

Steps to reproduce

Not required

Public activity

3 archived notes

Participants are labeled by their role within this record.

01Commenter 2
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.
02Commenter 1
Tested with CR26711_6
03Commenter 2
Branch [archived branch] has been deleted by Participant.

[revision removed]

Related records