Catch unchecked exception in RtspSessionTiming parsing.

Issue: google/ExoPlayer#10165
#minor-release
PiperOrigin-RevId: 443653894
This commit is contained in:
claincly 2022-04-22 14:52:25 +01:00 committed by Ian Baker
parent c8744ee9bd
commit 59ced5325c
4 changed files with 27 additions and 9 deletions

View File

@ -67,6 +67,8 @@
([#56](https://github.com/androidx/media/pull/56)). ([#56](https://github.com/androidx/media/pull/56)).
* Fix RTSP basic authorization header. * Fix RTSP basic authorization header.
([#9544](https://github.com/google/ExoPlayer/issues/9544)). ([#9544](https://github.com/google/ExoPlayer/issues/9544)).
* Throw checked exception when parsing RTSP timing
([#10165](https://github.com/google/ExoPlayer/issues/10165)).
* Session: * Session:
* Fix NPE in MediaControllerImplLegacy * Fix NPE in MediaControllerImplLegacy
([#59](https://github.com/androidx/media/pull/59)) ([#59](https://github.com/androidx/media/pull/59))

View File

@ -459,6 +459,21 @@ import java.util.regex.Pattern;
"Invalid WWW-Authenticate header " + headerValue, /* cause= */ null); "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) { private static String getRtspStatusReasonPhrase(int statusCode) {
switch (statusCode) { switch (statusCode) {
case 200: case 200:

View File

@ -15,8 +15,8 @@
*/ */
package androidx.media3.exoplayer.rtsp; package androidx.media3.exoplayer.rtsp;
import static androidx.media3.common.util.Assertions.checkArgument; import static androidx.media3.common.util.Util.castNonNull;
import static androidx.media3.common.util.Assertions.checkNotNull; import static androidx.media3.exoplayer.rtsp.RtspMessageUtil.checkManifestExpression;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import androidx.media3.common.C; import androidx.media3.common.C;
@ -50,10 +50,11 @@ import java.util.regex.Pattern;
long startTimeMs; long startTimeMs;
long stopTimeMs; long stopTimeMs;
Matcher matcher = NPT_RANGE_PATTERN.matcher(sdpRangeAttribute); Matcher matcher = NPT_RANGE_PATTERN.matcher(sdpRangeAttribute);
checkArgument(matcher.matches()); checkManifestExpression(matcher.matches(), /* message= */ sdpRangeAttribute);
String startTimeString = checkNotNull(matcher.group(1)); @Nullable String startTimeString = matcher.group(1);
if (startTimeString.equals("now")) { checkManifestExpression(startTimeString != null, /* message= */ sdpRangeAttribute);
if (castNonNull(startTimeString).equals("now")) {
startTimeMs = LIVE_START_TIME; startTimeMs = LIVE_START_TIME;
} else { } else {
startTimeMs = (long) (Float.parseFloat(startTimeString) * C.MILLIS_PER_SECOND); startTimeMs = (long) (Float.parseFloat(startTimeString) * C.MILLIS_PER_SECOND);
@ -66,7 +67,7 @@ import java.util.regex.Pattern;
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
throw ParserException.createForMalformedManifest(stopTimeString, e); throw ParserException.createForMalformedManifest(stopTimeString, e);
} }
checkArgument(stopTimeMs > startTimeMs); checkManifestExpression(stopTimeMs >= startTimeMs, /* message= */ sdpRangeAttribute);
} else { } else {
stopTimeMs = C.TIME_UNSET; stopTimeMs = C.TIME_UNSET;
} }

View File

@ -19,6 +19,7 @@ import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertThrows;
import androidx.media3.common.C; import androidx.media3.common.C;
import androidx.media3.common.ParserException;
import androidx.test.ext.junit.runners.AndroidJUnit4; import androidx.test.ext.junit.runners.AndroidJUnit4;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -62,8 +63,7 @@ public class RtspSessionTimingTest {
} }
@Test @Test
public void parseTiming_withInvalidRangeTiming_throwsIllegalArgumentException() { public void parseTiming_withInvalidRangeTiming_throwsParserException() {
assertThrows( assertThrows(ParserException.class, () -> RtspSessionTiming.parseTiming("npt=10.000-2.054"));
IllegalArgumentException.class, () -> RtspSessionTiming.parseTiming("npt=10.000-2.054"));
} }
} }