Skip to content

Commit df4ae49

Browse files
icbakercopybara-github
authored andcommitted
Tweak line-feed regex to avoid exponential backtracking
Before this change, the `(.|\f)*` pattern can lead to exponential backtracking because it has to try many different combinations. Using the extra `+` makes the matching 'possessive', so once a match is found no other alternatives are considered. This is safe for these regexes, because the pattern in question is always at the end of the match, so there's no trailing characters that might need to be 'removed' from the match during a backtrack. This is a follow-up to 2bd2600 and 92677e9. #cherrypick PiperOrigin-RevId: 829457102
1 parent ea2303e commit df4ae49

File tree

2 files changed

+3
-3
lines changed

2 files changed

+3
-3
lines changed

libraries/exoplayer_rtsp/src/main/java/androidx/media3/exoplayer/rtsp/SessionDescriptionParser.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@
3636

3737
// SDP line always starts with an one letter tag, followed by an equal sign. The information
3838
// under the given tag follows an optional space.
39-
private static final Pattern SDP_LINE_PATTERN = Pattern.compile("([a-z])=\\s?((?:.|\f)+)");
39+
private static final Pattern SDP_LINE_PATTERN = Pattern.compile("([a-z])=\\s?((?:.|\f)++)");
4040
// SDP line with a one letter tag, an equal sign, and an empty value.
4141
private static final Pattern SDP_LINE_WITH_EMPTY_VALUE_PATTERN = Pattern.compile("^([a-z])=$");
4242
// Matches an attribute line (with a= sdp tag removed. Example: range:npt=0-50.0).
4343
// Attribute can also be a flag, i.e. without a value, like recvonly. Reference RFC4566 Section 9
4444
// Page 43, under "token-char".
4545
private static final Pattern ATTRIBUTE_PATTERN =
4646
Pattern.compile(
47-
"([\\x21\\x23-\\x27\\x2a\\x2b\\x2d\\x2e\\x30-\\x39\\x41-\\x5a\\x5e-\\x7e]+)(?::((?:.|\f)*))?");
47+
"([\\x21\\x23-\\x27\\x2a\\x2b\\x2d\\x2e\\x30-\\x39\\x41-\\x5a\\x5e-\\x7e]+)(?::((?:.|\f)*+))?");
4848
// SDP media description line: <mediaType> <port> <transmissionProtocol> <rtpPayloadType>
4949
// For instance: audio 0 RTP/AVP 97
5050
private static final Pattern MEDIA_DESCRIPTION_PATTERN =

libraries/extractor/src/main/java/androidx/media3/extractor/text/webvtt/WebvttCueParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public final class WebvttCueParser {
121121
private static final int TEXT_ALIGNMENT_RIGHT = 5;
122122

123123
public static final Pattern CUE_HEADER_PATTERN =
124-
Pattern.compile("^(\\S+)\\s+-->\\s+(\\S+)((?:.|\\f)*)?$");
124+
Pattern.compile("^(\\S+)\\s+-->\\s+(\\S+)((?:.|\\f)*+)?$");
125125
private static final Pattern CUE_SETTING_PATTERN = Pattern.compile("(\\S+?):(\\S+)");
126126

127127
private static final char CHAR_LESS_THAN = '<';

0 commit comments

Comments
 (0)