Skip to content

CXCursor.KindSpelling causes System.AccessViolationException to be thrown when CXCursor.Kind is invalid. This includes 0 (zero) #182

Closed
@phizch

Description

@phizch

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.

https://github.com/microsoft/ClangSharp/blob/6de4907256dc2dc19f2fb1f4b1d50eaa9031e719/sources/ClangSharp/Interop.Extensions/CXCursor.cs#L9

https://github.com/microsoft/ClangSharp/blob/6de4907256dc2dc19f2fb1f4b1d50eaa9031e719/sources/ClangSharp/Interop.Extensions/CXCursor.cs#L866

https://github.com/microsoft/ClangSharp/blob/6de4907256dc2dc19f2fb1f4b1d50eaa9031e719/sources/ClangSharp/Interop.Extensions/CXCursor.cs#L1293-L1329

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions