Skip to content

Commit

Permalink
Fix potential IndexOutOfRangeException when parsing M3U8
Browse files Browse the repository at this point in the history
  • Loading branch information
ScrubN committed Dec 31, 2023
1 parent 8d7f23c commit ee7c269
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions TwitchDownloaderCore/Tools/M3U8.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,17 @@ public static M3U8 Parse(System.IO.Stream stream, Encoding streamEncoding, strin

while (sr.ReadLine() is { } line)
{
if (string.IsNullOrWhiteSpace(line))
{
ClearStreamMetadata(out currentExtMediaInfo, out currentExtStreamInfo, out currentExtProgramDateTime, out currentByteRange, out currentExtPartInfo);
continue;
}

if (line[0] != '#')
{
var path = Path.Combine(basePath, line);
streams.Add(new Stream(currentExtMediaInfo, currentExtStreamInfo, currentExtPartInfo, currentExtProgramDateTime, currentByteRange, path));
currentExtMediaInfo = null;
currentExtStreamInfo = null;
currentExtProgramDateTime = default;
currentByteRange = default;
currentExtPartInfo = null;
ClearStreamMetadata(out currentExtMediaInfo, out currentExtStreamInfo, out currentExtProgramDateTime, out currentByteRange, out currentExtPartInfo);

continue;
}
Expand Down Expand Up @@ -90,15 +92,17 @@ public static M3U8 Parse(ReadOnlySpan<char> text, string basePath = "")
if (lineEnd != -1)
workingSlice = workingSlice[..lineEnd];

if (workingSlice.IsWhiteSpace())
{
ClearStreamMetadata(out currentExtMediaInfo, out currentExtStreamInfo, out currentExtProgramDateTime, out currentByteRange, out currentExtPartInfo);
continue;
}

if (workingSlice[0] != '#')
{
var path = Path.Combine(basePath, workingSlice.ToString());
streams.Add(new Stream(currentExtMediaInfo, currentExtStreamInfo, currentExtPartInfo, currentExtProgramDateTime, currentByteRange, path));
currentExtMediaInfo = null;
currentExtStreamInfo = null;
currentExtProgramDateTime = default;
currentByteRange = default;
currentExtPartInfo = null;
ClearStreamMetadata(out currentExtMediaInfo, out currentExtStreamInfo, out currentExtProgramDateTime, out currentByteRange, out currentExtPartInfo);

if (lineEnd == -1)
break;
Expand All @@ -120,6 +124,16 @@ public static M3U8 Parse(ReadOnlySpan<char> text, string basePath = "")
return new M3U8(metadataBuilder.ToMetadata(), streams.ToArray());
}

private static void ClearStreamMetadata(out Stream.ExtMediaInfo currentExtMediaInfo, out Stream.ExtStreamInfo currentExtStreamInfo, out DateTimeOffset currentExtProgramDateTime,
out Stream.ExtByteRange currentByteRange, out Stream.ExtPartInfo currentExtPartInfo)
{
currentExtMediaInfo = null;
currentExtStreamInfo = null;
currentExtProgramDateTime = default;
currentByteRange = default;
currentExtPartInfo = null;
}

private static bool ParseM3U8Key(ReadOnlySpan<char> text, Metadata.Builder metadataBuilder, ref Stream.ExtMediaInfo extMediaInfo, ref Stream.ExtStreamInfo extStreamInfo,
ref DateTimeOffset extProgramDateTime, ref Stream.ExtByteRange byteRange, ref Stream.ExtPartInfo extPartInfo)
{
Expand Down

0 comments on commit ee7c269

Please sign in to comment.