From ee7c2696d649b6d68a3d1ca3168e61da2f51a61a Mon Sep 17 00:00:00 2001 From: ScrubN <72096833+ScrubN@users.noreply.github.com> Date: Sun, 31 Dec 2023 02:45:08 -0500 Subject: [PATCH] Fix potential IndexOutOfRangeException when parsing M3U8 --- TwitchDownloaderCore/Tools/M3U8.cs | 34 +++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/TwitchDownloaderCore/Tools/M3U8.cs b/TwitchDownloaderCore/Tools/M3U8.cs index 7814097a..ca8c505f 100644 --- a/TwitchDownloaderCore/Tools/M3U8.cs +++ b/TwitchDownloaderCore/Tools/M3U8.cs @@ -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; } @@ -90,15 +92,17 @@ public static M3U8 Parse(ReadOnlySpan 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; @@ -120,6 +124,16 @@ public static M3U8 Parse(ReadOnlySpan 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 text, Metadata.Builder metadataBuilder, ref Stream.ExtMediaInfo extMediaInfo, ref Stream.ExtStreamInfo extStreamInfo, ref DateTimeOffset extProgramDateTime, ref Stream.ExtByteRange byteRange, ref Stream.ExtPartInfo extPartInfo) {