View Issue Details

IDProjectCategoryView StatusLast Update
0032636CommunityOCCT:Foundation Classespublic2021-10-21 19:58
ReporterBenjaminBihler Assigned Tobugmaster  
PrioritynormalSeverityfeature 
Status closedResolutionnot fixable 
PlatformAOSL 
Product Version7.0.0 
Target Version7.6.0 
Summary0032636: Foundation Classes - Make handles type-safe
DescriptionI have been debugging a strange application crash for hours and it seems as if I had found the error. There is a quite simple way to mess up handle types and I wonder whether this could be found out at runtime. But I see no possibility to do so.

Would you check the code in "Steps to Reproduce". Can a feature be added to detect handle type errors like this?
Steps To Reproduce#include <TDF_Attribute.hxx>
#include <TDataStd_AsciiString.hxx>
#include <TNaming_NamedShape.hxx>

void getSomething(Handle(TDF_Attribute)& string)
{
    Handle(TNaming_NamedShape) namedShape = new TNaming_NamedShape;

    // This is wrong!!! Can we find out at runtime (or even compile time?),
    // that the caller is using a wrong handle type?!?!
    string = namedShape;
}

int main()
{
    Handle(TDataStd_AsciiString) string;

    getSomething(string);

    string->Set("Hello World!");

    std::cout << "The string is: " << string->Get().ToCString() << std::endl;

    return 0;
}
TagsNo tags attached.
Test case number

Activities

BenjaminBihler

2021-10-19 16:44

developer   ~0104737

My error has happened within TDF_Label::FindAttribute(...), this is what I have in mind. The caller has not passed a TDF_Attribute handle reference, but a handle reference of the wrong type.

kgv

2021-10-20 09:44

developer   ~0104757

Last edited: 2021-10-20 09:45

I don't think there is a reasonable solution to this problem.
I recall we have discussed such scenarios that Handle()& syntax permits many years ago with an only conclusions that user should avoid such code.

> My error has happened within TDF_Label::FindAttribute(...), this is what I have in mind.
TDF_Label::FindAttribute() is defined as two methods:
Standard_Boolean FindAttribute (const Standard_GUID& anID,
                                Handle(TDF_Attribute)& anAttribute) const;

//! Safe variant of FindAttribute() for arbitrary type of argument
template <class T> 
Standard_Boolean FindAttribute (const Standard_GUID& theID,
                                Handle(T)& theAttr) const
{ 
  Handle(TDF_Attribute) anAttr;
  return FindAttribute (theID, anAttr) && ! (theAttr = Handle(T)::DownCast(anAttr)).IsNull();
}

The second one is defined as template and should care about safe casting.
Doesn't it work in your case? Why?

BenjaminBihler

2021-10-20 12:30

developer   ~0104759

Thank you for the explanation, Kirill.

I was wrong, the problem did not appear within TDF_Label::FindAttribute(...), but within an own method that is similar to FindAttribute(...). Thanks to your hint I use also the template type parameter now to check consistence.

Therefore, I guess this issue can be closed, since there is already a safe FindAttribute(...) method.

Issue History

Date Modified Username Field Change
2021-10-19 16:42 BenjaminBihler New Issue
2021-10-19 16:42 BenjaminBihler Assigned To => abv
2021-10-19 16:44 BenjaminBihler Note Added: 0104737
2021-10-20 09:44 kgv Note Added: 0104757
2021-10-20 09:45 kgv Note Edited: 0104757
2021-10-20 12:30 BenjaminBihler Note Added: 0104759
2021-10-20 12:44 kgv Assigned To abv => bugmaster
2021-10-20 12:44 kgv Status new => feedback
2021-10-20 12:44 kgv Resolution open => not fixable
2021-10-20 12:45 kgv Product Version => 7.0.0
2021-10-21 19:58 bugmaster Status feedback => closed