Crash when cloning a cell

Hello Aspose,

I get a NullRefException when cloning a cell (Cell.Clone()).
To reproduce please try this:

 private static string CellCloneBug(Page page)
        {
            var t = new Table();
            var r = t.Rows.Add();
            var c = r.Cells.Add();
            c.Paragraphs.Add(null);
            c.Clone();
            page.Paragraphs.Add(t);                
            return Path.Combine(myDataDir, "CellCloneBug.pdf");
        }

This leads to a crash when saving a document or calling Document.ProcessParagraphs()

Regards
Gerd

@Gerd
I tested a bit, it seems that problem lies in c.Paragraphs.Add(null);
There should be some kind of text fragment that can be cloned
The following seems to work fine

private static void CellCloneBug()
{
    var pdfDocument = new Document();
    var page = pdfDocument.Pages.Add();
    var t = new Table();
    var r = t.Rows.Add();
    var c = r.Cells.Add();
    c.Paragraphs.Add(new TextFragment("Sample text"));
            
    r.Cells.Add((Cell)c.Clone());
    page.Paragraphs.Add(t);
    pdfDocument.Save(OutputFolder + "CellCloneBug.pdf");
}

Hello,
sure you are right. But it was only for demonstration.

But when I look into other *.Add(xyz) interfaces, they raise an exception when adding a null reference. I’m fine with that.

But if it should be possible to add a null-reference to a cell’s paragraphs then a following *.Clone() should not crash afterwards. This is best practice.

The crash will also happen when saving the PDF document and the root cause is very hard to find.

Regards
Gerd

@Gerd
We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): PDFNET-58838

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

That’s fair, thank you for pointing that out
I’ll add task for development team to fix this issue

Hello Aspose,

since we suffer severly from this issue it would be help full to give an advice which properties of the cell class should be checked/set as a workround to avoid the clone method to crash later.

regards
Gerd

@Gerd
I checked Aspose.Pdf.Cell, it seems that currently there’s only issue with null text fragments - other properties either not nullable or optional
Did you encounter other cases that cause cell.Clone() to crash?

“Did you encounter other cases that cause cell.Clone() to crash?”

Hard to say, the crash happened at customer by its report-PDF.
My tests did not cover the situation that a cell must be cloned.
Our code does not use Cell.Clone().
But I have seen crashes with the clone of TextFragments and Segments.
So any hint for problematic Clone()-issues is welcome to workaround the nasty crashes showing the ASPOSE call stack to the customer.

“Optional properties” means checked for NULL?

@Gerd
Yes, I meant all other properties that weren’t related to TextFragment - they usually already null by default and seem to be covered with null checks

So it seems that issue is mostly with TextFragments and Segments
Let me make some tests a bit, I’ll try to see if there’s something that will be helpful

Do I understand correctly that all these crashes happens during Clone()?

Yes, and are occuring when calling PDF.Save().

1 Like

@Gerd
I tested a bit but couldn’t reproduce the same result with Segment
Here’s some found notes that hopefully will narrow some scope of errors

static void CellCloneBug()
{
    var pdfDocument = new Document();
    var page = pdfDocument.Pages.Add();
    var t = new Table();
    var r = t.Rows.Add();
    var c = r.Cells.Add();
    var tf = new TextFragment();
    //var tf = new TextFragment("test value");
    //tf.Text = null;//this causes NullRefException
    //tf.BaselinePosition = null;//this causes NullRefException
    tf.TextEditOptions = null;
    tf.EndNote = null;
    tf.FootNote = null;
    tf.Hyperlink = null;
    tf.Margin = null;
    //tf.Position = null;//this causes NullReference
    //tf.Segments = null;//null by default but causes NullRefException if set to null

    //tf.Segments = new TextSegmentCollection(null);//this doesn't work
    //tf.Segments.Add(null);//directly NullRefException
            
    var s = new TextSegment();
    //s.Text = string.Empty;//if Text = string.Empty then seems to go in endless cycle
    s.Text = "a";
    //s.Text = null;//directly NullRefException
    //s.TextState = null;//if null then NullRefException on Segments.Add(s) regardless of text value
    s.TextEditOptions = null;
    s.Position = null;
    s.BaselinePosition = null;
    s.Hyperlink = null;
    tf.Segments.Add(s);

    //c.Paragraphs.Add(null);//this is reported
    c.Paragraphs.Add(tf);

    r.Cells.Add((Cell)c.Clone());
    page.Paragraphs.Add(t);
    pdfDocument.Save(OutputFolder + "CellCloneBug.pdf");
}

I’ll try to ask developers tomorrow for possible insights
For now, is it possible to share call stack that you got from customer?
maybe there will be something of use