Fix potential nullref in embedBuilder value setter (#734)

* 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
This commit is contained in:
Pat Murphy
2017-07-05 16:56:43 -07:00
committed by RogueException
parent 8cd99beb62
commit d89804d7c7
2 changed files with 17 additions and 7 deletions

View File

@@ -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);
}
}

View File

@@ -44,7 +44,7 @@ namespace Discord
get => _embed.Url; get => _embed.Url;
set 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; _embed.Url = value;
} }
} }
@@ -53,7 +53,7 @@ namespace Discord
get => _embed.Thumbnail?.Url; get => _embed.Thumbnail?.Url;
set 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); _embed.Thumbnail = new EmbedThumbnail(value, null, null, null);
} }
} }
@@ -62,7 +62,7 @@ namespace Discord
get => _embed.Image?.Url; get => _embed.Image?.Url;
set 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); _embed.Image = new EmbedImage(value, null, null, null);
} }
} }
@@ -260,7 +260,7 @@ namespace Discord
get => _field.Value; get => _field.Value;
set 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 (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)); if (stringValue.Length > MaxFieldValueLength) throw new ArgumentException($"Field value length must be less than or equal to {MaxFieldValueLength}.", nameof(Value));
_field.Value = stringValue; _field.Value = stringValue;
@@ -313,7 +313,7 @@ namespace Discord
get => _author.Url; get => _author.Url;
set 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; _author.Url = value;
} }
} }
@@ -322,7 +322,7 @@ namespace Discord
get => _author.IconUrl; get => _author.IconUrl;
set 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; _author.IconUrl = value;
} }
} }
@@ -372,7 +372,7 @@ namespace Discord
get => _footer.IconUrl; get => _footer.IconUrl;
set 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; _footer.IconUrl = value;
} }
} }