From 30c14e235f19442753e5eeeb8a86cbc22960afb1 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Thu, 10 Apr 2014 22:02:39 +0300 Subject: [PATCH] Make sure TrackOffsetIters make sense Disallow creation of iterators that have a path not connected to the entry endpoint, or an out of range offset. --- source/libr2c2/trackoffsetiter.cpp | 33 ++++++++++++++++++++++++++---- source/libr2c2/trackoffsetiter.h | 4 ++++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/source/libr2c2/trackoffsetiter.cpp b/source/libr2c2/trackoffsetiter.cpp index f8e8a2d..9676985 100644 --- a/source/libr2c2/trackoffsetiter.cpp +++ b/source/libr2c2/trackoffsetiter.cpp @@ -24,25 +24,50 @@ TrackOffsetIter::TrackOffsetIter(Track *t, unsigned e, float o): _track(t, e), _offset(_track ? o : 0), _path((_track && _offset) ? _track->get_active_path() : -1) -{ } +{ + validate(); +} TrackOffsetIter::TrackOffsetIter(Track *t, unsigned e, unsigned p, float o): _track(t, e), _offset(o), _path(p) -{ } +{ + validate(); +} TrackOffsetIter::TrackOffsetIter(const TrackIter &t, float o): _track(t), _offset(_track ? o : 0), _path((_track && _offset) ? _track->get_active_path() : -1) -{ } +{ + validate(); +} TrackOffsetIter::TrackOffsetIter(const TrackIter &t, unsigned p, float o): _track(t), _offset(o), _path(p) -{ } +{ + validate(); +} + +void TrackOffsetIter::validate() +{ + if(!_track) + return; + + if(_offset<0) + throw invalid_argument("TrackOffsetIter::TrackOffsetIter"); + + if(_path>=0) + { + if(!_track.endpoint().has_path(_path)) + throw invalid_argument("TrackOffsetIter::TrackOffsetIter"); + if(_offset>_track->get_type().get_path_length(_path)) + throw invalid_argument("TrackOffsetIter::TrackOffsetIter"); + } +} BlockIter TrackOffsetIter::block_iter() const { diff --git a/source/libr2c2/trackoffsetiter.h b/source/libr2c2/trackoffsetiter.h index 776f7a2..3156c08 100644 --- a/source/libr2c2/trackoffsetiter.h +++ b/source/libr2c2/trackoffsetiter.h @@ -29,6 +29,10 @@ public: TrackOffsetIter(const TrackIter &, float = 0); TrackOffsetIter(const TrackIter &, unsigned, float); +private: + void validate(); + +public: Track *track() const { return _track.track(); } unsigned entry() const { return _track.entry(); } const TrackIter &track_iter() const { return _track; } -- 2.45.2