Skip to content

Commit

Permalink
Fix: Add RegexDefaults class and update all Regex timeouts
Browse files Browse the repository at this point in the history
Introduced a new static class `RegexDefaults` in `Constants.cs` to define default values for regular expressions, including a default timeout of 200 milliseconds.

Updated various `Regex` instances across multiple files (`DateTimeSerializer.cs`, `RecurrencePatternSerializer.cs`, `RequestStatusSerializer.cs`, `StringSerializer.cs`, `TimeSpanSerializer.cs`, `UtcOffsetSerializer.cs`, `WeekDaySerializer.cs`, `SimpleDeserializer.cs`, `TextUtil.cs`) to use the new `RegexDefaults.Timeout` for specifying the timeout parameter.
  • Loading branch information
axunonb committed Oct 13, 2024
1 parent 5067c3c commit ae4bd4f
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 21 deletions.
15 changes: 15 additions & 0 deletions Ical.Net/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -353,4 +353,19 @@ public static class CalendarMethods
/// </summary>
public const string DeclineCounter = "DECLINECOUNTER";
}

/// <summary>
/// The defaults used for regular expressions.
/// </summary>
public static class RegexDefaults
{
/// <summary>
/// The default timeout for regular expressions in milliseconds.
/// </summary>
public const int TimeoutMilliseconds = 200;
/// <summary>
/// The default timeout for regular expressions.
/// </summary>
public static readonly TimeSpan Timeout = TimeSpan.FromMilliseconds(TimeoutMilliseconds);
}
}
4 changes: 2 additions & 2 deletions Ical.Net/Serialization/DataTypes/DateTimeSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ public override string SerializeToString(object obj)
}

private const RegexOptions _ciCompiled = RegexOptions.Compiled | RegexOptions.IgnoreCase;
internal static readonly Regex DateOnlyMatch = new Regex(@"^((\d{4})(\d{2})(\d{2}))?$", _ciCompiled);
internal static readonly Regex FullDateTimePatternMatch = new Regex(@"^((\d{4})(\d{2})(\d{2}))T((\d{2})(\d{2})(\d{2})(Z)?)$", _ciCompiled);
internal static readonly Regex DateOnlyMatch = new Regex(@"^((\d{4})(\d{2})(\d{2}))?$", _ciCompiled, RegexDefaults.Timeout);
internal static readonly Regex FullDateTimePatternMatch = new Regex(@"^((\d{4})(\d{2})(\d{2}))T((\d{2})(\d{2})(\d{2})(Z)?)$", _ciCompiled, RegexDefaults.Timeout);

public override object Deserialize(TextReader tr)
{
Expand Down
16 changes: 8 additions & 8 deletions Ical.Net/Serialization/DataTypes/RecurrencePatternSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -190,25 +190,25 @@ public override string SerializeToString(object obj)
private const RegexOptions _ciCompiled = RegexOptions.IgnoreCase | RegexOptions.Compiled;

internal static readonly Regex OtherInterval =
new Regex(@"every\s+(?<Interval>other|\d+)?\w{0,2}\s*(?<Freq>second|minute|hour|day|week|month|year)s?,?\s*(?<More>.+)", _ciCompiled);
new Regex(@"every\s+(?<Interval>other|\d+)?\w{0,2}\s*(?<Freq>second|minute|hour|day|week|month|year)s?,?\s*(?<More>.+)", _ciCompiled, RegexDefaults.Timeout);

internal static readonly Regex AdverbFrequencies = new Regex(@"FREQ=(SECONDLY|MINUTELY|HOURLY|DAILY|WEEKLY|MONTHLY|YEARLY);?(.*)", _ciCompiled);
internal static readonly Regex AdverbFrequencies = new Regex(@"FREQ=(SECONDLY|MINUTELY|HOURLY|DAILY|WEEKLY|MONTHLY|YEARLY);?(.*)", _ciCompiled, RegexDefaults.Timeout);

internal static readonly Regex NumericTemporalUnits = new Regex(@"(?<Num>\d+)\w\w\s+(?<Type>second|minute|hour|day|week|month)", _ciCompiled);
internal static readonly Regex NumericTemporalUnits = new Regex(@"(?<Num>\d+)\w\w\s+(?<Type>second|minute|hour|day|week|month)", _ciCompiled, RegexDefaults.Timeout);

internal static readonly Regex TemporalUnitType = new Regex(@"(?<Type>second|minute|hour|day|week|month)\s+(?<Num>\d+)", _ciCompiled);
internal static readonly Regex TemporalUnitType = new Regex(@"(?<Type>second|minute|hour|day|week|month)\s+(?<Num>\d+)", _ciCompiled, RegexDefaults.Timeout);

internal static readonly Regex RelativeDaysOfWeek =
new Regex(
@"(?<Num>\d+\w{0,2})?(\w|\s)+?(?<First>first)?(?<Last>last)?\s*((?<Day>sunday|monday|tuesday|wednesday|thursday|friday|saturday)\s*(and|or)?\s*)+",
_ciCompiled);
_ciCompiled, RegexDefaults.Timeout);

internal static readonly Regex Time = new Regex(@"at\s+(?<Hour>\d{1,2})(:(?<Minute>\d{2})((:|\.)(?<Second>\d{2}))?)?\s*(?<Meridian>(a|p)m?)?",
_ciCompiled);
_ciCompiled, RegexDefaults.Timeout);

internal static readonly Regex RecurUntil = new Regex(@"^\s*until\s+(?<DateTime>.+)$", _ciCompiled);
internal static readonly Regex RecurUntil = new Regex(@"^\s*until\s+(?<DateTime>.+)$", _ciCompiled, RegexDefaults.Timeout);

internal static readonly Regex SpecificRecurrenceCount = new Regex(@"^\s*for\s+(?<Count>\d+)\s+occurrences\s*$", _ciCompiled);
internal static readonly Regex SpecificRecurrenceCount = new Regex(@"^\s*for\s+(?<Count>\d+)\s+occurrences\s*$", _ciCompiled, RegexDefaults.Timeout);

public override object Deserialize(TextReader tr)
{
Expand Down
4 changes: 2 additions & 2 deletions Ical.Net/Serialization/DataTypes/RequestStatusSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ public override string SerializeToString(object obj)
}
}

internal static readonly Regex NarrowRequestMatch = new Regex(@"(.*?[^\\]);(.*?[^\\]);(.+)", RegexOptions.Compiled);
internal static readonly Regex BroadRequestMatch = new Regex(@"(.*?[^\\]);(.+)", RegexOptions.Compiled);
internal static readonly Regex NarrowRequestMatch = new Regex(@"(.*?[^\\]);(.*?[^\\]);(.+)", RegexOptions.Compiled, RegexDefaults.Timeout);
internal static readonly Regex BroadRequestMatch = new Regex(@"(.*?[^\\]);(.+)", RegexOptions.Compiled, RegexDefaults.Timeout);

public override object Deserialize(TextReader tr)
{
Expand Down
2 changes: 1 addition & 1 deletion Ical.Net/Serialization/DataTypes/StatusCodeSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public override string SerializeToString(object obj)
return Encode(sc, Escape(string.Join(".", vals)));
}

internal static readonly Regex StatusCode = new Regex(@"\d(\.\d+)*", RegexOptions.Compiled | RegexOptions.CultureInvariant);
internal static readonly Regex StatusCode = new Regex(@"\d(\.\d+)*", RegexOptions.Compiled | RegexOptions.CultureInvariant, RegexDefaults.Timeout);

public override object Deserialize(TextReader tr)
{
Expand Down
4 changes: 2 additions & 2 deletions Ical.Net/Serialization/DataTypes/StringSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public StringSerializer() { }

public StringSerializer(SerializationContext ctx) : base(ctx) { }

internal static readonly Regex SingleBackslashMatch = new Regex(@"(?<!\\)\\(?!\\)", RegexOptions.Compiled);
internal static readonly Regex SingleBackslashMatch = new Regex(@"(?<!\\)\\(?!\\)", RegexOptions.Compiled, RegexDefaults.Timeout);

protected virtual string Unescape(string value)
{
Expand Down Expand Up @@ -98,7 +98,7 @@ public override string SerializeToString(object obj)
return string.Join(",", values);
}

internal static readonly Regex UnescapedCommas = new Regex(@"(?<!\\),", RegexOptions.Compiled);
internal static readonly Regex UnescapedCommas = new Regex(@"(?<!\\),", RegexOptions.Compiled, RegexDefaults.Timeout);
public override object Deserialize(TextReader tr)
{
if (tr == null)
Expand Down
2 changes: 1 addition & 1 deletion Ical.Net/Serialization/DataTypes/TimeSpanSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public override string SerializeToString(object obj)

internal static readonly Regex TimespanMatch =
new Regex(@"^(?<sign>\+|-)?P(((?<week>\d+)W)|(?<main>((?<day>\d+)D)?(?<time>T((?<hour>\d+)H)?((?<minute>\d+)M)?((?<second>\d+)S)?)?))$",
RegexOptions.Compiled | RegexOptions.IgnoreCase);
RegexOptions.Compiled | RegexOptions.IgnoreCase, RegexDefaults.Timeout);

public override object Deserialize(TextReader tr)
{
Expand Down
2 changes: 1 addition & 1 deletion Ical.Net/Serialization/DataTypes/UtcOffsetSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public override string SerializeToString(object obj)
return null;
}

internal static readonly Regex DecodeOffset = new Regex(@"(\+|-)(\d{2})(\d{2})(\d{2})?", RegexOptions.Compiled | RegexOptions.IgnoreCase);
internal static readonly Regex DecodeOffset = new Regex(@"(\+|-)(\d{2})(\d{2})(\d{2})?", RegexOptions.Compiled | RegexOptions.IgnoreCase, RegexDefaults.Timeout);

public override object Deserialize(TextReader tr)
{
Expand Down
2 changes: 1 addition & 1 deletion Ical.Net/Serialization/DataTypes/WeekDaySerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public override string SerializeToString(object obj)
return Encode(ds, value);
}

private static readonly Regex _dayOfWeek = new Regex(@"(\+|-)?(\d{1,2})?(\w{2})", RegexOptions.Compiled | RegexOptions.IgnoreCase);
private static readonly Regex _dayOfWeek = new Regex(@"(\+|-)?(\d{1,2})?(\w{2})", RegexOptions.Compiled | RegexOptions.IgnoreCase, RegexDefaults.Timeout);

public override object Deserialize(TextReader tr)
{
Expand Down
2 changes: 1 addition & 1 deletion Ical.Net/Serialization/SimpleDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ internal SimpleDeserializer(
private const string _paramNameGroup = "paramName";
private const string _paramValueGroup = "paramValue";

private static readonly Regex _contentLineRegex = new Regex(BuildContentLineRegex(), RegexOptions.Compiled);
private static readonly Regex _contentLineRegex = new Regex(BuildContentLineRegex(), RegexOptions.Compiled, RegexDefaults.Timeout);

private readonly DataTypeMapper _dataTypeMapper;
private readonly ISerializerFactory _serializerFactory;
Expand Down
4 changes: 2 additions & 2 deletions Ical.Net/Utility/TextUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public static string RemoveEmptyLines(string s)
return s;
}

internal static readonly Regex NormalizeToCrLf = new Regex(@"((\r(?=[^\n]))|((?<=[^\r])\n))", RegexOptions.Compiled);
internal static readonly Regex NormalizeToCrLf = new Regex(@"((\r(?=[^\n]))|((?<=[^\r])\n))", RegexOptions.Compiled, RegexDefaults.Timeout);

/// <summary>
/// Normalizes line endings, converting "\r" into "\r\n" and "\n" into "\r\n".
Expand All @@ -62,7 +62,7 @@ public static TextReader Normalize(string s, SerializationContext ctx)
return new StringReader(s);
}

internal static readonly Regex NewLineMatch = new Regex(@"(\r\n[ \t])", RegexOptions.Compiled);
internal static readonly Regex NewLineMatch = new Regex(@"(\r\n[ \t])", RegexOptions.Compiled, RegexDefaults.Timeout);

/// <summary> Unwraps lines from the RFC 5545 "line folding" technique. </summary>
public static string UnwrapLines(string s) => NewLineMatch.Replace(s, string.Empty);
Expand Down

0 comments on commit ae4bd4f

Please sign in to comment.