Bug 1296532 - Use fallible arrays in MoofParser - r=jya
Use fallible allocations for arrays that could get arbitrarily long
(independant of the media file size.)
MozReview-Commit-ID: LWiKFVpYK5d
--- a/media/libstagefright/binding/MoofParser.cpp
+++ b/media/libstagefright/binding/MoofParser.cpp
@@ -847,25 +847,33 @@ Saiz::Saiz(Box& aBox, AtomType aDefaultT
}
if (flags & 1) {
mAuxInfoType = reader->ReadU32();
mAuxInfoTypeParameter = reader->ReadU32();
}
uint8_t defaultSampleInfoSize = reader->ReadU8();
uint32_t count = reader->ReadU32();
if (defaultSampleInfoSize) {
+ if (!mSampleInfoSize.SetCapacity(count, fallible)) {
+ LOG(Saiz, "OOM");
+ reader->DiscardRemaining();
+ return;
+ }
for (int i = 0; i < count; i++) {
- mSampleInfoSize.AppendElement(defaultSampleInfoSize);
+ MOZ_ALWAYS_TRUE(mSampleInfoSize.AppendElement(defaultSampleInfoSize,
+ fallible));
}
} else {
if (!reader->ReadArray(mSampleInfoSize, count)) {
- LOG(Saiz, "Incomplete Box (missing count:%u)", count);
+ LOG(Saiz, "Incomplete Box (OOM or missing count:%u)", count);
+ reader->DiscardRemaining();
return;
}
}
+ reader->DiscardRemaining();
mValid = true;
}
Saio::Saio(Box& aBox, AtomType aDefaultType)
: mAuxInfoType(aDefaultType)
, mAuxInfoTypeParameter(0)
{
BoxReader reader(aBox);
@@ -887,23 +895,27 @@ Saio::Saio(Box& aBox, AtomType aDefaultT
}
size_t count = reader->ReadU32();
need = (version ? sizeof(uint64_t) : sizeof(uint32_t)) * count;
if (reader->Remaining() < need) {
LOG(Saio, "Incomplete Box (have:%lld need:%lld)",
(uint64_t)reader->Remaining(), (uint64_t)need);
return;
}
- mOffsets.SetCapacity(count);
+ if (!mOffsets.SetCapacity(count, fallible)) {
+ LOG(Saiz, "OOM");
+ reader->DiscardRemaining();
+ return;
+ }
if (version == 0) {
for (size_t i = 0; i < count; i++) {
- mOffsets.AppendElement(reader->ReadU32());
+ MOZ_ALWAYS_TRUE(mOffsets.AppendElement(reader->ReadU32(), fallible));
}
} else {
for (size_t i = 0; i < count; i++) {
- mOffsets.AppendElement(reader->ReadU64());
+ MOZ_ALWAYS_TRUE(mOffsets.AppendElement(reader->ReadU64(), fallible));
}
}
mValid = true;
}
#undef LOG
}
--- a/media/libstagefright/binding/include/mp4_demuxer/ByteReader.h
+++ b/media/libstagefright/binding/include/mp4_demuxer/ByteReader.h
@@ -309,28 +309,46 @@ public:
if (!ptr) {
MOZ_ASSERT(false);
return 0;
}
return *reinterpret_cast<const T*>(ptr);
}
template <typename T>
- bool ReadArray(nsTArray<T>& aDest, size_t aLength)
+ MOZ_MUST_USE bool ReadArray(nsTArray<T>& aDest, size_t aLength)
{
auto ptr = Read(aLength * sizeof(T));
if (!ptr) {
return false;
}
aDest.Clear();
aDest.AppendElements(reinterpret_cast<const T*>(ptr), aLength);
return true;
}
+ template <typename T>
+ MOZ_MUST_USE bool ReadArray(FallibleTArray<T>& aDest, size_t aLength)
+ {
+ auto ptr = Read(aLength * sizeof(T));
+ if (!ptr) {
+ return false;
+ }
+
+ aDest.Clear();
+ if (!aDest.SetCapacity(aLength, mozilla::fallible)) {
+ return false;
+ }
+ MOZ_ALWAYS_TRUE(aDest.AppendElements(reinterpret_cast<const T*>(ptr),
+ aLength,
+ mozilla::fallible));
+ return true;
+ }
+
private:
const uint8_t* mPtr;
size_t mRemaining;
size_t mLength;
};
}
#endif
--- a/media/libstagefright/binding/include/mp4_demuxer/MoofParser.h
+++ b/media/libstagefright/binding/include/mp4_demuxer/MoofParser.h
@@ -142,27 +142,27 @@ struct Sample
class Saiz final : public Atom
{
public:
Saiz(Box& aBox, AtomType aDefaultType);
AtomType mAuxInfoType;
uint32_t mAuxInfoTypeParameter;
- nsTArray<uint8_t> mSampleInfoSize;
+ FallibleTArray<uint8_t> mSampleInfoSize;
};
class Saio final : public Atom
{
public:
Saio(Box& aBox, AtomType aDefaultType);
AtomType mAuxInfoType;
uint32_t mAuxInfoTypeParameter;
- nsTArray<uint64_t> mOffsets;
+ FallibleTArray<uint64_t> mOffsets;
};
class AuxInfo {
public:
AuxInfo(int64_t aMoofOffset, Saiz& aSaiz, Saio& aSaio);
private:
int64_t mMoofOffset;