Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Add checks to prevent crash seen in load testing #4

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Source/C++/Core/Ap4Atom.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,12 @@ AP4_AtomListWriter::Action(AP4_Atom* atom) const
m_Stream.Tell(after);

AP4_UI64 bytes_written = after-before;
AP4_ASSERT(bytes_written <= atom->GetSize());
// SV: Replaced AP4_ASSERT() with error check to prevent crash in production
// AP4_ASSERT(bytes_written <= atom->GetSize());
if (bytes_written > atom->GetSize()) {
AP4_Debug("ERROR: bytes_written (%llu) exceeds atom size (%llu)\n", bytes_written, atom->GetSize());
return AP4_FAILURE;
}
if (bytes_written < atom->GetSize()) {
AP4_Debug("WARNING: atom serialized to fewer bytes than declared size\n");
AP4_UI64 padding = atom->GetSize()-bytes_written;
Expand Down
18 changes: 15 additions & 3 deletions Source/C++/Core/Ap4CommonEncryption.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1757,7 +1757,9 @@ AP4_CencEncryptingProcessor::CreateFragmentHandler(AP4_TrakAtom* trak,
if (tfhd_flags & AP4_TFHD_FLAG_SAMPLE_DESCRIPTION_INDEX_PRESENT) {
sample_description_index = tfhd->GetSampleDescriptionIndex();
} else {
sample_description_index = trex->GetDefaultSampleDescriptionIndex();
if (trex) {
sample_description_index = trex->GetDefaultSampleDescriptionIndex();
}
}
if (sample_description_index > 0) {
clear_sample_description_index = sample_description_index+stsd->GetSampleDescriptionCount()/2;
Expand Down Expand Up @@ -2447,7 +2449,10 @@ AP4_CencDecryptingProcessor::CreateFragmentHandler(AP4_TrakAtom* trak,
AP4_CencTrackDecrypter* track_decrypter =
AP4_DYNAMIC_CAST(AP4_CencTrackDecrypter, m_TrackHandlers[i]);
if (track_decrypter) {
unsigned int index = trex->GetDefaultSampleDescriptionIndex();
unsigned int index = 0;
if (trex) {
index = trex->GetDefaultSampleDescriptionIndex();
}
unsigned int tfhd_flags = tfhd->GetFlags();
if (tfhd_flags & AP4_TFHD_FLAG_SAMPLE_DESCRIPTION_INDEX_PRESENT) {
index = tfhd->GetSampleDescriptionIndex();
Expand Down Expand Up @@ -2878,7 +2883,9 @@ AP4_CencSampleInfoTable::Create(AP4_UI08 flags,
AP4_Atom* atom = item->GetData();
if (atom->GetType() == AP4_ATOM_TYPE_TRUN) {
AP4_TrunAtom* trun = AP4_DYNAMIC_CAST(AP4_TrunAtom, atom);
sample_info_count += trun->GetEntries().ItemCount();
if (trun) {
sample_info_count += trun->GetEntries().ItemCount();
}
}
}

Expand Down Expand Up @@ -2906,6 +2913,11 @@ AP4_CencSampleInfoTable::Create(AP4_UI08 flags,
AP4_Atom* atom = item->GetData();
if (atom->GetType() == AP4_ATOM_TYPE_TRUN) {
AP4_TrunAtom* trun = AP4_DYNAMIC_CAST(AP4_TrunAtom, atom);
if (trun == NULL) {
AP4_Debug("ERROR: trun is NULL while traversing traf\n");
result = AP4_ERROR_INVALID_FORMAT;
goto end;
}

if (saio_index == 0) {
aux_info_data.Seek(aux_info_data_offset+saio.GetEntries()[0]);
Expand Down
12 changes: 10 additions & 2 deletions Source/C++/Core/Ap4Processor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,11 @@ AP4_Processor::ProcessFragments(AP4_MoovAtom* moov,
for (;AP4_Atom* child = moof->GetChild(AP4_ATOM_TYPE_TRAF, handlers.ItemCount());) {
AP4_ContainerAtom* traf = AP4_DYNAMIC_CAST(AP4_ContainerAtom, child);
AP4_TfhdAtom* tfhd = AP4_DYNAMIC_CAST(AP4_TfhdAtom, traf->GetChild(AP4_ATOM_TYPE_TFHD));

if (tfhd == NULL) {
AP4_Debug("ERROR: tfhd is NULL while iterating traf atoms\n");
return AP4_ERROR_INVALID_FORMAT;
}

// find the 'trak' for this track
AP4_TrakAtom* trak = NULL;
for (AP4_List<AP4_Atom>::Item* child_item = moov->GetChildren().FirstItem();
Expand Down Expand Up @@ -259,7 +263,11 @@ AP4_Processor::ProcessFragments(AP4_MoovAtom* moov,
AP4_ContainerAtom* traf = AP4_DYNAMIC_CAST(AP4_ContainerAtom, moof->GetChild(AP4_ATOM_TYPE_TRAF, i));
if (traf == NULL) continue;
AP4_TfhdAtom* tfhd = AP4_DYNAMIC_CAST(AP4_TfhdAtom, traf->GetChild(AP4_ATOM_TYPE_TFHD));

if (tfhd == NULL) {
AP4_Debug("ERROR: tfhd is NULL while iterating all tracks\n");
return AP4_ERROR_INVALID_FORMAT;
}

// compute the base data offset
AP4_UI64 base_data_offset;
if (tfhd->GetFlags() & AP4_TFHD_FLAG_BASE_DATA_OFFSET_PRESENT) {
Expand Down
Loading