Improve handling of failed NAL prefix reads

nalPrefix.readUnsignedIntToInt() will throw if the unsigned output doesn't fit
in an int. Before this change, in the error case we'd retry reading from after
the NAL prefix position. Throw a ParserException so that reading an obviously
invalid NAL prefix will be treated as a fatal exception by the default load
error handling policy.

PiperOrigin-RevId: 236839690
This commit is contained in:
andrewlewis 2019-03-05 14:51:07 +00:00 committed by Oliver Woodman
parent 034209c5a2
commit febb8c589e
2 changed files with 11 additions and 3 deletions

View file

@ -1251,7 +1251,11 @@ public class FragmentedMp4Extractor implements Extractor {
// Read the NAL length so that we know where we find the next one, and its type.
input.readFully(nalPrefixData, nalUnitLengthFieldLengthDiff, nalUnitPrefixLength);
nalPrefix.setPosition(0);
sampleCurrentNalBytesRemaining = nalPrefix.readUnsignedIntToInt() - 1;
int nalLengthInt = nalPrefix.readInt();
if (nalLengthInt < 1) {
throw new ParserException("Invalid NAL length");
}
sampleCurrentNalBytesRemaining = nalLengthInt - 1;
// Write a start code for the current NAL unit.
nalStartCode.setPosition(0);
output.sampleData(nalStartCode, 4);

View file

@ -520,9 +520,13 @@ public final class Mp4Extractor implements Extractor, SeekMap {
while (sampleBytesWritten < sampleSize) {
if (sampleCurrentNalBytesRemaining == 0) {
// Read the NAL length so that we know where we find the next one.
input.readFully(nalLength.data, nalUnitLengthFieldLengthDiff, nalUnitLengthFieldLength);
input.readFully(nalLengthData, nalUnitLengthFieldLengthDiff, nalUnitLengthFieldLength);
nalLength.setPosition(0);
sampleCurrentNalBytesRemaining = nalLength.readUnsignedIntToInt();
int nalLengthInt = nalLength.readInt();
if (nalLengthInt < 0) {
throw new ParserException("Invalid NAL length");
}
sampleCurrentNalBytesRemaining = nalLengthInt;
// Write a start code for the current NAL unit.
nalStartCode.setPosition(0);
trackOutput.sampleData(nalStartCode, 4);