Skip to content

Commit

Permalink
Add check to avoid doing illegal memory access from an invalid iterat…
Browse files Browse the repository at this point in the history
…or from std::prev()

PiperOrigin-RevId: 573248334
  • Loading branch information
MediaPipe Team authored and copybara-github committed Oct 13, 2023
1 parent 652792e commit 8823046
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions mediapipe/framework/tool/switch_demux_calculator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
// limitations under the License.

#include <algorithm>
#include <iterator>
#include <memory>
#include <queue>
#include <set>
Expand Down Expand Up @@ -223,8 +224,8 @@ absl::Status SwitchDemuxCalculator::RecordPackets(CalculatorContext* cc) {

// Returns the channel index for a Timestamp.
int SwitchDemuxCalculator::ChannelIndex(Timestamp timestamp) {
auto it = std::prev(channel_history_.upper_bound(timestamp));
return it->second;
auto it = channel_history_.upper_bound(timestamp);
return it == channel_history_.begin() ? -1 : std::prev(it)->second;
}

// Dispatches all queued input packets with known channels.
Expand All @@ -237,10 +238,12 @@ absl::Status SwitchDemuxCalculator::SendActivePackets(CalculatorContext* cc) {
auto& queue = input_queue_[input_id];
while (!queue.empty() && queue.front().Timestamp() <= channel_settled) {
int channel_index = ChannelIndex(queue.front().Timestamp());
std::string output_tag = tool::ChannelTag(tag, channel_index);
auto output_id = cc->Outputs().GetId(output_tag, index);
if (output_id.IsValid()) {
cc->Outputs().Get(output_id).AddPacket(queue.front());
if (channel_index != -1) {
std::string output_tag = tool::ChannelTag(tag, channel_index);
auto output_id = cc->Outputs().GetId(output_tag, index);
if (output_id.IsValid()) {
cc->Outputs().Get(output_id).AddPacket(queue.front());
}
}
queue.pop();
}
Expand Down

0 comments on commit 8823046

Please sign in to comment.