Commit 8c793184 by olly Committed by Oliver Woodman

Fix crash when bad VTT timestamp encountered

Issue: #3396

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=173868849
parent d02e1df4
WEBVTT # This comment is allowed
# First timestamp is missing the 1/1000ths component, but parse anyway.
00:00 --> 00:01.234
This is the first subtitle.
02.345 --> 00:03.456
This is the second subtitle.
0.0.0 --> 00:05.678
This should be discarded (too many dots).
00:06.789 --> not-a-timestamp
This should be discarded (not a timestamp).
......@@ -36,6 +36,7 @@ import java.util.List;
public class WebvttDecoderTest extends InstrumentationTestCase {
private static final String TYPICAL_FILE = "webvtt/typical";
private static final String TYPICAL_WITH_BAD_TIMESTAMPS = "webvtt/typical_with_bad_timestamps";
private static final String TYPICAL_WITH_IDS_FILE = "webvtt/typical_with_identifiers";
private static final String TYPICAL_WITH_COMMENTS_FILE = "webvtt/typical_with_comments";
private static final String WITH_POSITIONING_FILE = "webvtt/with_positioning";
......@@ -67,6 +68,17 @@ public class WebvttDecoderTest extends InstrumentationTestCase {
assertCue(subtitle, 2, 2345000, 3456000, "This is the second subtitle.");
}
public void testDecodeTypicalWithBadTimestamps() throws IOException, SubtitleDecoderException {
WebvttSubtitle subtitle = getSubtitleForTestAsset(TYPICAL_WITH_BAD_TIMESTAMPS);
// Test event count.
assertEquals(4, subtitle.getEventTimeCount());
// Test cues.
assertCue(subtitle, 0, 0, 1234000, "This is the first subtitle.");
assertCue(subtitle, 2, 2345000, 3456000, "This is the second subtitle.");
}
public void testDecodeTypicalWithIds() throws IOException, SubtitleDecoderException {
WebvttSubtitle subtitle = getSubtitleForTestAsset(TYPICAL_WITH_IDS_FILE);
......
......@@ -56,9 +56,13 @@ public final class WebvttParserUtil {
String[] parts = timestamp.split("\\.", 2);
String[] subparts = parts[0].split(":");
for (String subpart : subparts) {
value = value * 60 + Long.parseLong(subpart);
value = (value * 60) + Long.parseLong(subpart);
}
return (value * 1000 + Long.parseLong(parts[1])) * 1000;
value *= 1000;
if (parts.length == 2) {
value += Long.parseLong(parts[1]);
}
return value * 1000;
}
/**
......@@ -74,7 +78,7 @@ public final class WebvttParserUtil {
}
return Float.parseFloat(s.substring(0, s.length() - 1)) / 100;
}
/**
* Reads lines up to and including the next WebVTT cue header.
*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment