Description
Invalid CXCursor
cause System.AccessViolationException
in Locals/Watch windows terminating debugging.
If an invalid CXCursor
is in the locals debugging window and 'Enable Property Evaluation' is enabled the debugger crashes.
To reproduce have the locals window open and 'Enable Property Evaluation' enabled in the options and debug this:
static void Main( string[] args )
{
ClangSharp.Interop.CXCursor boom = default;
System.Diagnostics.Debugger.Break();
}
The not so good
I've traced the issue to clang.getCursorKindSpelling(Kind);
. if Kind is invalid, and that includes zero, an AccessViolationException
gets thrown. This is not good, but it's probably a problem with libclang, not ClangSharp. A solution is to just check that it's valid before using that function.
The bad
What is much worse is that the KindSpelling
property on CXCursor
calls clang.getCursorKindSpelling(Kind)
without checking if Kind
is valid.
The ugly
What is critical, is that the DebuggerDisplayString
property tries to retrieve KindSpelling
and that property gets read by the DebuggerDisplayAttribute
. Since CXCursor
is a struct the debugger will happily try to show the value even before it's been initialized, so a breakpoint anywhere in the scope before the variable will cause the program and debugger to crash. This also applies when a CXCursor is a field or property in a class or struct.