Skip to content

Commit

Permalink
Fix potential nullref in embedBuilder value setter (#734)
Browse files Browse the repository at this point in the history
* Fix potential nullref in embedBuilder value setter

* Null check on footer iconUrl

* Adding checks for the other URL properties

* Adding IsNullOrUri extension

* Setting StringExtensions as internal
  • Loading branch information
moiph authored and Auralytical committed Jul 5, 2017
1 parent 8cd99be commit d89804d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 7 deletions.
10 changes: 10 additions & 0 deletions src/Discord.Net.Core/Extensions/StringExtensions.cs
@@ -0,0 +1,10 @@
using System;

namespace Discord
{
internal static class StringExtensions
{
public static bool IsNullOrUri(this string url) =>
string.IsNullOrEmpty(url) || Uri.IsWellFormedUriString(url, UriKind.Absolute);
}
}
14 changes: 7 additions & 7 deletions src/Discord.Net.Rest/Entities/Messages/EmbedBuilder.cs
Expand Up @@ -44,7 +44,7 @@ public string Url
get => _embed.Url;
set
{
if (!Uri.IsWellFormedUriString(value, UriKind.Absolute)) throw new ArgumentException("Url must be a well-formed URI", nameof(Url));
if (!value.IsNullOrUri()) throw new ArgumentException("Url must be a well-formed URI", nameof(Url));
_embed.Url = value;
}
}
Expand All @@ -53,7 +53,7 @@ public string ThumbnailUrl
get => _embed.Thumbnail?.Url;
set
{
if (!Uri.IsWellFormedUriString(value, UriKind.Absolute)) throw new ArgumentException("Url must be a well-formed URI", nameof(ThumbnailUrl));
if (!value.IsNullOrUri()) throw new ArgumentException("Url must be a well-formed URI", nameof(ThumbnailUrl));
_embed.Thumbnail = new EmbedThumbnail(value, null, null, null);
}
}
Expand All @@ -62,7 +62,7 @@ public string ImageUrl
get => _embed.Image?.Url;
set
{
if (!Uri.IsWellFormedUriString(value, UriKind.Absolute)) throw new ArgumentException("Url must be a well-formed URI", nameof(ImageUrl));
if (!value.IsNullOrUri()) throw new ArgumentException("Url must be a well-formed URI", nameof(ImageUrl));
_embed.Image = new EmbedImage(value, null, null, null);
}
}
Expand Down Expand Up @@ -260,7 +260,7 @@ public object Value
get => _field.Value;
set
{
var stringValue = value.ToString();
var stringValue = value?.ToString();
if (string.IsNullOrEmpty(stringValue)) throw new ArgumentException($"Field value must not be null or empty.", nameof(Value));
if (stringValue.Length > MaxFieldValueLength) throw new ArgumentException($"Field value length must be less than or equal to {MaxFieldValueLength}.", nameof(Value));
_field.Value = stringValue;
Expand Down Expand Up @@ -313,7 +313,7 @@ public string Url
get => _author.Url;
set
{
if (!Uri.IsWellFormedUriString(value, UriKind.Absolute)) throw new ArgumentException("Url must be a well-formed URI", nameof(Url));
if (!value.IsNullOrUri()) throw new ArgumentException("Url must be a well-formed URI", nameof(Url));
_author.Url = value;
}
}
Expand All @@ -322,7 +322,7 @@ public string IconUrl
get => _author.IconUrl;
set
{
if (!Uri.IsWellFormedUriString(value, UriKind.Absolute)) throw new ArgumentException("Url must be a well-formed URI", nameof(IconUrl));
if (!value.IsNullOrUri()) throw new ArgumentException("Url must be a well-formed URI", nameof(IconUrl));
_author.IconUrl = value;
}
}
Expand Down Expand Up @@ -372,7 +372,7 @@ public string IconUrl
get => _footer.IconUrl;
set
{
if (!Uri.IsWellFormedUriString(value, UriKind.Absolute)) throw new ArgumentException("Url must be a well-formed URI", nameof(IconUrl));
if (!value.IsNullOrUri()) throw new ArgumentException("Url must be a well-formed URI", nameof(IconUrl));
_footer.IconUrl = value;
}
}
Expand Down

0 comments on commit d89804d

Please sign in to comment.