Skip to content

Fix null reference when markdown content is empty - #7463

Merged
Ilya (iSazonov) merged 4 commits into
PowerShell:masterfrom
adityapatwardhan:FixMarkdownNullReference
Aug 27, 2018
Merged

Fix null reference when markdown content is empty#7463
Ilya (iSazonov) merged 4 commits into
PowerShell:masterfrom
adityapatwardhan:FixMarkdownNullReference

Conversation

@adityapatwardhan

@adityapatwardhan Aditya Patwardhan (adityapatwardhan) commented Aug 6, 2018

Copy link
Copy Markdown
Member

Fix #7453

PR Summary

Fix cases where markdown content can be empty and we get a null reference exception.

PR Checklist

@iSazonov Ilya (iSazonov) left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder that we have to check so many null references. Seems we need review all the code (not in the PR).

protected override void Write(VT100Renderer renderer, FencedCodeBlock obj)
{
foreach (var codeLine in obj.Lines.Lines)
if (obj?.Lines != null && obj.Lines.Lines != null)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we use "?" we could do

if (obj?.Lines?.Lines != null)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to obj?.Lines.Lines != null

obj.Lines is of type StringLineGroup which cannot be null.

if (obj?.Lines != null && obj.Lines.Lines != null)
{
if (!string.IsNullOrWhiteSpace(codeLine.ToString()))
foreach (var codeLine in obj?.Lines.Lines)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have checked obj != null in previous line and could remove "?"

foreach (var codeLine in obj.Lines.Lines) 

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

{
// Format header and then add blank line to improve readability.
switch (obj.Level)
string headerText = obj.Inline?.FirstChild?.ToString();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we check obj != null too?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

else
{
renderer.Write(renderer.EscapeSequences.FormatLink(obj.FirstChild.ToString(), obj.Url));
renderer.Write(renderer.EscapeSequences.FormatLink(obj.FirstChild?.ToString(), obj.Url));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We check obj.FirstChild != null in both code path in the "if-else" - seems we should move the check to new "if":

if (obj.FirstChild != null)
{
    if (obj.IsImage)
    {
        renderer.Write(renderer.EscapeSequences.FormatImage(obj.FirstChild.ToString()));
    }
    else
    {
        renderer.Write(renderer.EscapeSequences.FormatLink(obj.FirstChild.ToString(), obj.Url));
    }
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The question marks here would have different semantics of course, but I imagine FormatLink will thrown an NRE if its first argument is null.

So I would do similar to Ilya (@iSazonov) (just a style change):

if (obj.FirstChild == null)
{
    return;
}

if (obj.IsImage)
{
    renderer.Write(renderer.EscapeSequences.FormatImage(obj.FirstChild.ToString()));
}
else
{
    renderer.Write(renderer.EscapeSequences.FormatLink(obj.FirstChild.ToString(), obj.Url));
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We support having empty alt-text for images and empty link text. So updated accordingly.

@adityapatwardhan

Copy link
Copy Markdown
Member Author

Rob Holt (@rjmholt) Ilya (@iSazonov) Addressed the comments please have another look.

if (obj?.Lines.Lines != null)
{
if (!string.IsNullOrWhiteSpace(codeLine.ToString()))
foreach (var codeLine in obj.Lines.Lines)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

string instead of var would be more obvious here

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

string text = obj?.FirstChild?.ToString();

// Format link as image or link.
if (obj.IsImage)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If obj is null, this will cause an NRE. Would the logic be preserved like this:

string text = obj?.FirstChild?.ToString();
if (text == null)
{
    return;
}

// Golden path code...

?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

obj can never be null, hence removed ? after obj.

We allow text to be null hence the above logic holds.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (obj.IsImage) - can cause an NRE.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ilya (@iSazonov) obj is guaranteed to be not null. If it is null the renderer is not called.

@adityapatwardhan

Copy link
Copy Markdown
Member Author

Rob Holt (@rjmholt) Updated.

@rjmholt Rob Holt (rjmholt) left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

// This specifically helps for parameters help content.
if (string.Equals(obj.Info, "yaml", StringComparison.OrdinalIgnoreCase))
{
renderer.Write("\t").WriteLine(codeLine.ToString());

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We call codeLine.ToString()) twice above - should we use a variable?
And in line 34 too.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One is in if and other is in else, so only one of them is called. And this change is out of scope for this PR.

@iSazonov

Copy link
Copy Markdown
Collaborator

Dongbo Wang (@daxian-dbw) Steve Lee (@SteveL-MSFT) The PR is ready to merge if you haven't comments.

@iSazonov
Ilya (iSazonov) merged commit aec954e into PowerShell:master Aug 27, 2018
@adityapatwardhan
Aditya Patwardhan (adityapatwardhan) deleted the FixMarkdownNullReference branch August 28, 2018 17:09
@TravisEz13 Travis Plunk (TravisEz13) added this to the v6.1.0 milestone Aug 28, 2018
Thatgfsj (Thatgfsj) pushed a commit to Thatgfsj-contribs/PowerShell that referenced this pull request Aug 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NullReferenceException in ConvertFrom-Markdown -AsVT100EncodedString with empty alt text

4 participants