Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix null reference when markdown content is empty #7463

Merged
merged 4 commits into from
Aug 27, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Address code review comments
  • Loading branch information
adityapatwardhan committed Aug 13, 2018
commit 09fedf01428436253f107ea4d4fd9392ee8ff9cd
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ internal class FencedCodeBlockRenderer : VT100ObjectRenderer<FencedCodeBlock>
{
protected override void Write(VT100Renderer renderer, FencedCodeBlock obj)
{
if (obj?.Lines != null && obj.Lines.Lines != null)
if (obj?.Lines.Lines != null)
{
foreach (var codeLine in obj?.Lines.Lines)
foreach (var codeLine in obj.Lines.Lines)
Copy link
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
Member Author

Choose a reason for hiding this comment

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

Fixed

{
if (!string.IsNullOrWhiteSpace(codeLine.ToString()))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal class HeaderBlockRenderer : VT100ObjectRenderer<HeadingBlock>
{
protected override void Write(VT100Renderer renderer, HeadingBlock obj)
{
string headerText = obj.Inline?.FirstChild?.ToString();
string headerText = obj?.Inline?.FirstChild?.ToString();

if (!string.IsNullOrEmpty(headerText))
{
Expand Down
6 changes: 4 additions & 2 deletions src/Microsoft.PowerShell.MarkdownRender/LinkInlineRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@ internal class LinkInlineRenderer : VT100ObjectRenderer<LinkInline>
{
protected override void Write(VT100Renderer renderer, LinkInline obj)
{
string text = obj?.FirstChild?.ToString();

// Format link as image or link.
if (obj.IsImage)
Copy link
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
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
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
Member Author

Choose a reason for hiding this comment

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

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

{
renderer.Write(renderer.EscapeSequences.FormatImage(obj.FirstChild?.ToString()));
renderer.Write(renderer.EscapeSequences.FormatImage(text));
}
else
{
renderer.Write(renderer.EscapeSequences.FormatLink(obj.FirstChild?.ToString(), obj.Url));
renderer.Write(renderer.EscapeSequences.FormatLink(text, obj.Url));
}
}
}
Expand Down