From 85bd080a1765c795dfde07360ada29ff7a61b728 Mon Sep 17 00:00:00 2001 From: claincly Date: Fri, 22 Apr 2022 14:52:25 +0100 Subject: [PATCH] Catch unchecked exception in RtspSessionTiming parsing. Issue: google/ExoPlayer#10165 #minor-release PiperOrigin-RevId: 443653894 --- .../exoplayer2/source/rtsp/RtspMessageUtil.java | 15 +++++++++++++++ .../exoplayer2/source/rtsp/RtspSessionTiming.java | 13 +++++++------ .../source/rtsp/RtspSessionTimingTest.java | 6 +++--- 3 files changed, 25 insertions(+), 9 deletions(-) diff --git a/library/rtsp/src/main/java/com/google/android/exoplayer2/source/rtsp/RtspMessageUtil.java b/library/rtsp/src/main/java/com/google/android/exoplayer2/source/rtsp/RtspMessageUtil.java index 44d7743fe6..76b4beae9c 100644 --- a/library/rtsp/src/main/java/com/google/android/exoplayer2/source/rtsp/RtspMessageUtil.java +++ b/library/rtsp/src/main/java/com/google/android/exoplayer2/source/rtsp/RtspMessageUtil.java @@ -457,6 +457,21 @@ import java.util.regex.Pattern; "Invalid WWW-Authenticate header " + headerValue, /* cause= */ null); } + /** + * Throws {@link ParserException#createForMalformedManifest ParserException} if {@code expression} + * evaluates to false. + * + * @param expression The expression to evaluate. + * @param message The error message. + * @throws ParserException If {@code expression} is false. + */ + public static void checkManifestExpression(boolean expression, @Nullable String message) + throws ParserException { + if (!expression) { + throw ParserException.createForMalformedManifest(message, /* cause= */ null); + } + } + private static String getRtspStatusReasonPhrase(int statusCode) { switch (statusCode) { case 200: diff --git a/library/rtsp/src/main/java/com/google/android/exoplayer2/source/rtsp/RtspSessionTiming.java b/library/rtsp/src/main/java/com/google/android/exoplayer2/source/rtsp/RtspSessionTiming.java index 4f33beff96..711c60b365 100644 --- a/library/rtsp/src/main/java/com/google/android/exoplayer2/source/rtsp/RtspSessionTiming.java +++ b/library/rtsp/src/main/java/com/google/android/exoplayer2/source/rtsp/RtspSessionTiming.java @@ -15,8 +15,8 @@ */ package com.google.android.exoplayer2.source.rtsp; -import static com.google.android.exoplayer2.util.Assertions.checkArgument; -import static com.google.android.exoplayer2.util.Assertions.checkNotNull; +import static com.google.android.exoplayer2.source.rtsp.RtspMessageUtil.checkManifestExpression; +import static com.google.android.exoplayer2.util.Util.castNonNull; import androidx.annotation.Nullable; import com.google.android.exoplayer2.C; @@ -48,10 +48,11 @@ import java.util.regex.Pattern; long startTimeMs; long stopTimeMs; Matcher matcher = NPT_RANGE_PATTERN.matcher(sdpRangeAttribute); - checkArgument(matcher.matches()); + checkManifestExpression(matcher.matches(), /* message= */ sdpRangeAttribute); - String startTimeString = checkNotNull(matcher.group(1)); - if (startTimeString.equals("now")) { + @Nullable String startTimeString = matcher.group(1); + checkManifestExpression(startTimeString != null, /* message= */ sdpRangeAttribute); + if (castNonNull(startTimeString).equals("now")) { startTimeMs = LIVE_START_TIME; } else { startTimeMs = (long) (Float.parseFloat(startTimeString) * C.MILLIS_PER_SECOND); @@ -64,7 +65,7 @@ import java.util.regex.Pattern; } catch (NumberFormatException e) { throw ParserException.createForMalformedManifest(stopTimeString, e); } - checkArgument(stopTimeMs > startTimeMs); + checkManifestExpression(stopTimeMs >= startTimeMs, /* message= */ sdpRangeAttribute); } else { stopTimeMs = C.TIME_UNSET; } diff --git a/library/rtsp/src/test/java/com/google/android/exoplayer2/source/rtsp/RtspSessionTimingTest.java b/library/rtsp/src/test/java/com/google/android/exoplayer2/source/rtsp/RtspSessionTimingTest.java index bbf744c50d..fe7b0b58e5 100644 --- a/library/rtsp/src/test/java/com/google/android/exoplayer2/source/rtsp/RtspSessionTimingTest.java +++ b/library/rtsp/src/test/java/com/google/android/exoplayer2/source/rtsp/RtspSessionTimingTest.java @@ -20,6 +20,7 @@ import static org.junit.Assert.assertThrows; import androidx.test.ext.junit.runners.AndroidJUnit4; import com.google.android.exoplayer2.C; +import com.google.android.exoplayer2.ParserException; import org.junit.Test; import org.junit.runner.RunWith; @@ -62,8 +63,7 @@ public class RtspSessionTimingTest { } @Test - public void parseTiming_withInvalidRangeTiming_throwsIllegalArgumentException() { - assertThrows( - IllegalArgumentException.class, () -> RtspSessionTiming.parseTiming("npt=10.000-2.054")); + public void parseTiming_withInvalidRangeTiming_throwsParserException() { + assertThrows(ParserException.class, () -> RtspSessionTiming.parseTiming("npt=10.000-2.054")); } }