Null reference exceptoin in PostSharp.Patterns.Formatters.UnsafeStringBuilder.Append(String s)

eric.ziko's Avatar

eric.ziko

28 Dec, 2017 11:44 PM

I am using PostSharp 5.0.41 and trying to add logging to my codebase via NLOG pursuant to the instructions on your web site. At certain points in my application I am getting null reference exceptions from your PostSharp.Patterns.* code

Here is a stack trace of one of the places that it happening (continue reading after stack trace - more info to follow)

at PostSharp.Patterns.Formatters.UnsafeStringBuilder.Append(String s)
at PostSharp.Patterns.Formatters.DefaultFormatter2.Write(UnsafeStringBuilder stringBuilder, TValue value)
at PostSharp.Patterns.Diagnostics.RecordBuilders.TextLogRecordBuilder.AppendSemanticParameter[T](SemanticParameterKind kind, String name, T value, IFormatter1 formatter, String prefix, Int32 index)
at PostSharp.Patterns.Diagnostics.RecordBuilders.TextLogRecordBuilder.SetParameterT
at PostSharp.Patterns.Diagnostics.RecordBuilders.LogRecordBuilder.SetParameterT
at FACS4C.Winforms.DevExpressExtensions.TabbedViewExtensions.AllDocuments(TabbedView tabbedView) in f:\1FIS_FACS\Olap\ezps\FACS4C.Winforms.Controls\DevExpressExtensions\TabbedViewExtensions.cs:line 11
at FACS4C.Winforms.Controls.ucTabbedWorkSpaceController.lazyLoadDocuments(IWorkSpaceSaveState workSpaceSaveState) in f:\1FIS_FACS\Olap\ezps\FACS4C.Winforms.Controls\Controls\ucTabbedWorkSpaceController.lazyLoadDocuments.cs:line 37
at FACS4C.Winforms.Controls.ucTabbedWorkSpaceController.<>c_DisplayClass31_0.<LoadWorkSpace>b0() in f:\1FIS_FACS\Olap\ezps\FACS4C.Winforms.Controls\Controls\ucTabbedWorkSpaceController.LoadWorkSpace.cs:line 42
at FACS4C.Winforms.DevExpressExtensions.SplashScreenManagerExtensions.ShowWaitForm(SplashScreenManager s, Action a, String caption, Boolean showWaitForm, String description) in f:\1FIS_FACS\Olap\ezps\FACS4C.Winforms.Common\DevExpressExtensions\SplashScreenManagerExtensions.cs:line 13
at FACS4C.Winforms.Controls.ucTabbedWorkSpaceController.LoadWorkSpace(SaveLocation fromLocation) in f:\1FIS_FACS\Olap\ezps\FACS4C.Winforms.Controls\Controls\ucTabbedWorkSpaceController.LoadWorkSpace.cs:line 21
at FACS4C.Winforms.Controls.ucTabbedWorkSpace.<.ctor>b_29_13(Object s, ItemClickEventArgs e) in f:\1FIS_FACS\Olap\ezps\FACS4C.Winforms.Controls\Controls\ucTabbedWorkSpace.cs:line 129
at DevExpress.XtraBars.ItemClickEventHandler.Invoke(Object sender, ItemClickEventArgs e)
at DevExpress.XtraBars.BarItem.OnClick(BarItemLink link)
at DevExpress.XtraBars.BarBaseButtonItem.OnClick(BarItemLink link)
at DevExpress.XtraBars.BarButtonItem.OnClick(BarItemLink link)
at DevExpress.XtraBars.BarItemLink.OnLinkClick()
at DevExpress.XtraBars.BarButtonItemLink.OnLinkClick()
at DevExpress.XtraBars.BarItemLink.OnLinkAction(BarLinkAction action, Object actionArgs)
at DevExpress.XtraBars.BarButtonItemLink.OnLinkAction(BarLinkAction action, Object actionArgs)
at DevExpress.XtraBars.Controls.VerticalLinksNavigation.ProcessKeyDown(KeyEventArgs e, BarItemLink activeLink)
at DevExpress.XtraBars.Controls.CustomPopupBarControl.ProcessKeyDown(KeyEventArgs e)
at DevExpress.XtraBars.ViewInfo.BarSelectionInfo.ProcessKeyDown(KeyEventArgs e)
at DevExpress.XtraBars.MessageFilter.BarManagerHook.PreFilterMessage(Message& m)
at DevExpress.XtraBars.MessageFilter.BarManagerMessageFilter.System.Windows.Forms.IMessageFilter.PreFilterMessage(Message& m)

After decompiling the method that is throwing the exception (below) - I believe the exception is being caused by checking s.Length in the first line of the method, without actually checking if the (string s) paramater is null.

Can you please advise...

Also - when trying to debug this code in Visual Studio, I can see any of your code - it is hidden from my stack trace...

[SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId="s")]
public unsafe bool Append(string s)
{
    int length = s.Length;
    if (length != 0)
    {
        if ((this.cursor + length) > this.end)
        {
            return this.OnOverflow();
        }
        fixed (char* str = ((char*) s))
        {
            char* chPtr = str;
            if (chPtr != null)
            {
                chPtr += RuntimeHelpers.OffsetToStringData;
            }
            BufferHelper.CopyMemory((void*) this.cursor, (void*) chPtr, length * 2);
        }
        this.cursor += length;
    }
    return true;
}
  1. Support Staff 1 Posted by PostSharp Techn... on 29 Dec, 2017 02:38 PM

    PostSharp Technologies's Avatar

    Hello,

    thank you for reporting this issue. The trigger is that DevExpress.XtraBars.Docking2010.Views.Tabbed.TabbedView.ToString() method returns null, which is where the null value is coming from.

    Before we fix this bug, you can:
    a) Exclude this parameter from being logged by annotating it using [NotLogged]attribute, so your method signature would look like public static IEnumerable<BaseDocument> AllDocuments([NotLogged]this TabbedView view) (guessing the visibility, return type and parameter name).
    b) Implement and register a custom formatter for DevExpress.XtraBars.Docking2010.Views.Tabbed.TabbedView objects as described at http://doc.postsharp.net/custom-formatter.

    We're closing the ticket for now as the bug has been internally filed as issue #15762. We will contact you as soon as the bug fix has been released.

    For more details on our support policies and prioritization of bug fixes, please visit https://www.postsharp.net/support/policies.

    Best regards,
    -tony

  2. PostSharp Technologies closed this discussion on 29 Dec, 2017 02:38 PM.

  3. eric.ziko re-opened this discussion on 02 Jan, 2018 09:04 PM

  4. 2 Posted by eric.ziko on 02 Jan, 2018 09:04 PM

    eric.ziko's Avatar

    Tony,

    I appreciate your quick reply, and for this use case, we can apply the fix that you recommended.

    The bigger problem is that we don't know where else in our code this sort of thing is likely to crop up, so we can't take the risk of adding your logging aspect across our codebase without being afraid that it will break our code in other spots.

    Please let us know as soon as the bug is fixed & released

    Eric Ziko

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac